| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | |
|---|
| 5 | BEGIN { chdir 't' if -d 't' } |
|---|
| 6 | use lib '../lib'; |
|---|
| 7 | |
|---|
| 8 | use Test::More tests => 26; |
|---|
| 9 | use Test::File::Contents; |
|---|
| 10 | use Test::DatabaseRow; |
|---|
| 11 | use Test::Output; |
|---|
| 12 | use File::Path; |
|---|
| 13 | |
|---|
| 14 | use Email::Send; |
|---|
| 15 | use Email::Send::Test; |
|---|
| 16 | |
|---|
| 17 | use Lancelot::DB; |
|---|
| 18 | use Lancelot::Message; |
|---|
| 19 | use Lancelot::Log qw/init log/; |
|---|
| 20 | |
|---|
| 21 | sub copy_msg { |
|---|
| 22 | return Lancelot::Message->new($_[0]->as_string); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | BEGIN { use_ok('Lancelot::Module::moderate'); } |
|---|
| 26 | |
|---|
| 27 | my $listname = 'pl-testlist@example.com'; |
|---|
| 28 | |
|---|
| 29 | rmtree("$ENV{HOME}/.pl/$listname", 0); |
|---|
| 30 | |
|---|
| 31 | my $db = new Lancelot::DB $listname, { create => 1 }; |
|---|
| 32 | $db->set_config('list.name', 'Pl-testlist'); |
|---|
| 33 | $db->set_config('mail.delimiter', '+'); |
|---|
| 34 | $db->set_config('lancelot.mailer', 'Test'); |
|---|
| 35 | |
|---|
| 36 | $db->add_address('moderator@example.com', { moderator => 1 }); |
|---|
| 37 | $db->add_address('hugo@example.com', {}); |
|---|
| 38 | $db->add_address('susi@example.net', {}); |
|---|
| 39 | |
|---|
| 40 | local $Test::DatabaseRow::dbh = $db->{dbh}; |
|---|
| 41 | |
|---|
| 42 | # Lancelot::Log::init("STDERR:all=debug"); |
|---|
| 43 | |
|---|
| 44 | my $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 45 | To: pl-testlist@example.com |
|---|
| 46 | Subject: Test |
|---|
| 47 | Date: Fri, 15 Jul 15:41:00 2011 +0200 |
|---|
| 48 | Message-ID: modtest-1@example.com |
|---|
| 49 | |
|---|
| 50 | This is a test. |
|---|
| 51 | '); |
|---|
| 52 | |
|---|
| 53 | # Pass through without moderation |
|---|
| 54 | my $tmsg = copy_msg($msg); |
|---|
| 55 | my @result = Lancelot::Module::moderate::execute($db, $tmsg); |
|---|
| 56 | is(scalar(@result), 1, "non-moderated list: result length"); |
|---|
| 57 | is($result[0], "", "non-moderated list: result (empty)"); |
|---|
| 58 | |
|---|
| 59 | # Whole-list moderation |
|---|
| 60 | $db->set_config('mod.enable', 1); |
|---|
| 61 | |
|---|
| 62 | $tmsg = copy_msg($msg); |
|---|
| 63 | @result = Lancelot::Module::moderate::execute($db, $tmsg); |
|---|
| 64 | row_ok(table => 'modmessage', |
|---|
| 65 | where => [ list_id => 1, messageid => 'modtest-1@example.com', |
|---|
| 66 | reason => 'modlist', dispose => 'pending' ], |
|---|
| 67 | label => 'list moderation: simple msg/modmessage entry check'); |
|---|
| 68 | is(scalar(@result), 2, "list moderation: result length"); |
|---|
| 69 | is($result[0], "moderation-modlist", "list moderation: simple msg/template"); |
|---|
| 70 | is_deeply($result[1], {'mod-number' => 1}, "list moderation: simple msg/number"); |
|---|
| 71 | my @emails = Email::Send::Test->emails; |
|---|
| 72 | my $s = $emails[0]->as_string; |
|---|
| 73 | $s =~ s/\(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\)/(DATE)/; |
|---|
| 74 | is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com> |
|---|
| 75 | To: pl-testlist+moderators@example.com |
|---|
| 76 | Subject: Pl-testlist moderation notification |
|---|
| 77 | Reply-To: pl-testlist+moderate@example.com |
|---|
| 78 | |
|---|
| 79 | Please attend to the following messages at your convenience. The |
|---|
| 80 | default action (after 7 days) is "Discard". |
|---|
| 81 | |
|---|
| 82 | Counts for message categories: |
|---|
| 83 | |
|---|
| 84 | Messages posted to a moderated list: 1 |
|---|
| 85 | |
|---|
| 86 | MESSAGES POSTED TO A MODERATED LIST |
|---|
| 87 | |
|---|
| 88 | [ ] DEF: Approve/Reject/Discard all these messages immediately |
|---|
| 89 | |
|---|
| 90 | ------------------------------------------------------------------------ |
|---|
| 91 | [ ] 1: Approve/Reject/Discard <modtest-1@example.com> |
|---|
| 92 | Comment: |
|---|
| 93 | ------------------------------------------------------------------------ |
|---|
| 94 | From: hugo@example.com |
|---|
| 95 | Subj: Test |
|---|
| 96 | Date: Fri, 15 Jul 15:41:00 2011 +0200 (DATE) |
|---|
| 97 | |
|---|
| 98 | This is a test. |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | Thank you! |
|---|
| 102 | |
|---|
| 103 | The Project Lancelot mail processor |
|---|
| 104 | |
|---|
| 105 | ', "list moderation: simple msg/mod notify"); |
|---|
| 106 | Email::Send::Test->clear; |
|---|
| 107 | |
|---|
| 108 | # Send the same message in for moderation again |
|---|
| 109 | $tmsg = copy_msg($msg); |
|---|
| 110 | @result = Lancelot::Module::moderate::execute($db, $tmsg); |
|---|
| 111 | is(scalar(@result), 2, "list moderation: double msg/result length"); |
|---|
| 112 | is($result[0], "moderated-already", "list moderation: double msg/template"); |
|---|
| 113 | is_deeply($result[1], { 'message-id' => 'modtest-1@example.com' }, |
|---|
| 114 | "list moderation: double msg/template args"); |
|---|
| 115 | |
|---|
| 116 | # Check nonsubscriber moderation |
|---|
| 117 | $db->flush_mod_message(1); |
|---|
| 118 | $db->delete_configs('mod.enable'); |
|---|
| 119 | $db->set_config('mod.notifylines', 3); |
|---|
| 120 | my $t2msg = Lancelot::Message->new('From: unsubscribed@example.com |
|---|
| 121 | To: pl-testlist@example.com |
|---|
| 122 | Subject: Test |
|---|
| 123 | Date: Fri, 15 Jul 15:41:00 2011 +0200 |
|---|
| 124 | Message-ID: modtest-2@example.com |
|---|
| 125 | |
|---|
| 126 | This is a test. |
|---|
| 127 | '); |
|---|
| 128 | $t2msg->set_flag('nonsubscriber', 1); |
|---|
| 129 | |
|---|
| 130 | @result = Lancelot::Module::moderate::execute($db, $t2msg); |
|---|
| 131 | row_ok(table => 'modmessage', |
|---|
| 132 | where => [ list_id => 1, messageid => 'modtest-2@example.com', |
|---|
| 133 | reason => 'nonsub', dispose => 'pending' ], |
|---|
| 134 | label => 'nonsub moderation: simple msg/modmessage entry check'); |
|---|
| 135 | is($result[0], "moderation-nonsub", "nonsub moderation: simple msg/template"); |
|---|
| 136 | is($result[1]->{'mod-number'}, 1, "nonsub moderation: simple msg/number"); |
|---|
| 137 | |
|---|
| 138 | # Moderated subscribers |
|---|
| 139 | $db->flush_mod_message(1); |
|---|
| 140 | $db->delete_configs('mod.enable'); |
|---|
| 141 | $db->add_address('fritz@example.com', { moderated => 1 }); |
|---|
| 142 | my $t3msg = Lancelot::Message->new('From: fritz@example.com |
|---|
| 143 | To: pl-testlist@example.com |
|---|
| 144 | Subject: Test |
|---|
| 145 | Date: Fri, 15 Jul 15:41:00 2011 +0200 |
|---|
| 146 | Message-ID: modtest-3@example.com |
|---|
| 147 | |
|---|
| 148 | This is a test. |
|---|
| 149 | Second line. |
|---|
| 150 | Third line. |
|---|
| 151 | Fourth line. |
|---|
| 152 | '); |
|---|
| 153 | |
|---|
| 154 | @result = Lancelot::Module::moderate::execute($db, $t3msg); |
|---|
| 155 | row_ok(table => 'modmessage', |
|---|
| 156 | where => [ list_id => 1, messageid => 'modtest-3@example.com', |
|---|
| 157 | reason => 'modsub', dispose => 'pending' ], |
|---|
| 158 | label => 'sub moderation: simple msg/modmessage entry check'); |
|---|
| 159 | is($result[0], "moderation-modsub", "sub moderation: simple msg/template"); |
|---|
| 160 | is($result[1]->{'mod-number'}, 1, "sub moderation: simple msg/number"); |
|---|
| 161 | @emails = Email::Send::Test->emails; |
|---|
| 162 | $s = $emails[0]->as_string; |
|---|
| 163 | $s =~ s/\(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\)/(DATE)/; |
|---|
| 164 | is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com> |
|---|
| 165 | To: pl-testlist+moderators@example.com |
|---|
| 166 | Subject: Pl-testlist moderation notification |
|---|
| 167 | Reply-To: pl-testlist+moderate@example.com |
|---|
| 168 | |
|---|
| 169 | Please attend to the following messages at your convenience. The |
|---|
| 170 | default action (after 7 days) is "Discard". |
|---|
| 171 | |
|---|
| 172 | Counts for message categories: |
|---|
| 173 | |
|---|
| 174 | Messages posted by "moderated" subscribers: 1 |
|---|
| 175 | |
|---|
| 176 | MESSAGES POSTED BY "MODERATED" SUBSCRIBERS |
|---|
| 177 | |
|---|
| 178 | [ ] DEF: Approve/Reject/Discard all these messages immediately |
|---|
| 179 | |
|---|
| 180 | ------------------------------------------------------------------------ |
|---|
| 181 | [ ] 1: Approve/Reject/Discard <modtest-3@example.com> |
|---|
| 182 | Comment: |
|---|
| 183 | ------------------------------------------------------------------------ |
|---|
| 184 | From: fritz@example.com |
|---|
| 185 | Subj: Test |
|---|
| 186 | Date: Fri, 15 Jul 15:41:00 2011 +0200 (DATE) |
|---|
| 187 | |
|---|
| 188 | This is a test. |
|---|
| 189 | Second line. |
|---|
| 190 | Third line. |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | Thank you! |
|---|
| 194 | |
|---|
| 195 | The Project Lancelot mail processor |
|---|
| 196 | |
|---|
| 197 | ', "sub moderation: simple msg/mod notify"); |
|---|
| 198 | Email::Send::Test->clear; |
|---|
| 199 | |
|---|
| 200 | $db->dispose_mod_message(1, "approve"); |
|---|
| 201 | @result = Lancelot::Module::moderate::execute($db, $t3msg); |
|---|
| 202 | is(scalar(@result), 1, "approved message: result length"); |
|---|
| 203 | is($result[0], "", "approved message: result (empty)"); |
|---|
| 204 | |
|---|
| 205 | $db->flush_mod_message(1); |
|---|
| 206 | chmod 0, "$ENV{HOME}/.pl/$listname/mod"; |
|---|
| 207 | @result = Lancelot::Module::moderate::execute($db, $t3msg); |
|---|
| 208 | is(scalar(@result), 2, "non-storable message: result length"); |
|---|
| 209 | is($result[0], "mod-error", "non-storable message: result/template"); |
|---|
| 210 | is_deeply($result[1], { 'mod-number' => 1, |
|---|
| 211 | 'mod-name' => "$ENV{HOME}/.pl/$listname/mod/1" }, |
|---|
| 212 | "non-storable message: result/parameters"); |
|---|
| 213 | |
|---|
| 214 | $db->flush_mod_message(1); |
|---|
| 215 | chmod 0700, "$ENV{HOME}/.pl/$listname/mod"; |
|---|
| 216 | is(Lancelot::Module::moderate::notify_moderators($db), 0, |
|---|
| 217 | "notify_moderators w/o messages"); |
|---|
| 218 | |
|---|
| 219 | $db->set_config('mod.timeout', 3); |
|---|
| 220 | $db->set_config('mod.disposal', 'approve'); |
|---|
| 221 | my $t5msg = Lancelot::Message->new('From: unsubscribed@example.com |
|---|
| 222 | To: pl-testlist@example.com |
|---|
| 223 | Subject: Test |
|---|
| 224 | Date: Fri, 15 Jul 15:41:00 2011 +0200 |
|---|
| 225 | Message-ID: modtest-5@example.com |
|---|
| 226 | |
|---|
| 227 | This is a test. |
|---|
| 228 | '); |
|---|
| 229 | $t5msg->set_flag('nonsubscriber', 1); |
|---|
| 230 | my $t4msg = Lancelot::Message->new('From: fritz@example.com |
|---|
| 231 | To: pl-testlist@example.com |
|---|
| 232 | Subject: Test |
|---|
| 233 | Date: Fri, 15 Jul 15:41:00 2011 +0200 |
|---|
| 234 | Message-ID: modtest-4@example.com |
|---|
| 235 | '); |
|---|
| 236 | |
|---|
| 237 | Lancelot::Module::moderate::execute($db, $t2msg); |
|---|
| 238 | Lancelot::Module::moderate::execute($db, $t5msg); |
|---|
| 239 | Lancelot::Module::moderate::execute($db, $t4msg); |
|---|
| 240 | @emails = Email::Send::Test->emails; |
|---|
| 241 | $s = $emails[0]->as_string; |
|---|
| 242 | $s =~ s/\(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\)/(DATE)/g; |
|---|
| 243 | is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com> |
|---|
| 244 | To: pl-testlist+moderators@example.com |
|---|
| 245 | Subject: Pl-testlist moderation notification |
|---|
| 246 | Reply-To: pl-testlist+moderate@example.com |
|---|
| 247 | |
|---|
| 248 | Please attend to the following messages at your convenience. The |
|---|
| 249 | default action (after 3 days) is "Approve". |
|---|
| 250 | |
|---|
| 251 | Counts for message categories: |
|---|
| 252 | |
|---|
| 253 | Messages posted by "moderated" subscribers: 1 |
|---|
| 254 | Messages posted by nonsubscribers: 2 |
|---|
| 255 | |
|---|
| 256 | MESSAGES POSTED BY "MODERATED" SUBSCRIBERS |
|---|
| 257 | |
|---|
| 258 | [ ] DEF: Approve/Reject/Discard all these messages immediately |
|---|
| 259 | |
|---|
| 260 | ------------------------------------------------------------------------ |
|---|
| 261 | [ ] 3: Approve/Reject/Discard <modtest-4@example.com> |
|---|
| 262 | Comment: |
|---|
| 263 | ------------------------------------------------------------------------ |
|---|
| 264 | From: fritz@example.com |
|---|
| 265 | Subj: Test |
|---|
| 266 | Date: Fri, 15 Jul 15:41:00 2011 +0200 (DATE) |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | MESSAGES POSTED BY NONSUBSCRIBERS |
|---|
| 270 | |
|---|
| 271 | [ ] DEF: Approve/Reject/Discard all these messages immediately |
|---|
| 272 | |
|---|
| 273 | ------------------------------------------------------------------------ |
|---|
| 274 | [ ] 1: Approve/Reject/Discard <modtest-2@example.com> |
|---|
| 275 | Comment: |
|---|
| 276 | ------------------------------------------------------------------------ |
|---|
| 277 | From: unsubscribed@example.com |
|---|
| 278 | Subj: Test |
|---|
| 279 | Date: Fri, 15 Jul 15:41:00 2011 +0200 (DATE) |
|---|
| 280 | |
|---|
| 281 | This is a test. |
|---|
| 282 | ------------------------------------------------------------------------ |
|---|
| 283 | [ ] 2: Approve/Reject/Discard <modtest-5@example.com> |
|---|
| 284 | Comment: |
|---|
| 285 | ------------------------------------------------------------------------ |
|---|
| 286 | From: unsubscribed@example.com |
|---|
| 287 | Subj: Test |
|---|
| 288 | Date: Fri, 15 Jul 15:41:00 2011 +0200 (DATE) |
|---|
| 289 | |
|---|
| 290 | This is a test. |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | Thank you! |
|---|
| 294 | |
|---|
| 295 | The Project Lancelot mail processor |
|---|
| 296 | |
|---|
| 297 | ', "several msgs: mod notify"); |
|---|
| 298 | Email::Send::Test->clear; |
|---|
| 299 | |
|---|
| 300 | Lancelot::Log::init("STDERR:all=err"); |
|---|
| 301 | unlink "$ENV{HOME}/.pl/$listname/mod/2"; |
|---|
| 302 | stderr_like(sub { Lancelot::Module::moderate::notify_moderators($db) }, |
|---|
| 303 | qr|^30moderate.t\[\d+\]: module: mod message $ENV{HOME}/.pl/$listname/mod/2 seems to have disappeared\n|, |
|---|
| 304 | "notify_moderators w/lost message"); |
|---|