#!perl
use Cassandane::Tiny;

sub test_options {
    my ($self) = @_;

    my $caldav = $self->{caldav};

    my $get_options = sub {
        my ($url) = shift;
        my $res = $caldav->ua()->request(
            'OPTIONS',
            $caldav->request_url($url),
            { headers => { Authorization => $caldav->auth_header() } }
        );
        return $res->{headers}{dav} || [];
    };

    # Non-calendar collections - no scheduling
    $self->assert_null(@{ $get_options->('/') });
    $self->assert(not grep(/calendar-auto-schedule/,
                           @{ $get_options->('/dav/') }));

    # Calendar collections - scheduling enabled by default
    $self->assert(grep(/calendar-auto-schedule/,
            @{ $get_options->('/dav/calendars/') }));
    $self->assert(grep(/calendar-auto-schedule/,
            @{ $get_options->('/dav/calendars/user/') }));
    $self->assert(grep(/calendar-auto-schedule/,
            @{ $get_options->('/dav/calendars/user/cassandane/') }));
    $self->assert(grep(/calendar-auto-schedule/,
            @{ $get_options->('/dav/calendars/user/cassandane/Default') }));

    # Disable scheduling on Default calendar
    my $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propertyupdate xmlns:D="DAV:" xmlns:CY="http://cyrusimap.org/ns/">
  <D:set>
    <D:prop>
      <CY:scheduling-enabled>F</CY:scheduling-enabled>
    </D:prop>
  </D:set>
</D:propertyupdate>
EOF
    
    my $res = $caldav->Request('PROPPATCH',
                               "/dav/calendars/user/cassandane/Default",
                               $xml, 'Content-Type' => 'text/xml');

    # Scheduling should NOT be advertised
    $self->assert(not grep(/calendar-auto-schedule/,
            @{ $get_options->('/dav/calendars/user/cassandane/Default') }));
}
