| 1 | |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | BEGIN { chdir 't' if -d 't' } |
|---|
| 5 | use lib '../lib'; |
|---|
| 6 | |
|---|
| 7 | use Test::More tests => 32; |
|---|
| 8 | use File::Path; |
|---|
| 9 | use File::Basename; |
|---|
| 10 | use Test::File; |
|---|
| 11 | use Test::File::Contents; |
|---|
| 12 | use Test::Output; |
|---|
| 13 | |
|---|
| 14 | use Lancelot::DB; |
|---|
| 15 | use Lancelot::Template; |
|---|
| 16 | use Lancelot::Message; |
|---|
| 17 | use Lancelot::Log; |
|---|
| 18 | |
|---|
| 19 | sub copy_msg { |
|---|
| 20 | return Lancelot::Message->new($_[0]->as_string); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | BEGIN { use_ok('Lancelot::Module::archive_store'); } |
|---|
| 24 | |
|---|
| 25 | my ($listname) = 'pl-testlist@example.com'; |
|---|
| 26 | |
|---|
| 27 | rmtree("$ENV{HOME}/.pl/$listname", 0); |
|---|
| 28 | |
|---|
| 29 | my $db = new Lancelot::DB $listname, { create => 1 }; |
|---|
| 30 | $db->set_config('list.name', 'Pl-testlist'); |
|---|
| 31 | |
|---|
| 32 | my $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 33 | To: pl-testlist@example.net |
|---|
| 34 | Subject: Test |
|---|
| 35 | |
|---|
| 36 | This is a test. |
|---|
| 37 | '); |
|---|
| 38 | |
|---|
| 39 | # Don't archive messages from non-subscribers. |
|---|
| 40 | |
|---|
| 41 | $db->set_config('mail.policy.subscribersonly', 1); |
|---|
| 42 | $msg->set_flag('nonsubscriber', 1); |
|---|
| 43 | my $result; |
|---|
| 44 | Lancelot::Log::init ""; |
|---|
| 45 | stderr_like(sub { $result=Lancelot::Module::archive_store::execute($db, $msg);}, |
|---|
| 46 | qr/not archiving message because of subscriber-only policy/, |
|---|
| 47 | "nonsubscriber archive_store STDERR output"); |
|---|
| 48 | is($result, "", "nonsubscriber archive_store result OK"); |
|---|
| 49 | ok(! -e "$ENV{HOME}/.pl/$listname/archive", "no archive directory created"); |
|---|
| 50 | Lancelot::Log::init "NONE:"; |
|---|
| 51 | |
|---|
| 52 | # Basic archive test with defaults |
|---|
| 53 | |
|---|
| 54 | $db->set_config('mail.policy.subscribersonly', 1); |
|---|
| 55 | |
|---|
| 56 | $db->delete_configs('archive.directory'); # should be "archive" |
|---|
| 57 | $db->delete_configs('archive.dirmode'); # should be 0755 |
|---|
| 58 | $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 59 | To: pl-testlist@example.net |
|---|
| 60 | Subject: Test |
|---|
| 61 | |
|---|
| 62 | This is a test. |
|---|
| 63 | '); |
|---|
| 64 | $msg->set_flag("number", $db->increment_parameter("message")); |
|---|
| 65 | $msg->set_flag('nonsubscriber', 0); |
|---|
| 66 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 67 | is($result, "", "subscriber archive_store result OK"); |
|---|
| 68 | my $adir = "$ENV{HOME}/.pl/$listname/archive"; |
|---|
| 69 | ok(-d $adir, "archive directory created"); |
|---|
| 70 | file_mode_is($adir, 0755, "archive directory mode is 0755"); |
|---|
| 71 | my $msgn = $msg->get_flag("number"); |
|---|
| 72 | my $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100; |
|---|
| 73 | file_exists_ok($fname, "archive file created"); |
|---|
| 74 | file_readable_ok($fname, "archive file is readable"); |
|---|
| 75 | file_contents_is($fname, 'From: hugo@example.com |
|---|
| 76 | To: pl-testlist@example.net |
|---|
| 77 | Subject: Test |
|---|
| 78 | |
|---|
| 79 | This is a test. |
|---|
| 80 | ', "archive file correct contents"); |
|---|
| 81 | |
|---|
| 82 | # Set a message number if necessary |
|---|
| 83 | |
|---|
| 84 | $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 85 | To: pl-testlist@example.net |
|---|
| 86 | Subject: Test |
|---|
| 87 | |
|---|
| 88 | This is a test. |
|---|
| 89 | '); |
|---|
| 90 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 91 | is($msg->get_flag("number"), $msgn + 1, "message number was incremented"); |
|---|
| 92 | $msgn = $msg->get_flag("number"); |
|---|
| 93 | $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100; |
|---|
| 94 | file_exists_ok($fname, "second archive file created"); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | # different archive.directory |
|---|
| 98 | |
|---|
| 99 | $db->set_config('archive.directory', 'other-archive'); |
|---|
| 100 | $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 101 | To: pl-testlist@example.net |
|---|
| 102 | Subject: Test |
|---|
| 103 | |
|---|
| 104 | This is a test. |
|---|
| 105 | '); |
|---|
| 106 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 107 | is($result, "", "archive.directory test result"); |
|---|
| 108 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 109 | ok(-d $adir, "archive directory created"); |
|---|
| 110 | file_mode_is($adir, 0755, "archive directory mode is 0755"); |
|---|
| 111 | $msgn = $msg->get_flag("number"); |
|---|
| 112 | $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100; |
|---|
| 113 | file_exists_ok($fname, "archive.directory test file created"); |
|---|
| 114 | |
|---|
| 115 | # different archive.dirmode |
|---|
| 116 | rmtree($adir, 0); |
|---|
| 117 | $db->set_config('archive.dirmode', '488'); # 0750 |
|---|
| 118 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 119 | is($result, "", "archive.dirmode test result"); |
|---|
| 120 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 121 | ok(-d $adir, "archive directory created"); |
|---|
| 122 | file_mode_is($adir, 0750, "archive directory mode is 0750"); |
|---|
| 123 | |
|---|
| 124 | # different archive.dirmode in octal |
|---|
| 125 | rmtree($adir, 0); |
|---|
| 126 | $db->set_config('archive.dirmode', '0700'); |
|---|
| 127 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 128 | is($result, "", "octal archive.dirmode test result"); |
|---|
| 129 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 130 | ok(-d $adir, "archive directory created"); |
|---|
| 131 | file_mode_is($adir, 0700, "archive directory mode is 0700"); |
|---|
| 132 | |
|---|
| 133 | # absolute pathname for archive directory |
|---|
| 134 | rmtree($adir, 0); |
|---|
| 135 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 136 | $db->set_config('archive.directory', $adir); |
|---|
| 137 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 138 | is($result, "", "absolute pathname archive.directory test result"); |
|---|
| 139 | ok(-d $adir, "archive directory created"); |
|---|
| 140 | |
|---|
| 141 | # archive directory impossible to create |
|---|
| 142 | rmtree($adir, 0); |
|---|
| 143 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 144 | chmod 0111, dirname($adir); |
|---|
| 145 | $db->set_config('archive.directory', $adir); |
|---|
| 146 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 147 | is($result, "", "uncreatable archive.directory test result"); |
|---|
| 148 | chmod 0755, dirname($adir); |
|---|
| 149 | file_not_exists_ok($adir, "archive directory not created"); |
|---|
| 150 | |
|---|
| 151 | # archive file impossible to create |
|---|
| 152 | rmtree($adir, 0); |
|---|
| 153 | $adir = "$ENV{HOME}/.pl/$listname/other-archive"; |
|---|
| 154 | mkpath "$adir/0", 0; |
|---|
| 155 | chmod 0111, "$adir/0"; |
|---|
| 156 | $db->set_config('archive.directory', $adir); |
|---|
| 157 | $result = Lancelot::Module::archive_store::execute($db, $msg); |
|---|
| 158 | is($result, "", "uncreatable archive file test result"); |
|---|
| 159 | $msgn = $msg->get_flag("number"); |
|---|
| 160 | $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100; |
|---|
| 161 | chmod 0755, "$adir/0"; |
|---|
| 162 | file_not_exists_ok($fname, |
|---|
| 163 | "unwritable archive.directory test file not created"); |
|---|
| 164 | |
|---|
| 165 | # archive mail from subscriber |
|---|
| 166 | rmtree($adir, 0); |
|---|
| 167 | $db->set_config('mail.policy.subscribersonly', 0); |
|---|
| 168 | foreach my $nonsub (0, 1) { |
|---|
| 169 | my $msgc = copy_msg($msg); |
|---|
| 170 | $msgc->set_flag('nonsubscriber', $nonsub); |
|---|
| 171 | $result = Lancelot::Module::archive_store::execute($db, $msgc); |
|---|
| 172 | is($result, "", "nonsubscriber archive_store result OK ($nonsub)"); |
|---|
| 173 | $msgn = $msgc->get_flag("number"); |
|---|
| 174 | $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100; |
|---|
| 175 | file_exists_ok($fname, "nonsubscriber archive file created ($nonsub)"); |
|---|
| 176 | } |
|---|