| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Cyrus::CaldavAlarm; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use DateTime; |
| 44 |
use DateTime::Format::ISO8601; |
| 45 |
use JSON::XS; |
| 46 |
use Net::CalDAVTalk 0.05; |
| 47 |
use Mail::JMAPTalk 0.13; |
| 48 |
use Data::Dumper; |
| 49 |
use POSIX; |
| 50 |
use Carp; |
| 51 |
|
| 52 |
use lib '.'; |
| 53 |
use base qw(Cassandane::Cyrus::TestCase); |
| 54 |
use Cassandane::Util::Log; |
| 55 |
|
| 56 |
sub new |
| 57 |
{ |
| 58 |
my $class = shift; |
| 59 |
|
| 60 |
my $config = Cassandane::Config->default()->clone(); |
| 61 |
$config->set(caldav_realm => 'Cassandane'); |
| 62 |
$config->set(conversations => 'yes'); |
| 63 |
$config->set(httpmodules => 'caldav jmap tzdist'); |
| 64 |
$config->set(httpallowcompress => 'no'); |
| 65 |
$config->set(caldav_historical_age => -1); |
| 66 |
$config->set(calendar_minimum_alarm_interval => '61s'); |
| 67 |
$config->set(jmap_nonstandard_extensions => 'yes'); |
| 68 |
|
| 69 |
my $self = $class->SUPER::new({ |
| 70 |
config => $config, |
| 71 |
jmap => 1, |
| 72 |
adminstore => 1, |
| 73 |
services => ['imap', 'http'], |
| 74 |
}, @_); |
| 75 |
|
| 76 |
$self->needs('component', 'calalarmd'); |
| 77 |
return $self; |
| 78 |
} |
| 79 |
|
| 80 |
sub set_up |
| 81 |
{ |
| 82 |
my ($self) = @_; |
| 83 |
$self->SUPER::set_up(); |
| 84 |
my $service = $self->{instance}->get_service("http"); |
| 85 |
$ENV{DEBUGDAV} = 1; |
| 86 |
$self->{caldav} = Net::CalDAVTalk->new( |
| 87 |
user => 'cassandane', |
| 88 |
password => 'pass', |
| 89 |
host => $service->host(), |
| 90 |
port => $service->port(), |
| 91 |
scheme => 'http', |
| 92 |
url => '/', |
| 93 |
expandurl => 1, |
| 94 |
); |
| 95 |
$self->{jmap}->DefaultUsing([ |
| 96 |
'urn:ietf:params:jmap:core', |
| 97 |
'urn:ietf:params:jmap:calendars', |
| 98 |
'urn:ietf:params:jmap:principals', |
| 99 |
'urn:ietf:params:jmap:calendars:preferences', |
| 100 |
'https://cyrusimap.org/ns/jmap/calendars', |
| 101 |
'https://cyrusimap.org/ns/jmap/debug', |
| 102 |
]); |
| 103 |
} |
| 104 |
|
| 105 |
sub _can_match { |
| 106 |
my $event = shift; |
| 107 |
my $want = shift; |
| 108 |
|
| 109 |
# I wrote a really good one of these for Caldav, but this will do for now |
| 110 |
foreach my $key (keys %$want) { |
| 111 |
return 0 if not exists $event->{$key}; |
| 112 |
return 0 if $event->{$key} ne $want->{$key}; |
| 113 |
} |
| 114 |
|
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
sub assert_alarms { |
| 119 |
my $self = shift; |
| 120 |
my @want = @_; |
| 121 |
# pick first calendar alarm from notifications |
| 122 |
my $data = $self->{instance}->getnotify(); |
| 123 |
if ($self->{replica}) { |
| 124 |
my $more = $self->{replica}->getnotify(); |
| 125 |
push @$data, @$more; |
| 126 |
} |
| 127 |
my @events; |
| 128 |
foreach (@$data) { |
| 129 |
if ($_->{CLASS} eq 'EVENT') { |
| 130 |
my $e = decode_json($_->{MESSAGE}); |
| 131 |
if ($e->{event} eq "CalendarAlarm") { |
| 132 |
push @events, $e; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
my @left; |
| 138 |
while (my $event = shift @events) { |
| 139 |
my $found = 0; |
| 140 |
my @newwant; |
| 141 |
foreach my $data (@want) { |
| 142 |
if (not $found and _can_match($event, $data)) { |
| 143 |
$found = 1; |
| 144 |
} |
| 145 |
else { |
| 146 |
push @newwant, $data; |
| 147 |
} |
| 148 |
} |
| 149 |
if (not $found) { |
| 150 |
push @left, $event; |
| 151 |
} |
| 152 |
@want = @newwant; |
| 153 |
} |
| 154 |
|
| 155 |
if (@want or @left) { |
| 156 |
my $dump = Data::Dumper->Dump([\@want, \@left], [qw(want left)]); |
| 157 |
$self->assert_equals(0, scalar @want, |
| 158 |
"expected events were not received:\n$dump"); |
| 159 |
$self->assert_equals(0, scalar @left, |
| 160 |
"unexpected extra events were received:\n$dump"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
sub tear_down |
| 165 |
{ |
| 166 |
my ($self) = @_; |
| 167 |
|
| 168 |
$self->SUPER::tear_down(); |
| 169 |
} |
| 170 |
|
| 171 |
use Cassandane::Tiny::Loader 'tiny-tests/CaldavAlarm'; |
| 172 |
|
| 173 |
1; |