]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir_v2.t
search: replace ->query with ->mset
[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::Eml;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 require_git(2.6);
11 require_mods(qw(Search::Xapian DBD::SQLite));
12 require PublicInbox::V2Writable;
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $inboxdir = "$tmpdir/v2";
15 my $maildir = "$tmpdir/md";
16 my $spamdir = "$tmpdir/spam";
17 use_ok 'PublicInbox::Watch';
18 use_ok 'PublicInbox::Emergency';
19 my $cfgpfx = "publicinbox.test";
20 my $addr = 'test-public@example.com';
21 my @cmd = ('-init', '-V2', 'test', $inboxdir,
22         'http://example.com/v2list', $addr);
23 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
24 ok(run_script(\@cmd), 'public-inbox init OK');
25
26 my $msg = <<EOF;
27 From: user\@example.com
28 To: $addr
29 Subject: spam
30 Message-Id: <a\@b.com>
31 Date: Sat, 18 Jun 2016 00:00:00 +0000
32
33 something
34 EOF
35 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
36 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
37         'create FIFO to ensure we do not get stuck on it :P');
38 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
39
40 my $orig = <<EOF;
41 $cfgpfx.address=$addr
42 $cfgpfx.inboxdir=$inboxdir
43 $cfgpfx.watch=maildir:$maildir
44 $cfgpfx.filter=PublicInbox::Filter::Vger
45 publicinboxlearn.watchspam=maildir:$spamdir
46 EOF
47 my $config = PublicInbox::Config->new(\$orig);
48 my $ibx = $config->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50
51 PublicInbox::Watch->new($config)->scan('full');
52 my $total = scalar @{$ibx->over->recent};
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::Watch->new($config)->scan('full');
72 is_deeply($ibx->over->recent, [], 'deleted file');
73 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
74
75 # check with scrubbing
76 {
77         $msg .= qq(--
78 To unsubscribe from this list: send the line "unsubscribe git" in
79 the body of a message to majordomo\@vger.kernel.org
80 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
81         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
82         PublicInbox::Watch->new($config)->scan('full');
83         my $msgs = $ibx->over->recent;
84         is(scalar(@$msgs), 1, 'got one file back');
85         my $mref = $ibx->msg_by_smsg($msgs->[0]);
86         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
87
88         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
89         $write_spam->();
90         PublicInbox::Watch->new($config)->scan('full');
91         $msgs = $ibx->over->recent;
92         is(scalar(@$msgs), 0, 'inbox is empty again');
93         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
94 }
95
96 {
97         my $fail_bin = getcwd()."/t/fail-bin";
98         ok(-x "$fail_bin/spamc", "mock spamc exists");
99         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
100         local $ENV{PATH} = $fail_path;
101         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
102         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
103         {
104                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
105                 PublicInbox::Watch->new($config)->scan('full');
106         }
107         my $msgs = $ibx->over->recent;
108         is(scalar(@$msgs), 0, 'inbox is still empty');
109         is(unlink(glob("$maildir/new/*")), 1);
110 }
111
112 {
113         my $main_bin = getcwd()."/t/main-bin";
114         ok(-x "$main_bin/spamc", "mock spamc exists");
115         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
116         local $ENV{PATH} = $main_path;
117         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
118         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
119         PublicInbox::Watch->new($config)->scan('full');
120         my $msgs = $ibx->over->recent;
121         is(scalar(@$msgs), 1, 'inbox has one mail after spamc OK-ed a message');
122         my $mref = $ibx->msg_by_smsg($msgs->[0]);
123         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
124         delete $config->{'publicinboxwatch.spamcheck'};
125 }
126
127 {
128         my $patch = 't/data/0001.patch';
129         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
130         $msg = do { local $/; <$fh> };
131         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
132         PublicInbox::Watch->new($config)->scan('full');
133         my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
134         is($post->size, 1, 'diff postimage found');
135         my $pre = $ibx->search->mset('dfpre:090d998b6c2c');
136         is($pre->size, 1, 'diff preimage found');
137         $pre = $ibx->search->mset_to_smsg($ibx, $pre);
138         $post = $ibx->search->mset_to_smsg($ibx, $post);
139         is(scalar(@$pre), 1, 'diff preimage found');
140         is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message');
141 }
142
143 # multiple inboxes in the same maildir
144 {
145         my $v1repo = "$tmpdir/v1";
146         my $v1pfx = "publicinbox.v1";
147         my $v1addr = 'v1-public@example.com';
148         PublicInbox::Import::init_bare($v1repo);
149         my $cfg2 = <<EOF;
150 $orig$v1pfx.address=$v1addr
151 $v1pfx.inboxdir=$v1repo
152 $v1pfx.watch=maildir:$maildir
153 EOF
154         my $config = PublicInbox::Config->new(\$cfg2);
155         my $both = <<EOF;
156 From: user\@example.com
157 To: $addr, $v1addr
158 Subject: both
159 Message-Id: <both\@b.com>
160 Date: Sat, 18 Jun 2016 00:00:00 +0000
161
162 both
163 EOF
164         PublicInbox::Emergency->new($maildir)->prepare(\$both);
165         PublicInbox::Watch->new($config)->scan('full');
166         my $mset = $ibx->search->reopen->mset('m:both@b.com');
167         my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
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::Watch->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::Watch->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;