#!perl
use Cassandane::Tiny;

sub test_calendar_set_isdefault_requires_admin
    :min_version_3_1
    ($self)
{
    my $jmap = $self->default_user->jmap;
    my $admintalk = $self->{adminstore}->get_client();
    my $service = $self->{instance}->get_service("http");

    xlog $self, "create shared account";
    my $sharer = $self->{instance}->create_user('sharer');

    my $orig_default = $sharer->calendars->default;
    my $shared_cal   = $sharer->calendars->create({ name => 'Attack Target' });

    $orig_default->share_with(cassandane => [ qw( mayReadItems mayWriteAll ) ]);

    # Granting mayShare on a single shared calendar must not be enough to flip
    # the account's default calendar -- that is per-account state and requires
    # admin on the calendar home, not on the target alone.
    $shared_cal->share_with(cassandane => [ qw( mayReadItems mayWriteAll mayShare ) ]);

    xlog $self, "as cassandane, attempt to flip sharer's default via onSuccessSetIsDefault";
    my $set = $jmap->request([
        [ 'Calendar/set' => {
            accountId => 'sharer',
            onSuccessSetIsDefault => $shared_cal->id,
        } ],
    ])->single_sentence('Calendar/set')->as_set;

    xlog $self, "the refusal is reported as forbidden";
    $self->assert_str_equals('forbidden',
        $set->update_errors->{$shared_cal->id}{type});

    xlog $self, "re-read the default; it must not have changed";
    my $res = $sharer->jmap->CallMethods([
        [ 'Calendar/get' => { accountId => 'sharer' }, "R1" ],
    ]);
    my ($current_default) = grep { $_->{isDefault} } @{ $res->[0][1]{list} };
    $self->assert_not_null($current_default);
    $self->assert_str_equals($orig_default->{id}, $current_default->{id});
}
