Debian Codegraph @

cassandane/Cassandane/Cyrus/JMAPEmail.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 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
32 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 # AND FITNESS, IN NO EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE
34 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::JMAPEmail;
41 use strict;
42 use warnings;
43 use DateTime;
44 use JSON::XS;
45 use Net::CalDAVTalk 0.09;
46 use Net::CardDAVTalk 0.03;
47 use Mail::JMAPTalk 0.13;
48 use Data::Dumper;
49 use Storable 'dclone';
50 use MIME::Base64 qw(encode_base64);
51 use Cwd qw(abs_path getcwd);
52 use URI;
53 use URI::Escape;
54
55 use lib '.';
56 use base qw(Cassandane::Cyrus::TestCase);
57 use Cassandane::Util::Log;
58 use Cassandane::Util::Slurp;
59
60 use charnames ':full';
61
62 sub new
63 {
64 my ($class, @args) = @_;
65
66 my $config = Cassandane::Config->default()->clone();
67 $config->set(caldav_historical_age => -1,
68 caldav_realm => 'Cassandane',
69 conversations => 'yes',
70 conversations_counted_flags => "\\Draft \\Flagged \$IsMailingList \$IsNotification \$HasAttachment \$muted",
71 defaultdomain => 'example.com',
72 httpallowcompress => 'no',
73 httpmodules => 'carddav caldav jmap',
74 imipnotifier => 'imip',
75 icalendar_max_size => 100000,
76 jmap_nonstandard_extensions => 'yes',
77 jmapsubmission_deleteonsend => 'no',
78 notesmailbox => 'Notes',
79 sync_log => 'yes');
80
81 # setup sieve
82 my ($maj, $min) = Cassandane::Instance->get_version();
83 if ($maj == 3 && $min == 0) {
84 # need to explicitly add 'body' to sieve_extensions for 3.0
85 $config->set(sieve_extensions =>
86 "fileinto reject vacation vacation-seconds imap4flags notify " .
87 "envelope relational regex subaddress copy date index " .
88 "imap4flags mailbox mboxmetadata servermetadata variables " .
89 "body");
90 }
91 elsif ($maj < 3) {
92 # also for 2.5 (the earliest Cyrus that Cassandane can test)
93 $config->set(sieve_extensions =>
94 "fileinto reject vacation vacation-seconds imap4flags notify " .
95 "envelope relational regex subaddress copy date index " .
96 "imap4flags body");
97 }
98 $config->set(sievenotifier => 'mailto');
99
100 my $self = $class->SUPER::new({
101 config => $config,
102 jmap => 1,
103 deliver => 1,
104 adminstore => 1,
105 services => [ 'imap', 'http', 'sieve' ]
106 }, @args);
107
108 $self->needs('component', 'jmap');
109 return $self;
110 }
111
112 sub set_up
113 {
114 my ($self) = @_;
115 $self->SUPER::set_up();
116 $self->{jmap}->DefaultUsing([
117 'urn:ietf:params:jmap:core',
118 'urn:ietf:params:jmap:mail',
119 'urn:ietf:params:jmap:submission',
120 ]);
121 }
122
123 sub getinbox
124 {
125 my ($self, $args) = @_;
126
127 $args = {} unless $args;
128
129 my $jmap = $self->{jmap};
130
131 xlog $self, "get existing mailboxes";
132 my $res = $jmap->CallMethods([['Mailbox/get', $args, "R1"]]);
133 $self->assert_not_null($res);
134
135 my %m = map { $_->{name} => $_ } @{$res->[0][1]{list}};
136 return $m{"Inbox"};
137 }
138
139 sub get_account_capabilities
140 :min_version_3_1 :needs_component_jmap
141 {
142 my ($self) = @_;
143
144 my $jmap = $self->{jmap};
145
146 my $Request;
147 my $Response;
148
149 xlog $self, "get session";
150 $Request = {
151 headers => {
152 'Authorization' => $jmap->auth_header(),
153 },
154 content => '',
155 };
156 $Response = $jmap->ua->get($jmap->uri(), $Request);
157 if ($ENV{DEBUGJMAP}) {
158 warn "JMAP " . Dumper($Request, $Response);
159 }
160 $self->assert_str_equals('200', $Response->{status});
161
162 my $session;
163 $session = eval { decode_json($Response->{content}) } if $Response->{success};
164
165 return $session->{accounts}{cassandane}{accountCapabilities};
166 }
167
168 sub defaultprops_for_email_get
169 {
170 return ( "id", "blobId", "threadId", "mailboxIds", "keywords", "size", "receivedAt", "messageId", "inReplyTo", "references", "sender", "from", "to", "cc", "bcc", "replyTo", "subject", "sentAt", "hasAttachment", "preview", "bodyValues", "textBody", "htmlBody", "attachments" );
171 }
172
173 sub download
174 {
175 my ($self, $accountid, $blobid) = @_;
176 my $jmap = $self->{jmap};
177
178 my $uri = $jmap->downloaduri($accountid, $blobid);
179 my %Headers;
180 $Headers{'Authorization'} = $jmap->auth_header();
181 my %getopts = (headers => \%Headers);
182 my $res = $jmap->ua->get($uri, \%getopts);
183 xlog $self, "JMAP DOWNLOAD @_ " . Dumper($res);
184 return $res;
185 }
186
187 sub email_query_window_internal
188 {
189 my ($self, %params) = @_;
190 my %exp;
191 my $jmap = $self->{jmap};
192 my $res;
193
194 $params{filter} //= undef;
195 $params{wantGuidSearch} //= JSON::false;
196 $params{calculateTotal} //= JSON::true;
197
198 my $using = [
199 'urn:ietf:params:jmap:core',
200 'urn:ietf:params:jmap:mail',
201 'urn:ietf:params:jmap:submission',
202 'https://cyrusimap.org/ns/jmap/mail',
203 'https://cyrusimap.org/ns/jmap/quota',
204 'https://cyrusimap.org/ns/jmap/debug',
205 'https://cyrusimap.org/ns/jmap/performance',
206 ];
207
208 my $imaptalk = $self->{store}->get_client();
209
210 # check IMAP server has the XCONVERSATIONS capability
211 $self->assert($self->{store}->get_client()->capability()->{xconversations});
212
213 xlog $self, "generating email A";
214 $exp{A} = $self->make_message("Email A");
215 $exp{A}->set_attributes(uid => 1, cid => $exp{A}->make_cid());
216
217 xlog $self, "generating email B";
218 $exp{B} = $self->make_message("Email B");
219 $exp{B}->set_attributes(uid => 2, cid => $exp{B}->make_cid());
220
221 xlog $self, "generating email C referencing A";
222 $exp{C} = $self->make_message("Re: Email A", references => [ $exp{A} ]);
223 $exp{C}->set_attributes(uid => 3, cid => $exp{A}->get_attribute('cid'));
224
225 xlog $self, "generating email D";
226 $exp{D} = $self->make_message("Email D");
227 $exp{D}->set_attributes(uid => 2, cid => $exp{B}->make_cid());
228
229 xlog $self, "run squatter";
230 $self->{instance}->run_command({cyrus => 1}, 'squatter');
231
232 xlog $self, "list all emails";
233 $res = $jmap->CallMethods([['Email/query', {
234 calculateTotal => JSON::true,
235 }, "R1"]]);
236 $self->assert_num_equals(4, scalar @{$res->[0][1]->{ids}});
237 $self->assert_num_equals(4, $res->[0][1]->{total});
238
239 my $ids = $res->[0][1]->{ids};
240 my @subids;
241
242 xlog $self, "list emails from position 1";
243 $res = $jmap->CallMethods([
244 ['Email/query', {
245 position => 1,
246 filter => $params{filter},
247 calculateTotal => $params{calculateTotal},
248 }, "R1"]
249 ], $using);
250 $self->assert_equals($params{wantGuidSearch},
251 $res->[0][1]{performance}{details}{isGuidSearch});
252 @subids = @{$ids}[1..3];
253 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
254 if ($params{calculateTotal}) {
255 $self->assert_num_equals(4, $res->[0][1]->{total});
256 }
257
258 xlog $self, "list emails from position 4";
259 $res = $jmap->CallMethods([
260 ['Email/query', {
261 position => 4,
262 filter => $params{filter},
263 calculateTotal => $params{calculateTotal},
264 }, "R1"]
265 ], $using);
266 $self->assert_equals($params{wantGuidSearch},
267 $res->[0][1]{performance}{details}{isGuidSearch});
268 $self->assert_num_equals(0, scalar @{$res->[0][1]->{ids}});
269 if ($params{calculateTotal}) {
270 $self->assert_num_equals(4, $res->[0][1]->{total});
271 }
272
273 xlog $self, "limit emails from position 1 to one email";
274 $res = $jmap->CallMethods([
275 ['Email/query', {
276 position => 1,
277 limit => 1,
278 filter => $params{filter},
279 calculateTotal => $params{calculateTotal},
280 }, "R1"]
281 ], $using);
282 $self->assert_equals($params{wantGuidSearch},
283 $res->[0][1]{performance}{details}{isGuidSearch});
284 @subids = @{$ids}[1..1];
285 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
286 $self->assert_num_equals(1, $res->[0][1]->{position});
287 if ($params{calculateTotal}) {
288 $self->assert_num_equals(4, $res->[0][1]->{total});
289 }
290
291 xlog $self, "anchor at 2nd email";
292 $res = $jmap->CallMethods([
293 ['Email/query', {
294 anchor => @{$ids}[1],
295 filter => $params{filter},
296 calculateTotal => $params{calculateTotal},
297 }, "R1"]
298 ], $using);
299 $self->assert_equals($params{wantGuidSearch},
300 $res->[0][1]{performance}{details}{isGuidSearch});
301 @subids = @{$ids}[1..3];
302 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
303 $self->assert_num_equals(1, $res->[0][1]->{position});
304 if ($params{calculateTotal}) {
305 $self->assert_num_equals(4, $res->[0][1]->{total});
306 }
307
308 xlog $self, "anchor at 2nd email and offset 1";
309 $res = $jmap->CallMethods([
310 ['Email/query', {
311 anchor => @{$ids}[1],
312 anchorOffset => 1,
313 filter => $params{filter},
314 calculateTotal => $params{calculateTotal},
315 }, "R1"]
316 ], $using);
317 $self->assert_equals($params{wantGuidSearch},
318 $res->[0][1]{performance}{details}{isGuidSearch});
319 @subids = @{$ids}[2..3];
320 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
321 $self->assert_num_equals(2, $res->[0][1]->{position});
322 if ($params{calculateTotal}) {
323 $self->assert_num_equals(4, $res->[0][1]->{total});
324 }
325
326 xlog $self, "anchor at 3rd email and offset -1";
327 $res = $jmap->CallMethods([
328 ['Email/query', {
329 anchor => @{$ids}[2],
330 anchorOffset => -1,
331 filter => $params{filter},
332 calculateTotal => $params{calculateTotal},
333 }, "R1"]
334 ], $using);
335 $self->assert_equals($params{wantGuidSearch},
336 $res->[0][1]{performance}{details}{isGuidSearch});
337 @subids = @{$ids}[1..3];
338 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
339 $self->assert_num_equals(1, $res->[0][1]->{position});
340 if ($params{calculateTotal}) {
341 $self->assert_num_equals(4, $res->[0][1]->{total});
342 }
343
344 xlog $self, "anchor at 1st email offset 1 and limit 2";
345 $res = $jmap->CallMethods([
346 ['Email/query', {
347 anchor => @{$ids}[0],
348 anchorOffset => 1,
349 limit => 2,
350 filter => $params{filter},
351 calculateTotal => $params{calculateTotal},
352 }, "R1"]
353 ], $using);
354 $self->assert_equals($params{wantGuidSearch},
355 $res->[0][1]{performance}{details}{isGuidSearch});
356 @subids = @{$ids}[1..2];
357 $self->assert_deep_equals(\@subids, $res->[0][1]->{ids});
358 $self->assert_num_equals(1, $res->[0][1]->{position});
359 if ($params{calculateTotal}) {
360 $self->assert_num_equals(4, $res->[0][1]->{total});
361 }
362 }
363
364 sub _set_quotaroot
365 {
366 my ($self, $quotaroot) = @_;
367 $self->{quotaroot} = $quotaroot;
368 }
369
370 sub _set_quotalimits
371 {
372 my ($self, %resources) = @_;
373 my $admintalk = $self->{adminstore}->get_client();
374
375 my $quotaroot = delete $resources{quotaroot} || $self->{quotaroot};
376 my @quotalist;
377 foreach my $resource (keys %resources)
378 {
379 my $limit = $resources{$resource}
380 or die "No limit specified for $resource";
381 push(@quotalist, uc($resource), $limit);
382 }
383 $self->{limits}->{$quotaroot} = { @quotalist };
384 $admintalk->setquota($quotaroot, \@quotalist);
385 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
386 }
387
388 use Cassandane::Tiny::Loader 'tiny-tests/JMAPEmail';
389
390 1;