#!perl
use Cassandane::Tiny;

sub test_mailbox_set_sharewith
    :NoAltNameSpace :JMAPExtensions
    ($self)
{
    my $jmap = $self->{jmap};
    my $admin = $self->{adminstore}->get_client();

    my $using = [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:mail',
        'urn:ietf:params:jmap:mail:share',
    ];

    my $inboxId = $self->getinbox()->{id};
    $self->assert_not_null($inboxId);

    $self->{instance}->create_user("sharee");

    my %no_rights = map {; $_ => JSON::false } qw(
        mayReadItems mayAddItems mayRemoveItems mayCreateChild mayDelete
        mayRename maySubmit maySetSeen maySetKeywords mayShare
    );

    xlog $self, "Overwrite shareWith";
    my $res = $jmap->CallMethods([
        ['Mailbox/get', {
            ids => [$inboxId],
            properties => ['shareWith'],
        }, 'R1'],
        ['Mailbox/set', {
            update => {
                $inboxId => {
                    shareWith => {
                        sharee => {
                            mayReadItems => JSON::true,
                        },
                    },
                },
            },
        }, 'R2'],
        ['Mailbox/get', {
            ids => [$inboxId],
            properties => ['shareWith'],
        }, 'R3'],
    ], $using);

    $self->assert_null($res->[0][1]{list}[0]{shareWith});
    $self->assert_cmp_deeply({
        sharee => { %no_rights, mayReadItems => JSON::true },
    }, $res->[2][1]{list}[0]{shareWith});
    my $acl = $admin->getacl("user.cassandane");
    my %map = @$acl;
    $self->assert_str_equals('lr', $map{sharee});

    xlog $self, "Patch shareWith";
    $res = $jmap->CallMethods([
        ['Mailbox/set', {
            update => {
                $inboxId => {
                    'shareWith/sharee/maySetSeen' => JSON::true,
                },
            },
        }, 'R1'],
        ['Mailbox/get', {
            ids => [$inboxId],
            properties => ['shareWith'],
        }, 'R2'],
    ], $using);

    $self->assert_cmp_deeply({
        sharee => {
            %no_rights,
            mayReadItems => JSON::true,
            maySetSeen   => JSON::true,
        },
    }, $res->[1][1]{list}[0]{shareWith});
    $acl = $admin->getacl("user.cassandane");
    %map = @$acl;
    $self->assert_str_equals('lrs', $map{sharee});

    xlog $self, "Patch shareWith with unknown right";
    $res = $jmap->CallMethods([
        ['Mailbox/set', {
            update => {
                $inboxId => {
                    'shareWith/sharee/unknownRight' => JSON::true,
                },
            },
        }, 'R1'],
    ], $using);
    $self->assert_str_equals('invalidProperties',
        $res->[0][1]{notUpdated}{$inboxId}{type});
    $self->assert_cmp_deeply(['shareWith/sharee/unknownRight'],
        $res->[0][1]{notUpdated}{$inboxId}{properties});

    xlog $self, "Try to give access to a bogus userid";
    $res = $jmap->CallMethods([
            ['Mailbox/set', {
                    update => { $inboxId => {
                        shareWith => {
                            "../v/victim" => {
                                mayReadItems => JSON::true
                            }
                        },
             }}}, "R1"]
    ], $using);
    $self->assert_cmp_deeply(
        {
            $inboxId => {
                type => 'invalidProperties',
                properties => [
                    "shareWith/..~1v~1victim"
                ]
            }
        },
        $res->[0][1]{notUpdated});

    # auth_unix drops a group that isn't in /etc/group from the ACL, so
    # this only checks that a group sharee is accepted
    xlog $self, "Give access to a group";
    $res = $jmap->CallMethods([
        ['Mailbox/set', {
            update => { $inboxId => {
                'shareWith/group:c1234' => { mayReadItems => JSON::true },
            }},
        }, 'R1'],
    ], $using);
    $self->assert(exists $res->[0][1]{updated}{$inboxId});

    xlog $self, "Try to give access to a bogus group";
    $res = $jmap->CallMethods([
        ['Mailbox/set', {
            update => { $inboxId => {
                shareWith => { "group:../v/victim" => { mayReadItems => JSON::true } },
            }},
        }, 'R1'],
    ], $using);
    $self->assert_cmp_deeply(
        {
            $inboxId => {
                type => 'invalidProperties',
                properties => [ "shareWith/group:..~1v~1victim" ],
            }
        },
        $res->[0][1]{notUpdated});
}
