]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir_v2.t
tests: use strict everywhere
[public-inbox.git] / t / watch_maildir_v2.t
1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use Test::More;
5 use File::Temp qw/tempdir/;
6 use PublicInbox::MIME;
7 use Cwd;
8 use PublicInbox::Config;
9 require './t/common.perl';
10 require_git(2.6);
11 my @mods = qw(Search::Xapian DBD::SQLite Filesys::Notify::Simple);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for watch_maildir_v2.t" if $@;
15 }
16 require PublicInbox::V2Writable;
17 my $tmpdir = tempdir('watch_maildir-v2-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $inboxdir = "$tmpdir/v2";
19 my $maildir = "$tmpdir/md";
20 my $spamdir = "$tmpdir/spam";
21 use_ok 'PublicInbox::WatchMaildir';
22 use_ok 'PublicInbox::Emergency';
23 my $cfgpfx = "publicinbox.test";
24 my $addr = 'test-public@example.com';
25 my @cmd = ('-init', '-V2', 'test', $inboxdir,
26         'http://example.com/v2list', $addr);
27 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
28 ok(run_script(\@cmd), 'public-inbox init OK');
29
30 my $msg = <<EOF;
31 From: user\@example.com
32 To: $addr
33 Subject: spam
34 Message-Id: <a\@b.com>
35 Date: Sat, 18 Jun 2016 00:00:00 +0000
36
37 something
38 EOF
39 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
40 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
41         'create FIFO to ensure we do not get stuck on it :P');
42 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
43
44 my $orig = <<EOF;
45 $cfgpfx.address=$addr
46 $cfgpfx.inboxdir=$inboxdir
47 $cfgpfx.watch=maildir:$maildir
48 $cfgpfx.filter=PublicInbox::Filter::Vger
49 publicinboxlearn.watchspam=maildir:$spamdir
50 EOF
51 my $config = PublicInbox::Config->new(\$orig);
52 my $ibx = $config->lookup_name('test');
53 ok($ibx, 'found inbox by name');
54 my $srch = $ibx->search;
55
56 PublicInbox::WatchMaildir->new($config)->scan('full');
57 my ($total, undef) = $srch->reopen->query('');
58 is($total, 1, 'got one revision');
59
60 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
61 # my @list = $git->qx(qw(rev-list refs/heads/master));
62 # is(scalar @list, 1, 'one revision in rev-list');
63
64 my $write_spam = sub {
65         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
66         $sem->prepare(\$msg);
67         $sem->commit;
68         my @new = glob("$spamdir/new/*");
69         is(scalar @new, 1);
70         my @p = split(m!/+!, $new[0]);
71         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
72         is(unlink($new[0]), 1);
73 };
74 $write_spam->();
75 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
76 PublicInbox::WatchMaildir->new($config)->scan('full');
77 is(($srch->reopen->query(''))[0], 0, 'deleted file');
78
79 # check with scrubbing
80 {
81         $msg .= qq(--
82 To unsubscribe from this list: send the line "unsubscribe git" in
83 the body of a message to majordomo\@vger.kernel.org
84 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
85         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
86         PublicInbox::WatchMaildir->new($config)->scan('full');
87         my ($nr, $msgs) = $srch->reopen->query('');
88         is($nr, 1, 'got one file back');
89         my $mref = $ibx->msg_by_smsg($msgs->[0]);
90         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
91
92         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
93         $write_spam->();
94         PublicInbox::WatchMaildir->new($config)->scan('full');
95         ($nr, $msgs) = $srch->reopen->query('');
96         is($nr, 0, 'inbox is empty again');
97 }
98
99 {
100         my $fail_bin = getcwd()."/t/fail-bin";
101         ok(-x "$fail_bin/spamc", "mock spamc exists");
102         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
103         local $ENV{PATH} = $fail_path;
104         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
105         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
106         {
107                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
108                 PublicInbox::WatchMaildir->new($config)->scan('full');
109         }
110         my ($nr, $msgs) = $srch->reopen->query('');
111         is($nr, 0, 'inbox is still empty');
112         is(unlink(glob("$maildir/new/*")), 1);
113 }
114
115 {
116         my $main_bin = getcwd()."/t/main-bin";
117         ok(-x "$main_bin/spamc", "mock spamc exists");
118         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
119         local $ENV{PATH} = $main_path;
120         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
121         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
122         PublicInbox::WatchMaildir->new($config)->scan('full');
123         my ($nr, $msgs) = $srch->reopen->query('');
124         is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
125         my $mref = $ibx->msg_by_smsg($msgs->[0]);
126         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
127         delete $config->{'publicinboxwatch.spamcheck'};
128 }
129
130 {
131         my $patch = 't/data/0001.patch';
132         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
133         $msg = eval { local $/; <$fh> };
134         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
135         PublicInbox::WatchMaildir->new($config)->scan('full');
136         my ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');
137         is($nr, 1, 'diff postimage found');
138         my $post = $msgs->[0];
139         ($nr, $msgs) = $srch->query('dfpre:090d998b6c2c');
140         is($nr, 1, 'diff preimage found');
141         is($post->{blob}, $msgs->[0]->{blob}, 'same message');
142 }
143
144 # multiple inboxes in the same maildir
145 {
146         my $v1repo = "$tmpdir/v1";
147         my $v1pfx = "publicinbox.v1";
148         my $v1addr = 'v1-public@example.com';
149         is(system(qw(git init -q --bare), $v1repo), 0, 'v1 init OK');
150         my $cfg2 = <<EOF;
151 $orig$v1pfx.address=$v1addr
152 $v1pfx.inboxdir=$v1repo
153 $v1pfx.watch=maildir:$maildir
154 EOF
155         my $config = PublicInbox::Config->new(\$cfg2);
156         my $both = <<EOF;
157 From: user\@example.com
158 To: $addr, $v1addr
159 Subject: both
160 Message-Id: <both\@b.com>
161 Date: Sat, 18 Jun 2016 00:00:00 +0000
162
163 both
164 EOF
165         PublicInbox::Emergency->new($maildir)->prepare(\$both);
166         PublicInbox::WatchMaildir->new($config)->scan('full');
167         my ($total, $msgs) = $srch->reopen->query('m:both@b.com');
168         my $v1 = $config->lookup_name('v1');
169         my $msg = $v1->git->cat_file($msgs->[0]->{blob});
170         is($both, $$msg, 'got original message back from v1');
171         $msg = $ibx->git->cat_file($msgs->[0]->{blob});
172         is($both, $$msg, 'got original message back from v2');
173 }
174
175 {
176         my $want = <<'EOF';
177 From: <u@example.com>
178 List-Id: <i.want.you.to.want.me>
179 Message-ID: <do.want@example.com>
180 EOF
181         my $do_not_want = <<'EOF';
182 From: <u@example.com>
183 List-Id: <do.not.want>
184 X-Mailing-List: no@example.com
185 Message-ID: <do.not.want@example.com>
186 EOF
187         my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
188         PublicInbox::Emergency->new($maildir)->prepare(\$want);
189         PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
190         my $config = PublicInbox::Config->new(\$cfg);
191         PublicInbox::WatchMaildir->new($config)->scan('full');
192         $ibx = $config->lookup_name('test');
193         my $num = $ibx->mm->num_for('do.want@example.com');
194         ok(defined $num, 'List-ID matched for watch');
195         $num = $ibx->mm->num_for('do.not.want@example.com');
196         is($num, undef, 'unaccepted List-ID matched for watch');
197
198         $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
199         $config = PublicInbox::Config->new(\$cfg);
200         PublicInbox::WatchMaildir->new($config)->scan('full');
201         $ibx = $config->lookup_name('test');
202         $num = $ibx->mm->num_for('do.not.want@example.com');
203         ok(defined $num, 'X-Mailing-List matched');
204 }
205
206 done_testing;