Debian Codegraph @

cassandane/Cassandane/Cyrus/JMAPMailbox.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::JMAPMailbox;
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
53 use lib '.';
54 use base qw(Cassandane::Cyrus::TestCase);
55 use Cassandane::Util::Log;
56
57 use lib '../perl/imap';
58 use Cyrus::DList;
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_realm => 'Cassandane',
68 conversations => 'yes',
69 conversations_counted_flags => "\\Draft \\Flagged \$IsMailingList \$IsNotification \$HasAttachment",
70 httpmodules => 'carddav caldav jmap',
71 specialuse_extra => '\\XSpecialUse \\XChats \\XTemplates \\XNotes',
72 notesmailbox => 'Notes',
73 httpallowcompress => 'no');
74
75 my $self = $class->SUPER::new({
76 config => $config,
77 jmap => 1,
78 adminstore => 1,
79 services => [ 'imap', 'http' ]
80 }, @args);
81
82 $self->needs('component', 'jmap');
83 return $self;
84 }
85
86 sub setup_default_using
87 {
88 my ($self) = @_;
89 $self->{jmap}->DefaultUsing([
90 'urn:ietf:params:jmap:core',
91 'urn:ietf:params:jmap:mail',
92 ]);
93 }
94
95 sub set_up
96 {
97 my ($self) = @_;
98 $self->SUPER::set_up();
99
100 if ($self->{jmap}) {
101 $self->setup_default_using();
102 }
103 # n.b. tests that use :NoStartInstances will need to call
104 # $self->setup_default_using() themselves!
105 }
106
107 sub getinbox
108 {
109 my ($self, $args) = @_;
110
111 $args = {} unless $args;
112
113 my $jmap = $self->{jmap};
114
115 xlog $self, "get existing mailboxes";
116 my $res = $jmap->CallMethods([['Mailbox/get', $args, "R1"]]);
117 $self->assert_not_null($res);
118
119 my %m = map { $_->{name} => $_ } @{$res->[0][1]{list}};
120 return $m{"Inbox"};
121 }
122
123 sub _check_one_count {
124 my $self = shift;
125 my $want = shift;
126 my $have = shift;
127 my $name = shift;
128 $self->assert_num_equals($want, $have);
129 }
130
131 sub _check_counts
132 {
133 my $self = shift;
134 my $name = shift;
135 my %expect = @_;
136
137 my $jmap = $self->{jmap};
138
139 my $res = $jmap->CallMethods([['Mailbox/get', {}, 'R']]);
140
141 # "totalEmails": 3,
142 # "unreadEmails": 1,
143 # "totalThreads": 3,
144 # "unreadThreads": 1,
145
146 for my $folder (@{$res->[0][1]{list}}) {
147 my $want = $expect{$folder->{name}};
148 next unless $want;
149 $self->_check_one_count($want->[0], $folder->{totalEmails}, "$folder->{name} totalEmails");
150 $self->_check_one_count($want->[1], $folder->{unreadEmails}, "$folder->{name} unreadEmails");
151 $self->_check_one_count($want->[2], $folder->{totalThreads}, "$folder->{name} totalThreads");
152 $self->_check_one_count($want->[3], $folder->{unreadThreads}, "$folder->{name} unreadThreads");
153 }
154 }
155
156 use Cassandane::Tiny::Loader 'tiny-tests/JMAPMailbox';
157
158 1;