#!perl
use Cassandane::Tiny;

# A sharee's per-user VALARM must stop firing once the owner revokes
# their access to the calendar
sub test_sharee_alarm_after_revoke
    :MagicPlus :NoAltNameSpace
{
    my ($self) = @_;

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

    my $CalendarId = $CalDAV->NewCalendar({name => 'foo'});
    $self->assert_not_null($CalendarId);
    my $mboxname = "user.cassandane.#calendars.$CalendarId";

    my $AdminTalk = $self->{adminstore}->get_client();
    $self->{instance}->create_user('foo');
    $AdminTalk->setacl($mboxname, foo => 'lrswipkxtecdn789');

    # Subscribing to a shared DAV mailbox needs the magic "+dav"
    # userid suffix.
    my $foostore = $self->{instance}->get_service('imap')->create_store(
                        username => "foo+dav");
    $foostore->get_client->subscribe($mboxname);

    my $service = $self->{instance}->get_service("http");
    my $FooDAV = Net::CalDAVTalk->new(
        user => 'foo',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $cal = $FooDAV->GetCalendar("cassandane.$CalendarId");
    $self->assert_not_null($cal);

    my $now = DateTime->now();
    $now->set_time_zone('Australia/Sydney');
    # bump everything forward so a slow run (say: valgrind)
    # doesn't cause things to magically fire...
    $now->add(DateTime::Duration->new(seconds => 300));

    my $startdt = $now->clone();
    $startdt->add(DateTime::Duration->new(seconds => 2));
    my $start = $startdt->strftime('%Y%m%dT%H%M%S');

    my $enddt = $startdt->clone();
    $enddt->add(DateTime::Duration->new(seconds => 15));
    my $end = $enddt->strftime('%Y%m%dT%H%M%S');

    my $latedt = $startdt->clone();
    $latedt->add(DateTime::Duration->new(seconds => 300));
    my $late = $latedt->strftime('%Y%m%dT%H%M%S');

    my $uuid = "574E2CD0-2D2A-4554-8B63-C7504481D3A9";
    my $href = "$CalendarId/$uuid.ics";
    my $cardtmpl = <<EOF;
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.10.4//EN
CALSCALE:GREGORIAN
BEGIN:VTIMEZONE
TZID:Australia/Sydney
BEGIN:STANDARD
DTSTART:19700101T000000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
TZOFFSETFROM:+1100
TZOFFSETTO:+1000
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19700101T000000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=10
TZOFFSETFROM:+1000
TZOFFSETTO:+1100
END:DAYLIGHT
END:VTIMEZONE

BEGIN:VEVENT
CREATED:20150806T234327Z
UID:574E2CD0-2D2A-4554-8B63-C7504481D3A9
DTEND;TZID=Australia/Sydney:$end
TRANSP:OPAQUE
SUMMARY:XXSUMMARYXX
DTSTART;TZID=Australia/Sydney:$start
DTSTAMP:20150806T234327Z
SEQUENCE:0
XXALARMDATAXX
END:VEVENT

END:VCALENDAR
EOF

    my $latealarm = <<EOF;
BEGIN:VALARM
TRIGGER:PT5M
ACTION:DISPLAY
SUMMARY: My alarm foo
DESCRIPTION:My latealarm has triggered
END:VALARM
EOF

    my $card = $cardtmpl;
    $card =~ s/XXSUMMARYXX/Simple/;
    $card =~ s/XXALARMDATAXX//;
    $CalDAV->Request('PUT', $href, $card, 'Content-Type' => 'text/calendar');

    my $FooEvents = $FooDAV->GetEvents("cassandane.$CalendarId");
    $self->assert_num_equals(1, scalar @$FooEvents);

    # foo accepts the share and sets their own alarm on the event while
    # they still have access
    my $foocard = $cardtmpl;
    $foocard =~ s/XXSUMMARYXX/Simple/;
    $foocard =~ s/XXALARMDATAXX/$latealarm/;
    $FooDAV->Request('PUT', $FooEvents->[0]{href}, $foocard,
                     'Content-Type' => 'text/calendar');

    $FooEvents = $FooDAV->GetEvents("cassandane.$CalendarId");
    $self->assert_not_null($FooEvents->[0]{alerts});

    # the owner revokes foo's access -- nothing clears foo's subscription
    # to the calendar
    $AdminTalk->setacl($mboxname, foo => '');

    # the owner edits the event after revoking access
    $card = $cardtmpl;
    $card =~ s/XXSUMMARYXX/Changed after revoke/;
    $card =~ s/XXALARMDATAXX//;
    $CalDAV->Request('PUT', $href, $card, 'Content-Type' => 'text/calendar');

    # clean notification cache
    $self->{instance}->getnotify();

    # fire foo's (former) alarm
    $self->{instance}->run_command({ cyrus => 1 }, 'calalarmd',
                                   '-t' => $now->epoch() + 600);

    # foo no longer has access to the calendar, so must not receive an
    # alarm notification -- especially not one containing the owner's
    # post-revocation edits
    $self->assert_alarms();
}
