root/t/30moderate.t

Revision 303:6f57b7328cb8, 9.6 KB (checked in by Anselm Lingnau <anselm@…>, 9 months ago)

The Great Renaming: Change all command names to 'pl-*' and directory to '.pl'.
The idea behind this is to emphasise that the software is called ?*Project*
Lancelot? as opposed to plain ?Lancelot?. There is apparently a KDE tool that
goes by the name of ?Lancelot?, and we want to make clear that this is separate
and different. Also, Project Lancelot has been around for a lot longer!

Line 
1#!/usr/bin/perl -w
2
3use strict;
4
5BEGIN { chdir 't' if -d 't' }
6use lib '../lib';
7
8use Test::More tests => 26;
9use Test::File::Contents;
10use Test::DatabaseRow;
11use Test::Output;
12use File::Path;
13
14use Email::Send;
15use Email::Send::Test;
16
17use Lancelot::DB;
18use Lancelot::Message;
19use Lancelot::Log qw/init log/;
20
21sub copy_msg {
22    return Lancelot::Message->new($_[0]->as_string);
23}
24
25BEGIN { use_ok('Lancelot::Module::moderate'); }
26
27my $listname = 'pl-testlist@example.com';
28
29rmtree("$ENV{HOME}/.pl/$listname", 0);
30
31my $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
40local $Test::DatabaseRow::dbh = $db->{dbh};
41
42# Lancelot::Log::init("STDERR:all=debug");
43
44my $msg = Lancelot::Message->new('From: hugo@example.com
45To: pl-testlist@example.com
46Subject: Test
47Date: Fri, 15 Jul 15:41:00 2011 +0200
48Message-ID: modtest-1@example.com
49
50This is a test.
51');
52
53# Pass through without moderation
54my $tmsg = copy_msg($msg);
55my @result = Lancelot::Module::moderate::execute($db, $tmsg);
56is(scalar(@result), 1, "non-moderated list: result length");
57is($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);
64row_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');
68is(scalar(@result), 2, "list moderation: result length");
69is($result[0], "moderation-modlist", "list moderation: simple msg/template");
70is_deeply($result[1], {'mod-number' => 1}, "list moderation: simple msg/number");
71my @emails = Email::Send::Test->emails;
72my $s = $emails[0]->as_string;
73$s =~ s/\(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\)/(DATE)/;
74is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com>
75To: pl-testlist+moderators@example.com
76Subject: Pl-testlist moderation notification
77Reply-To: pl-testlist+moderate@example.com
78
79Please attend to the following messages at your convenience. The
80default action (after 7 days) is "Discard".
81
82Counts for message categories:
83
84               Messages posted to a moderated list: 1
85
86MESSAGES 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
101Thank you!
102
103The Project Lancelot mail processor
104
105', "list moderation: simple msg/mod notify");
106Email::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);
111is(scalar(@result), 2, "list moderation: double msg/result length");
112is($result[0], "moderated-already", "list moderation: double msg/template");
113is_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);
120my $t2msg = Lancelot::Message->new('From: unsubscribed@example.com
121To: pl-testlist@example.com
122Subject: Test
123Date: Fri, 15 Jul 15:41:00 2011 +0200
124Message-ID: modtest-2@example.com
125
126This is a test.
127');
128$t2msg->set_flag('nonsubscriber', 1);
129
130@result = Lancelot::Module::moderate::execute($db, $t2msg);
131row_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');
135is($result[0], "moderation-nonsub", "nonsub moderation: simple msg/template");
136is($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 });
142my $t3msg = Lancelot::Message->new('From: fritz@example.com
143To: pl-testlist@example.com
144Subject: Test
145Date: Fri, 15 Jul 15:41:00 2011 +0200
146Message-ID: modtest-3@example.com
147
148This is a test.
149Second line.
150Third line.
151Fourth line.
152');
153
154@result = Lancelot::Module::moderate::execute($db, $t3msg);
155row_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');
159is($result[0], "moderation-modsub", "sub moderation: simple msg/template");
160is($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)/;
164is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com>
165To: pl-testlist+moderators@example.com
166Subject: Pl-testlist moderation notification
167Reply-To: pl-testlist+moderate@example.com
168
169Please attend to the following messages at your convenience. The
170default action (after 7 days) is "Discard".
171
172Counts for message categories:
173
174        Messages posted by "moderated" subscribers: 1
175
176MESSAGES 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
193Thank you!
194
195The Project Lancelot mail processor
196
197', "sub moderation: simple msg/mod notify");
198Email::Send::Test->clear;
199
200$db->dispose_mod_message(1, "approve");
201@result = Lancelot::Module::moderate::execute($db, $t3msg);
202is(scalar(@result), 1, "approved message: result length");
203is($result[0], "", "approved message: result (empty)");
204
205$db->flush_mod_message(1);
206chmod 0, "$ENV{HOME}/.pl/$listname/mod";
207@result = Lancelot::Module::moderate::execute($db, $t3msg);
208is(scalar(@result), 2, "non-storable message: result length");
209is($result[0], "mod-error", "non-storable message: result/template");
210is_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);
215chmod 0700, "$ENV{HOME}/.pl/$listname/mod";
216is(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');
221my $t5msg = Lancelot::Message->new('From: unsubscribed@example.com
222To: pl-testlist@example.com
223Subject: Test
224Date: Fri, 15 Jul 15:41:00 2011 +0200
225Message-ID: modtest-5@example.com
226
227This is a test.
228');
229$t5msg->set_flag('nonsubscriber', 1);
230my $t4msg = Lancelot::Message->new('From: fritz@example.com
231To: pl-testlist@example.com
232Subject: Test
233Date: Fri, 15 Jul 15:41:00 2011 +0200
234Message-ID: modtest-4@example.com
235');
236
237Lancelot::Module::moderate::execute($db, $t2msg);
238Lancelot::Module::moderate::execute($db, $t5msg);
239Lancelot::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;
243is($s, 'From: Pl-testlist Management <pl-testlist+owner@example.com>
244To: pl-testlist+moderators@example.com
245Subject: Pl-testlist moderation notification
246Reply-To: pl-testlist+moderate@example.com
247
248Please attend to the following messages at your convenience. The
249default action (after 3 days) is "Approve".
250
251Counts for message categories:
252
253        Messages posted by "moderated" subscribers: 1
254                 Messages posted by nonsubscribers: 2
255
256MESSAGES 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
269MESSAGES 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
293Thank you!
294
295The Project Lancelot mail processor
296
297', "several msgs: mod notify");
298Email::Send::Test->clear;
299
300Lancelot::Log::init("STDERR:all=err");
301unlink "$ENV{HOME}/.pl/$listname/mod/2";
302stderr_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");
Note: See TracBrowser for help on using the browser.