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