#!perl
use Cassandane::Tiny;

sub test_no_shared_calendar
    :min_version_3_5 :needs_component_jmap :JMAPExtensions :NoAltNameSpace
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    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 $service = $self->{instance}->get_service("http");
    my $otherJmap = Mail::JMAPTalk->new(
        user => 'other',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/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;

    $res = $jmap->ua->get($jmap->uri(), {
        headers => {
            'Authorization' => $jmap->auth_header(),
        },
        content => '',
    });
    $self->assert_str_equals('200', $res->{status});
    my $session = eval { decode_json($res->{content}) };
    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',
                },
            },
            destroy => [$otherCalendarId],
        }, 'R3'],
        ['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->[2][1]{notDestroyed}{$otherCalendarId}{type});
    $self->assert_deep_equals([], $res->[3][1]{list});
}
