#!perl
use Cassandane::Tiny;

sub test_card_set_importance_shared
    :min_version_3_1 :needs_dependency_icalvcard
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $carddav = $self->{carddav};
    my $admintalk = $self->{adminstore}->get_client();
    my $service = $self->{instance}->get_service("http");

    xlog $self, "create shared account";
    $admintalk->create("user.manifold");

    my $mantalk = Net::CardDAVTalk->new(
        user => "manifold",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    $admintalk->setacl("user.manifold", admin => 'lrswipkxtecdan');
    $admintalk->setacl("user.manifold", manifold => 'lrswipkxtecdn');
    xlog $self, "share to user";
    $admintalk->setacl("user.manifold.#addressbooks.Default",
                       "cassandane" => 'lrswipkxtecdn') or die;

    xlog $self, "create contact";
    my $res = $jmap->CallMethods([['ContactCard/set', {
                    accountId => 'manifold',
                    create => {"1" => {name => { full => "John Doe" }}}
    }, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('ContactCard/set', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);
    my $id = $res->[0][1]{created}{"1"}{id};

    $admintalk->setacl("user.manifold.#addressbooks.Default",
                       "cassandane" => 'lrsn') or die;

    xlog $self, "update importance";
    $res = $jmap->CallMethods([['ContactCard/set', {
                    accountId => 'manifold',
                    update => {$id => {"cyrusimap.org:importance" => -0.1}}
    }, "R2"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('ContactCard/set', $res->[0][0]);
    $self->assert_str_equals('R2', $res->[0][2]);
    $self->assert(exists $res->[0][1]{updated}{$id});
}
