Debian Codegraph @

cassandane/Cassandane/Cyrus/Shared.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2024 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::Shared;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Instance;
49 use Cassandane::Mboxname;
50 use Cassandane::Util::Log;
51 use Cassandane::Util::Words;
52
53 $Data::Dumper::Sortkeys = 1;
54
55 sub new
56 {
57 my ($class, @args) = @_;
58
59 my $self = $class->SUPER::new({ adminstore => 1 }, @args);
60
61 return $self;
62 }
63
64 sub set_up
65 {
66 my ($self) = @_;
67
68 $self->SUPER::set_up();
69 }
70
71 sub tear_down
72 {
73 my ($self) = @_;
74
75 $self->SUPER::tear_down();
76 }
77
78 sub shared_subscribe_common
79 {
80 my ($self, $user1, $user2) = @_;
81
82 my $service = $self->{instance}->get_service('imap');
83 my $config = $self->{instance}->{config};
84 my $sep = $config->get_bool('unixhierarchysep', 'on') ? '/' : '.';
85
86 my $user1_inbox = Cassandane::Mboxname->new(config => $config);
87 $user1_inbox->from_username($user1);
88
89 my @user1_mailboxes = map {
90 $user1_inbox->make_child($_);
91 } random_words(3);
92 $self->{instance}->create_user($user1,
93 subdirs => \@user1_mailboxes);
94
95 my $user1_store = $service->create_store(username => $user1);
96 my $user1_talk = $user1_store->get_client();
97
98 foreach my $mb ($user1_inbox, @user1_mailboxes) {
99 $user1_talk->subscribe($mb->to_external('owner'));
100 $user1_talk->setacl($mb->to_external('owner'), $user2, 'lrs');
101 }
102
103 my $user2_inbox = Cassandane::Mboxname->new(config => $config);
104 $user2_inbox->from_username($user2);
105
106 my @user2_mailboxes = map {
107 $user2_inbox->make_child($_);
108 } random_words(3);
109 $self->{instance}->create_user($user2,
110 subdirs => \@user2_mailboxes);
111
112 my $user2_store = $service->create_store(username => $user2);
113 my $user2_talk = $user2_store->get_client();
114
115 foreach my $mb ($user2_inbox, @user2_mailboxes) {
116 $user2_talk->subscribe($mb->to_external('owner'));
117 $user2_talk->setacl($mb->to_external('owner'), $user1, 'lrs');
118 }
119
120 xlog("subscribe as $user1 to $user2\'s shared mb's");
121 foreach my $mb ($user2_inbox, @user2_mailboxes) {
122 $user1_talk->subscribe($mb->to_external('other'));
123 $self->assert_equals('ok', $user1_talk->get_last_completion_response());
124 }
125
126 xlog("make sure $user1 has the right subscriptions");
127 my $user1_subs = $user1_talk->list([qw(SUBSCRIBED)],
128 '', '*',
129 'RETURN', [qw(CHILDREN)]);
130 $self->assert_mailbox_structure($user1_subs, $sep, {
131 $user1_inbox->to_external('owner') => [ '\\Subscribed' ],
132 (map {(
133 $_->to_external('owner') => [ '\\Subscribed', '\\HasNoChildren' ]
134 )} @user1_mailboxes),
135 $user2_inbox->to_external('other') => [ '\\Subscribed' ],
136 (map {(
137 $_->to_external('other') => [
138 '\\Subscribed',
139 '\\HasNoChildren',
140 ]
141 )} @user2_mailboxes),
142 });
143
144 xlog("unsub as $user1 from $user2\'s folders");
145 foreach my $mb ($user2_inbox, @user2_mailboxes) {
146 $user1_talk->unsubscribe($mb->to_external('other'));
147 $self->assert_equals('ok', $user1_talk->get_last_completion_response());
148 }
149
150 xlog("make sure $user1 has the right subscriptions");
151 $user1_subs = $user1_talk->list([qw(SUBSCRIBED)],
152 '', '*',
153 'RETURN', [qw(CHILDREN)]);
154 $self->assert_mailbox_structure($user1_subs, $sep, {
155 $user1_inbox->to_external('owner') => [ '\\Subscribed' ],
156 (map {(
157 $_->to_external('owner') => [ '\\Subscribed', '\\HasNoChildren' ]
158 )} @user1_mailboxes),
159 });
160
161 xlog("subscribe as $user2 to $user1\'s shared mb's");
162 foreach my $mb ($user1_inbox, @user1_mailboxes) {
163 $user2_talk->subscribe($mb->to_external('other'));
164 $self->assert_equals('ok',
165 $user2_talk->get_last_completion_response());
166 }
167
168 xlog("make sure $user2 has the right subscriptions");
169 my $user2_subs = $user2_talk->list([qw(SUBSCRIBED)],
170 '', '*',
171 'RETURN', [qw(CHILDREN)]);
172 $self->assert_mailbox_structure($user2_subs, $sep, {
173 $user2_inbox->to_external('owner') => [ '\\Subscribed' ],
174 (map {(
175 $_->to_external('owner') => [ '\\Subscribed', '\\HasNoChildren' ]
176 )} @user2_mailboxes),
177 $user1_inbox->to_external('other') => [ '\\Subscribed' ],
178 (map {(
179 $_->to_external('other') => [
180 '\\Subscribed',
181 '\\HasNoChildren',
182 ]
183 )} @user1_mailboxes),
184 });
185
186 xlog("unsub as $user2 from $user1\'s folders");
187 foreach my $mb ($user1_inbox, @user1_mailboxes) {
188 $user2_talk->unsubscribe($mb->to_external('other'));
189 $self->assert_equals('ok',
190 $user2_talk->get_last_completion_response());
191 }
192
193 xlog("make sure $user2 has the right subscriptions");
194 $user2_subs = $user2_talk->list([qw(SUBSCRIBED)],
195 '', '*',
196 'RETURN', [qw(CHILDREN)]);
197 $self->assert_mailbox_structure($user2_subs, $sep, {
198 $user1_inbox->to_external('owner') => [ '\\Subscribed' ],
199 (map {(
200 $_->to_external('owner') => [ '\\Subscribed', '\\HasNoChildren' ]
201 )} @user2_mailboxes),
202 });
203 }
204
205 sub test_subscribe
206 {
207 my ($self) = @_;
208
209 $self->shared_subscribe_common('firstuser', 'seconduser');
210 }
211
212 sub test_subscribe_prefix
213 {
214 my ($self) = @_;
215
216 # one user is a prefix of the other!
217 $self->shared_subscribe_common('chris', 'christopher');
218 }
219
220 sub test_subscribe_vd
221 :VirtDomains :CrossDomains
222 {
223 my ($self) = @_;
224
225 $self->shared_subscribe_common('[email protected]',
226 '[email protected]');
227 }
228
229 sub test_subscribe_vd_prefix
230 :VirtDomains :CrossDomains
231 {
232 my ($self) = @_;
233
234 $self->shared_subscribe_common('[email protected]',
235 '[email protected]');
236 }
237
238 sub test_subscribe_vd_prefix2
239 :VirtDomains :CrossDomains
240 {
241 my ($self) = @_;
242
243 $self->shared_subscribe_common('[email protected]',
244 '[email protected]');
245 }
246
247 1;