#!perl
use Cassandane::Tiny;

sub test_no_shared_calendar
    :min_version_3_5 :JMAPExtensions :NoAltNameSpace
    ($self)
{
    my $jmap = $self->default_user->jmap;
    my $default_cal_id = $self->default_user->calendars->default->id;

    xlog $self, "create other user";
    my $admintalk = $self->{adminstore}->get_client();
    $admintalk->create('user.other');
    $admintalk->setacl('user.other', admin => 'lrswipkxtecdan') or die;
    $admintalk->setacl('user.other', other => 'lrswipkxtecdn') or die;

    my $other_user = $self->{instance}->create_user_without_setup('other');

    my $otherJmap = $other_user->jmap;
    $otherJmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:calendars',
        'https://cyrusimap.org/ns/jmap/calendars',
    ]);

    my $res = $otherJmap->CallMethods([
        ['Calendar/get', {
            properties => ['id'],
        }, 'R1'],
    ]);
    my $otherCalendarId = $res->[0][1]{list}[0]{id};
    $self->assert_not_null($otherCalendarId);
    $admintalk->setacl('user.other.#calendars', cassandane => 'lr') or die;

    my $session = $jmap->get_client_session->client_session;

    my $capabilities = $session->{accounts}{other}{accountCapabilities};
    $self->assert_not_null($capabilities->{'https://cyrusimap.org/ns/jmap/calendars'});

    $res = $jmap->CallMethods([
        ['Calendar/get', {
            accountId => 'other',
        }, 'R1'],
        ['Calendar/changes', {
            accountId => 'other',
            sinceState => '0',
        }, 'R2'],
        ['Calendar/set', {
            accountId => 'other',
            create => {
                calendar1 => {
                    name => 'test',
                },
            },
            update => {
                $otherCalendarId => {
                    name => 'test',
                },
            },
        }, 'R3'],
        ['Calendar/set', {
            accountId => 'other',
            destroy => [$otherCalendarId],
        }, 'R3.5'],
        ['CalendarEvent/get', {
            accountId => 'other',
        }, 'R4'],
    ]);
    $self->assert_deep_equals([], $res->[0][1]{list});
    $self->assert_deep_equals([], $res->[1][1]{created});
    $self->assert_deep_equals([], $res->[1][1]{updated});
    $self->assert_deep_equals([], $res->[1][1]{destroyed});
    $self->assert_str_equals('accountReadOnly',
        $res->[2][1]{notCreated}{calendar1}{type});
    $self->assert_str_equals('notFound',
        $res->[2][1]{notUpdated}{$otherCalendarId}{type});
    $self->assert_str_equals('notFound',
        $res->[3][1]{notDestroyed}{$otherCalendarId}{type});
    $self->assert_deep_equals([], $res->[4][1]{list});
}
