Debian Codegraph @

cassandane/Cassandane/Cyrus/MurderDAV.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2013 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::MurderDAV;
41 use strict;
42 use warnings;
43 use URI;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49 use Cassandane::Instance;
50
51 $Data::Dumper::Sortkeys = 1;
52
53 sub new
54 {
55 my ($class, @args) = @_;
56
57 my $config = Cassandane::Config->default()->clone();
58 $config->set('conversations' => 'yes');
59 $config->set_bits('httpmodules', 'caldav', 'carddav');
60
61 my $self = $class->SUPER::new({
62 config => $config,
63 httpmurder => 1,
64 jmap => 1,
65 adminstore => 1
66 }, @args);
67
68 $self->needs('component', 'murder');
69 $self->needs('component', 'httpd');
70 return $self;
71 }
72
73 sub set_up
74 {
75 my ($self) = @_;
76 $self->SUPER::set_up();
77 }
78
79 sub tear_down
80 {
81 my ($self) = @_;
82 $self->SUPER::tear_down();
83 }
84
85 sub test_aaa_setup
86 {
87 my ($self) = @_;
88
89 # does everything set up and tear down cleanly?
90 $self->assert(1);
91 }
92
93 # XXX This can't pass because we don't support multiple murder services
94 # XXX at once, but renaming out the "bogus" and running it, and it failing,
95 # XXX proves the infrastructure to prevent requesting both works.
96 sub bogustest_aaa_imapdav_setup
97 :IMAPMurder
98 {
99 my ($self) = @_;
100
101 # does everything set up and tear down cleanly?
102 $self->assert(1);
103 }
104
105 sub test_frontend_commands
106 :min_version_3_5
107 {
108 my ($self) = @_;
109 my $result;
110
111 my $frontend_svc = $self->{frontend}->get_service("http");
112 my $frontend_host = $frontend_svc->host();
113 my $frontend_port = $frontend_svc->port();
114 my $proxy_re = qr{
115 \b
116 ( localhost | $frontend_host )
117 : $frontend_port
118 \b
119 }x;
120
121 my $frontend_caldav = Net::CalDAVTalk->new(
122 user => 'cassandane',
123 password => 'pass',
124 host => $frontend_host,
125 port => $frontend_port,
126 scheme => 'http',
127 url => "http://$frontend_host:$frontend_port"
128 );
129
130 my $CALDAV = "urn:ietf:params:xml:ns:caldav";
131 my $CARDDAV = "urn:ietf:params:xml:ns:carddav";
132 my $xml = <<EOF;
133 <?xml version="1.0" encoding="UTF-8"?>
134 <D:propfind xmlns:D="DAV:" xmlns:C="$CALDAV" xmlns:A="$CARDDAV">
135 <D:prop>
136 <C:calendar-home-set/>
137 <A:addressbook-home-set/>
138 </D:prop>
139 </D:propfind>
140 EOF
141
142 xlog $self, "Get current-user-principal";
143 my $url = $frontend_caldav->GetCurrentUserPrincipal();
144 $self->assert_not_null($url);
145
146 # Copied from Net::DAVTalk::SetURL
147 my (undef, undef, undef, $cur_princ) =
148 $url =~ m{^http(s)?://([^/:]+)(?::(\d+))?(.*)?};
149
150 xlog $self, "PROPFIND for home-sets";
151 my $res = $frontend_caldav->Request('PROPFIND', $cur_princ,
152 $xml, 'Content-Type' => 'text/xml');
153
154 my $propstat = $res->{'{DAV:}response'}[0]{'{DAV:}propstat'}[0];
155 my $props = $propstat->{'{DAV:}prop'};
156 $self->assert_str_equals('HTTP/1.1 200 OK',
157 $propstat->{'{DAV:}status'}{content});
158 my $cal_home = $props->{"{$CALDAV}calendar-home-set"}{'{DAV:}href'}{content};
159 my $card_home =
160 $props->{"{$CARDDAV}addressbook-home-set"}{'{DAV:}href'}{content};
161 $self->assert_not_null($cal_home);
162 $self->assert_not_null($card_home);
163
164 xlog $self, "Create new calendar";
165 $frontend_caldav->SetURL($cal_home);
166 my $calid1 = $frontend_caldav->NewCalendar({name => 'foo'});
167 $self->assert_not_null($calid1);
168
169 xlog $self, "Change calendar name";
170 my $newid = $frontend_caldav->UpdateCalendar({ id => $calid1,
171 name => 'bar'});
172 $self->assert_str_equals($calid1, $newid);
173
174 xlog $self, "Create new event";
175 my $eventid1 = $frontend_caldav->NewEvent($calid1, {
176 timeZone => 'Etc/UTC',
177 start => '2015-01-01T12:00:00',
178 duration => 'PT1H',
179 title => 'waterfall',
180 });
181 $self->assert_not_null($eventid1);
182
183 xlog $self, "GET event";
184 $res = $frontend_caldav->Request('GET', $cal_home . $eventid1);
185 $self->assert_matches(qr/SUMMARY:waterfall/, $res->{content});
186
187 xlog $self, "Get calendars";
188 $res = $frontend_caldav->GetCalendars();
189 $self->assert_num_equals(2, scalar @{$res});
190
191 my $sync1;
192 my $sync2;;
193 my $calid2;
194 if ($res->[0]{id} eq $calid1) {
195 $sync1 = $res->[0]{syncToken};
196 $sync2 = $res->[1]{syncToken};
197 $calid2 = $res->[1]{id};
198 }
199 else {
200 $sync1 = $res->[1]{syncToken};
201 $sync2 = $res->[0]{syncToken};
202 $calid2 = $res->[0]{id};
203 }
204 $self->assert_not_null($calid1);
205 $self->assert_not_null($sync1);
206 $self->assert_not_null($sync2);
207
208 xlog $self, "Move event";
209 my $eventid2 = $frontend_caldav->MoveEvent($eventid1, $calid2);
210 $self->assert_not_null($eventid2);
211
212 xlog $self, "Sync Calendars";
213 my ($adds, $removes, $errors) =
214 $frontend_caldav->SyncEvents($calid1, syncToken => $sync1);
215 $self->assert_num_equals(0, scalar @{$adds});
216 $self->assert_num_equals(1, scalar @{$removes});
217 $self->assert_str_equals($removes->[0],
218 $frontend_caldav->fullpath($eventid1));
219
220 ($adds, $removes, $errors) =
221 $frontend_caldav->SyncEvents($calid2, syncToken => $sync2);
222 $self->assert_num_equals(1, scalar @{$adds});
223 $self->assert_str_equals($adds->[0]{href},
224 $frontend_caldav->fullpath($eventid2));
225 $self->assert_str_equals('waterfall', $adds->[0]{title});
226 $self->assert_num_equals(0, scalar @{$removes});
227
228 xlog $self, "Delete event";
229 $res = $frontend_caldav->DeleteEvent($eventid2);
230 $self->assert_num_equals(1, $res);
231
232 xlog $self, "Delete calendar";
233 $frontend_caldav->DeleteCalendar($calid1);
234 $res = $frontend_caldav->GetCalendar($calid1);
235 $self->assert_null($res);
236
237 # XXX test other commands
238 }
239
240 1;