Debian Codegraph @

cassandane/Cassandane/Cyrus/RelocateById.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2022-2022 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::RelocateById;
41 use strict;
42 use warnings;
43 use Data::Dumper;
44
45 use lib '.';
46 use base qw(Cassandane::Cyrus::TestCase);
47 use Cassandane::Util::Log;
48 use Cassandane::Util::Slurp;
49
50 sub new
51 {
52 my ($class, @args) = @_;
53 my $config = Cassandane::Config->default()->clone();
54 $config->set(
55 conversations => 'yes',
56 mailbox_legacy_dirs => 'yes',
57 delete_mode => 'delayed',
58 unixhierarchysep => 'no', # XXX need a :NoUnixHierarchySep
59 );
60 return $class->SUPER::new({
61 config => $config,
62 adminstore => 1,
63 }, @args);
64 }
65
66 sub set_up
67 {
68 my ($self) = @_;
69 $self->SUPER::set_up();
70 }
71
72 sub tear_down
73 {
74 my ($self) = @_;
75 $self->SUPER::tear_down();
76 }
77
78 sub test_user_nomatch
79 :min_version_3_6
80 {
81 my ($self) = @_;
82
83 my $errfile = $self->{instance}->get_basedir() . '/relocate.err';
84
85 my $user = 'nonexistent';
86
87 $self->{instance}->run_command({
88 cyrus => 1,
89 redirects => {
90 stderr => $errfile,
91 },
92 }, 'relocate_by_id', '-u', $user);
93
94 my $output = slurp_file($errfile);
95
96 # better complain if the user requested doesn't exist!
97 $self->assert_matches(qr{$user: user not found}, $output);
98 }
99
100 sub test_mailbox_nomatch
101 :min_version_3_6
102 {
103 my ($self) = @_;
104
105 my $errfile = $self->{instance}->get_basedir() . '/relocate.err';
106
107 my $mailbox = 'user.nonexistent';
108
109 $self->{instance}->run_command({
110 cyrus => 1,
111 redirects => {
112 stderr => $errfile,
113 },
114 }, 'relocate_by_id', $mailbox);
115
116 my $output = slurp_file($errfile);
117
118 # better complain if the mailbox requested doesn't exist!
119 $self->assert_matches(qr{$mailbox: mailbox not found}, $output);
120 }
121
122 sub test_mailbox_inbox_domain
123 :min_version_3_6 :NoAltNamespace :VirtDomains
124 {
125 my ($self) = @_;
126
127 my $adminstore = $self->{adminstore};
128 my $admintalk = $adminstore->get_client();
129
130 my $inbox = "user.magicuser\@example.com";
131 my $subfolder = "user.magicuser.foo\@example.com";
132
133 $admintalk->create($inbox);
134 $admintalk->setacl($inbox, admin => 'lrswipkxtecdan');
135 $admintalk->create($subfolder);
136 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
137
138 $adminstore->set_folder($subfolder);
139 $self->make_message("Email", store => $adminstore) or die;
140
141 # Create the search database.
142 xlog $self, "Run squatter";
143 $self->{instance}->run_command({cyrus => 1}, 'squatter');
144
145 my $basedir = $self->{instance}{basedir};
146 open(FH, "-|", "find", $basedir);
147 my @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
148 close(FH);
149
150 xlog $self, "files exist";
151 $self->assert_not_equals(0, scalar @files);
152
153 $self->{instance}->run_command({ cyrus => 1 },
154 'relocate_by_id', "user.magicuser\@example.com" );
155
156 open(FH, "-|", "find", $basedir);
157 @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
158 close(FH);
159
160 xlog $self, "no files left for this user";
161 $self->assert_equals(0, scalar @files);
162 }
163
164 sub test_mailbox_inbox_nodomain
165 :min_version_3_6
166 {
167 my ($self) = @_;
168
169 my $adminstore = $self->{adminstore};
170 my $admintalk = $adminstore->get_client();
171
172 my $inbox = "user.magicuser";
173 my $subfolder = "user.magicuser.foo";
174
175 $admintalk->create($inbox);
176 $admintalk->setacl($inbox, admin => 'lrswipkxtecdan');
177 $admintalk->create($subfolder);
178 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
179
180 $adminstore->set_folder($subfolder);
181 $self->make_message("Email", store => $adminstore) or die;
182
183 # Create the search database.
184 xlog $self, "Run squatter";
185 $self->{instance}->run_command({cyrus => 1}, 'squatter');
186
187 my $basedir = $self->{instance}{basedir};
188 open(FH, "-|", "find", $basedir);
189 my @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
190 close(FH);
191
192 xlog $self, "files exist";
193 $self->assert_not_equals(0, scalar @files);
194
195 $self->{instance}->run_command({ cyrus => 1 },
196 'relocate_by_id', "user.magicuser" );
197
198 open(FH, "-|", "find", $basedir);
199 @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
200 close(FH);
201
202 xlog $self, "no files left for this user";
203 $self->assert_equals(0, scalar @files);
204 }
205
206 sub test_mailbox_shared_domain
207 :min_version_3_6 :NoAltNamespace :VirtDomains
208 {
209 my ($self) = @_;
210
211 my $adminstore = $self->{adminstore};
212 my $admintalk = $adminstore->get_client();
213
214 my $mbox = "shared.magic\@example.com";
215 my $subfolder = "shared.magic.foo\@example.com";
216
217 $admintalk->create($mbox);
218 $admintalk->setacl($mbox, admin => 'lrswipkxtecdan');
219 $admintalk->create($subfolder);
220 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
221
222 $adminstore->set_folder($subfolder);
223 $self->make_message("Email", store => $adminstore) or die;
224
225 # Create the search database.
226 xlog $self, "Run squatter";
227 $self->{instance}->run_command({cyrus => 1}, 'squatter');
228
229 my $basedir = $self->{instance}{basedir};
230 open(FH, "-|", "find", $basedir);
231 my @files = grep { m{/magic/} and not m{/conf/lock/} } <FH>;
232 close(FH);
233
234 xlog $self, "files exist";
235 $self->assert_not_equals(0, scalar @files);
236
237 $self->{instance}->run_command({ cyrus => 1 },
238 'relocate_by_id', $mbox );
239
240 open(FH, "-|", "find", $basedir);
241 @files = grep { m{/magic/} and not m{/conf/lock/} } <FH>;
242 close(FH);
243
244 xlog $self, "no files left for this hierarchy";
245 $self->assert_equals(0, scalar @files);
246 }
247
248 sub test_mailbox_shared_nodomain
249 :min_version_3_6
250 {
251 my ($self) = @_;
252
253 my $adminstore = $self->{adminstore};
254 my $admintalk = $adminstore->get_client();
255
256 my $mbox = "shared.magic";
257 my $subfolder = "shared.magic.foo";
258
259 $admintalk->create($mbox);
260 $admintalk->setacl($mbox, admin => 'lrswipkxtecdan');
261 $admintalk->create($subfolder);
262 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
263
264 $adminstore->set_folder($subfolder);
265 $self->make_message("Email", store => $adminstore) or die;
266
267 # Create the search database.
268 xlog $self, "Run squatter";
269 $self->{instance}->run_command({cyrus => 1}, 'squatter');
270
271 my $basedir = $self->{instance}{basedir};
272 open(FH, "-|", "find", $basedir);
273 my @files = grep { m{/magic/} and not m{/conf/lock/} } <FH>;
274 close(FH);
275
276 xlog $self, "files exist";
277 $self->assert_not_equals(0, scalar @files);
278
279 $self->{instance}->run_command({ cyrus => 1 },
280 'relocate_by_id', $mbox );
281
282 open(FH, "-|", "find", $basedir);
283 @files = grep { m{/magic/} and not m{/conf/lock/} } <FH>;
284 close(FH);
285
286 xlog $self, "no files left for this user";
287 $self->assert_equals(0, scalar @files);
288 }
289
290 1;