root/t/20archive-store.t

Revision 303:6f57b7328cb8, 5.7 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
2use strict;
3
4BEGIN { chdir 't' if -d 't' }
5use lib '../lib';
6
7use Test::More tests => 32;
8use File::Path;
9use File::Basename;
10use Test::File;
11use Test::File::Contents;
12use Test::Output;
13
14use Lancelot::DB;
15use Lancelot::Template;
16use Lancelot::Message;
17use Lancelot::Log;
18
19sub copy_msg {
20    return Lancelot::Message->new($_[0]->as_string);
21}
22
23BEGIN { use_ok('Lancelot::Module::archive_store'); }
24
25my ($listname) = 'pl-testlist@example.com';
26
27rmtree("$ENV{HOME}/.pl/$listname", 0);
28
29my $db = new Lancelot::DB $listname, { create => 1 };
30$db->set_config('list.name', 'Pl-testlist');
31
32my $msg = Lancelot::Message->new('From: hugo@example.com
33To: pl-testlist@example.net
34Subject: Test
35
36This 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);
43my $result;
44Lancelot::Log::init "";
45stderr_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");
48is($result, "", "nonsubscriber archive_store result OK");
49ok(! -e "$ENV{HOME}/.pl/$listname/archive", "no archive directory created");
50Lancelot::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
59To: pl-testlist@example.net
60Subject: Test
61
62This 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);
67is($result, "", "subscriber archive_store result OK");
68my $adir = "$ENV{HOME}/.pl/$listname/archive";
69ok(-d $adir, "archive directory created");
70file_mode_is($adir, 0755, "archive directory mode is 0755");
71my $msgn = $msg->get_flag("number");
72my $fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100;
73file_exists_ok($fname, "archive file created");
74file_readable_ok($fname, "archive file is readable");
75file_contents_is($fname, 'From: hugo@example.com
76To: pl-testlist@example.net
77Subject: Test
78
79This 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
85To: pl-testlist@example.net
86Subject: Test
87
88This is a test.
89');
90$result = Lancelot::Module::archive_store::execute($db, $msg);
91is($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;
94file_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
101To: pl-testlist@example.net
102Subject: Test
103
104This is a test.
105');
106$result = Lancelot::Module::archive_store::execute($db, $msg);
107is($result, "", "archive.directory test result");
108$adir = "$ENV{HOME}/.pl/$listname/other-archive";
109ok(-d $adir, "archive directory created");
110file_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;
113file_exists_ok($fname, "archive.directory test file created");
114
115# different archive.dirmode
116rmtree($adir, 0);
117$db->set_config('archive.dirmode', '488'); # 0750
118$result = Lancelot::Module::archive_store::execute($db, $msg);
119is($result, "", "archive.dirmode test result");
120$adir = "$ENV{HOME}/.pl/$listname/other-archive";
121ok(-d $adir, "archive directory created");
122file_mode_is($adir, 0750, "archive directory mode is 0750");
123
124# different archive.dirmode in octal
125rmtree($adir, 0);
126$db->set_config('archive.dirmode', '0700');
127$result = Lancelot::Module::archive_store::execute($db, $msg);
128is($result, "", "octal archive.dirmode test result");
129$adir = "$ENV{HOME}/.pl/$listname/other-archive";
130ok(-d $adir, "archive directory created");
131file_mode_is($adir, 0700, "archive directory mode is 0700");
132
133# absolute pathname for archive directory
134rmtree($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);
138is($result, "", "absolute pathname archive.directory test result");
139ok(-d $adir, "archive directory created");
140
141# archive directory impossible to create
142rmtree($adir, 0);
143$adir = "$ENV{HOME}/.pl/$listname/other-archive";
144chmod 0111, dirname($adir);
145$db->set_config('archive.directory', $adir);
146$result = Lancelot::Module::archive_store::execute($db, $msg);
147is($result, "", "uncreatable archive.directory test result");
148chmod 0755, dirname($adir);
149file_not_exists_ok($adir, "archive directory not created");
150
151# archive file impossible to create
152rmtree($adir, 0);
153$adir = "$ENV{HOME}/.pl/$listname/other-archive";
154mkpath "$adir/0", 0;
155chmod 0111, "$adir/0";
156$db->set_config('archive.directory', $adir);
157$result = Lancelot::Module::archive_store::execute($db, $msg);
158is($result, "", "uncreatable archive file test result");
159$msgn = $msg->get_flag("number");
160$fname = sprintf "$adir/%u/%02u", int($msgn/100), $msgn % 100;
161chmod 0755, "$adir/0";
162file_not_exists_ok($fname,
163                   "unwritable archive.directory test file not created");
164
165# archive mail from subscriber
166rmtree($adir, 0);
167$db->set_config('mail.policy.subscribersonly', 0);
168foreach 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}
Note: See TracBrowser for help on using the browser.