| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-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 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::Autocreate; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use Cwd qw(getcwd); |
| 44 |
use Data::Dumper; |
| 45 |
use File::Temp qw(tempdir); |
| 46 |
|
| 47 |
use lib '.'; |
| 48 |
use base qw(Cassandane::Cyrus::TestCase); |
| 49 |
use Cassandane::Util::Log; |
| 50 |
|
| 51 |
sub new |
| 52 |
{ |
| 53 |
my $class = shift; |
| 54 |
my $config = Cassandane::Config->default()->clone(); |
| 55 |
|
| 56 |
$config->set( |
| 57 |
autocreate_post => 'yes', |
| 58 |
autocreate_quota => '500000', |
| 59 |
autocreate_inbox_folders => 'Drafts|Sent|Trash|SPAM|plus', |
| 60 |
autocreate_subscribe_folder => 'Drafts|Sent|Trash|SPAM|plus', |
| 61 |
autocreate_sieve_script => '@basedir@/conf/foo_sieve.script', |
| 62 |
autocreate_acl => 'plus anyone p', |
| 63 |
'xlist-drafts' => 'Drafts', |
| 64 |
'xlist-junk' => 'SPAM', |
| 65 |
'xlist-sent' => 'Sent', |
| 66 |
'xlist-trash' => 'Trash', |
| 67 |
); |
| 68 |
my $self = $class->SUPER::new({ |
| 69 |
config => $config, |
| 70 |
adminstore => 1, |
| 71 |
deliver => 1, |
| 72 |
}, @_); |
| 73 |
|
| 74 |
$self->needs('component', 'autocreate'); |
| 75 |
return $self; |
| 76 |
} |
| 77 |
|
| 78 |
sub set_up |
| 79 |
{ |
| 80 |
my ($self) = @_; |
| 81 |
$self->SUPER::set_up(); |
| 82 |
} |
| 83 |
|
| 84 |
sub tear_down |
| 85 |
{ |
| 86 |
my ($self) = @_; |
| 87 |
$self->SUPER::tear_down(); |
| 88 |
} |
| 89 |
|
| 90 |
sub test_autocreate_specialuse |
| 91 |
:min_version_3_0 :NoAltNameSpace |
| 92 |
{ |
| 93 |
my ($self) = @_; |
| 94 |
|
| 95 |
my $svc = $self->{instance}->get_service('imap'); |
| 96 |
my $store = $svc->create_store(username => 'foo'); |
| 97 |
my $talk = $store->get_client(); |
| 98 |
my $list = $talk->list('', '*', 'return', ['special-use']); |
| 99 |
|
| 100 |
my %map = ( |
| 101 |
drafts => 'Drafts', |
| 102 |
junk => 'SPAM', |
| 103 |
sent => 'Sent', |
| 104 |
trash => 'Trash', |
| 105 |
); |
| 106 |
foreach my $item (@$list) { |
| 107 |
my $key; |
| 108 |
foreach my $flag (@{$item->[0]}) { |
| 109 |
next unless $flag =~ m/\\(.*)/; |
| 110 |
$key = $1; |
| 111 |
last if $map{$key}; |
| 112 |
} |
| 113 |
my $name = delete $map{$key}; |
| 114 |
next unless $name; |
| 115 |
$self->assert_str_equals("INBOX.$name", $item->[2]); |
| 116 |
} |
| 117 |
$self->assert_num_equals(0, scalar keys %map); |
| 118 |
} |
| 119 |
|
| 120 |
sub test_autocreate_sieve_script_generation |
| 121 |
:min_version_3_0 :needs_component_sieve |
| 122 |
{ |
| 123 |
my ($self) = @_; |
| 124 |
|
| 125 |
my $basedir = $self->{instance}->get_basedir(); |
| 126 |
my $sieve_script_path = $basedir . "/conf/foo_sieve.script"; |
| 127 |
my $hitfolder = "INBOX.NewFolder"; |
| 128 |
my $testfolder = "INBOX.TestFolder"; |
| 129 |
my $missfolder = "INBOX"; |
| 130 |
|
| 131 |
open(FH, '>', "$sieve_script_path") |
| 132 |
or die "Cannot open $sieve_script_path for writing: $!"; |
| 133 |
print FH "require \[\"fileinto\", \"mailbox\"\];"; |
| 134 |
print FH "if mailboxexists \"$testfolder\" {"; |
| 135 |
print FH "fileinto \"$hitfolder\";"; |
| 136 |
print FH "}"; |
| 137 |
close(FH); |
| 138 |
|
| 139 |
my $svc = $self->{instance}->get_service('imap'); |
| 140 |
my $store = $svc->create_store(username => 'foo'); |
| 141 |
my $talk = $store->get_client(); |
| 142 |
|
| 143 |
my $sievedir = $self->{instance}->get_sieve_script_dir('foo'); |
| 144 |
$self->assert_file_test("$sievedir/foo_sieve.script.script", '-f'); |
| 145 |
$self->assert_file_test("$sievedir/defaultbc", '-f'); |
| 146 |
$self->assert_file_test("$sievedir/foo_sieve.script.bc", '-f'); |
| 147 |
} |
| 148 |
|
| 149 |
sub test_autocreate_acl |
| 150 |
:min_version_3_1 :needs_component_sieve :NoAltNameSpace |
| 151 |
{ |
| 152 |
my ($self) = @_; |
| 153 |
|
| 154 |
my %folder_acls = ( |
| 155 |
'INBOX' => [qw( foo lrswipkxtecdan )], |
| 156 |
'INBOX.Drafts' => [qw( foo lrswipkxtecdan )], |
| 157 |
'INBOX.Sent' => [qw( foo lrswipkxtecdan )], |
| 158 |
'INBOX.SPAM' => [qw( foo lrswipkxtecdan )], |
| 159 |
'INBOX.Trash' => [qw( foo lrswipkxtecdan )], |
| 160 |
'INBOX.plus' => [qw( foo lrswipkxtecdan anyone p )], |
| 161 |
); |
| 162 |
|
| 163 |
my $svc = $self->{instance}->get_service('imap'); |
| 164 |
my $store = $svc->create_store(username => 'foo'); |
| 165 |
my $talk = $store->get_client(); |
| 166 |
|
| 167 |
while (my ($folder, $acl) = each %folder_acls) { |
| 168 |
my $res = $talk->getacl($folder); |
| 169 |
$self->assert_deep_equals($folder_acls{$folder}, $res); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
sub test_legacymb_already_exists |
| 174 |
:NoStartInstances :NoAltNamespace |
| 175 |
{ |
| 176 |
my ($self) = @_; |
| 177 |
|
| 178 |
# want a separate IMAP service with separate config containing |
| 179 |
# the defaults (no autocreate!) plus mailbox_legacy_dirs: yes |
| 180 |
my $leg_conf = Cassandane::Config->default()->clone(); |
| 181 |
$leg_conf->set(mailbox_legacy_dirs => 'yes'); |
| 182 |
|
| 183 |
my $leg_svc = $self->{instance}->add_service( |
| 184 |
name => 'imaplegacymb', |
| 185 |
config => $leg_conf, |
| 186 |
); |
| 187 |
|
| 188 |
# now actually start everything |
| 189 |
$self->_start_instances(); |
| 190 |
|
| 191 |
# create some mailboxes for user foo under legacy storage |
| 192 |
my $leg_store = $leg_svc->create_store(username => 'admin', |
| 193 |
type => 'imap'); |
| 194 |
my $leg_talk = $leg_store->get_client(); |
| 195 |
|
| 196 |
$leg_talk->create('user.foo') or die; |
| 197 |
$leg_talk->setacl('user.foo', foo => 'lrswipkxtecdn') or die; |
| 198 |
$leg_talk->create('user.foo.bar') or die; |
| 199 |
$leg_talk->setacl('user.foo.bar', foo => 'lrswipkxtecdn') or die; |
| 200 |
|
| 201 |
$leg_talk->logout(); |
| 202 |
|
| 203 |
# those mailboxes had better be under legacy storage |
| 204 |
foreach my $mailbox (qw(user.foo user.foo.bar)) { |
| 205 |
my $mbpath = $self->{instance}->run_mbpath($mailbox); |
| 206 |
$self->assert_does_not_match(qr{/uuid/}, $mbpath->{data}); |
| 207 |
} |
| 208 |
|
| 209 |
# now log in as user foo -- better not get the default |
| 210 |
# autocreate set! |
| 211 |
|
| 212 |
my $svc = $self->{instance}->get_service('imap'); |
| 213 |
my $store = $svc->create_store(username => 'foo'); |
| 214 |
my $talk = $store->get_client(); |
| 215 |
|
| 216 |
my $data = $talk->list("", "*"); |
| 217 |
|
| 218 |
$self->assert_mailbox_structure($data, '.', { |
| 219 |
'INBOX' => '\\HasChildren', |
| 220 |
'INBOX.bar' => '\\HasNoChildren', |
| 221 |
}); |
| 222 |
} |
| 223 |
|
| 224 |
1; |