#!perl
use Cassandane::Tiny;

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

    $admintalk->create("user.other");
    $admintalk->setacl("user.other", admin => 'lrswipkxtecdan');

    # trigger Default addressbook autocreate
    my $othertalk = Net::CardDAVTalk->new(
        user => "other", password => 'pass',
        host => $service->host(), port => $service->port(),
        scheme => 'http', url => '/', expandurl => 1,
    );

    $admintalk->setacl('user.other.#addressbooks.Default',
                       'cassandane' => 'lrs1');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    # PROPFIND the D:acl property as the owner and check the privilege
    my $xml = <<'EOF';
<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:">
  <D:prop><D:acl/></D:prop>
</D:propfind>
EOF
    my $res = $othertalk->Request('PROPFIND', 'Default', $xml,
                                  'Content-Type' => 'text/xml',
                                  'Depth' => '0');
    # Request returns a parsed XML hash; match the element name in the
    # flattened structure rather than hard-coding the nesting shape
    require Data::Dumper;
    my $flat = Data::Dumper::Dumper($res);
    $self->assert_matches(qr{auto-subscribe}, $flat);
}
