]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir_v2.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / watch_maildir_v2.t
1 # Copyright (C) 2018-2021 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 $cfg = PublicInbox::Config->new(\$orig);
48 my $ibx = $cfg->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 $ibx->{-no_fsync} = 1;
51
52 PublicInbox::Watch->new($cfg)->scan('full');
53 my $total = scalar @{$ibx->over->recent};
54 is($total, 1, 'got one revision');
55
56 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
57 # my @list = $git->qx(qw(rev-list refs/heads/master));
58 # is(scalar @list, 1, 'one revision in rev-list');
59
60 my $write_spam = sub {
61         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
62         $sem->prepare(\$msg);
63         $sem->commit;
64         my @new = glob("$spamdir/new/*");
65         is(scalar @new, 1);
66         my @p = split(m!/+!, $new[0]);
67         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
68         is(unlink($new[0]), 1);
69 };
70 $write_spam->();
71 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
72 PublicInbox::Watch->new($cfg)->scan('full');
73 is_deeply($ibx->over->recent, [], 'deleted file');
74 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
75
76 # check with scrubbing
77 {
78         $msg .= qq(--
79 To unsubscribe from this list: send the line "unsubscribe git" in
80 the body of a message to majordomo\@vger.kernel.org
81 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
82         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
83         PublicInbox::Watch->new($cfg)->scan('full');
84         my $msgs = $ibx->over->recent;
85         is(scalar(@$msgs), 1, 'got one file back');
86         my $mref = $ibx->msg_by_smsg($msgs->[0]);
87         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
88
89         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
90         $write_spam->();
91         PublicInbox::Watch->new($cfg)->scan('full');
92         $msgs = $ibx->over->recent;
93         is(scalar(@$msgs), 0, 'inbox is empty again');
94         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
95 }
96
97 {
98         my $fail_bin = getcwd()."/t/fail-bin";
99         ok(-x "$fail_bin/spamc", "mock spamc exists");
100         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
101         local $ENV{PATH} = $fail_path;
102         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
103         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
104         {
105                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
106                 PublicInbox::Watch->new($cfg)->scan('full');
107         }
108         my $msgs = $ibx->over->recent;
109         is(scalar(@$msgs), 0, 'inbox is still empty');
110         is(unlink(glob("$maildir/new/*")), 1);
111 }
112
113 {
114         my $main_bin = getcwd()."/t/main-bin";
115         ok(-x "$main_bin/spamc", "mock spamc exists");
116         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
117         local $ENV{PATH} = $main_path;
118         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
119         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
120         PublicInbox::Watch->new($cfg)->scan('full');
121         my $msgs = $ibx->over->recent;
122         is(scalar(@$msgs), 1, 'inbox has one mail after spamc OK-ed a message');
123         my $mref = $ibx->msg_by_smsg($msgs->[0]);
124         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
125         delete $cfg->{'publicinboxwatch.spamcheck'};
126 }
127
128 {
129         my $patch = 't/data/0001.patch';
130         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
131         $msg = do { local $/; <$fh> };
132         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
133         PublicInbox::Watch->new($cfg)->scan('full');
134         my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
135         is($post->size, 1, 'diff postimage found');
136         my $pre = $ibx->search->mset('dfpre:090d998b6c2c');
137         is($pre->size, 1, 'diff preimage found');
138         $pre = $ibx->search->mset_to_smsg($ibx, $pre);
139         $post = $ibx->search->mset_to_smsg($ibx, $post);
140         is(scalar(@$pre), 1, 'diff preimage found');
141         is($post->[0]->{blob}, $pre->[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         PublicInbox::Import::init_bare($v1repo);
150         my $raw = <<EOF;
151 $orig$v1pfx.address=$v1addr
152 $v1pfx.inboxdir=$v1repo
153 $v1pfx.watch=maildir:$maildir
154 EOF
155         my $cfg = PublicInbox::Config->new(\$raw);
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::Watch->new($cfg)->scan('full');
167         my $mset = $ibx->search->reopen->mset('m:both@b.com');
168         my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
169         my $v1 = $cfg->lookup_name('v1');
170         my $msg = $v1->git->cat_file($msgs->[0]->{blob});
171         is($both, $$msg, 'got original message back from v1');
172         $msg = $ibx->git->cat_file($msgs->[0]->{blob});
173         is($both, $$msg, 'got original message back from v2');
174 }
175
176 {
177         my $want = <<'EOF';
178 From: <u@example.com>
179 List-Id: <i.want.you.to.want.me>
180 Message-ID: <do.want@example.com>
181 EOF
182         my $do_not_want = <<'EOF';
183 From: <u@example.com>
184 List-Id: <do.not.want>
185 X-Mailing-List: no@example.com
186 Message-ID: <do.not.want@example.com>
187 EOF
188         my $raw = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
189         PublicInbox::Emergency->new($maildir)->prepare(\$want);
190         PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
191         my $cfg = PublicInbox::Config->new(\$raw);
192         PublicInbox::Watch->new($cfg)->scan('full');
193         $ibx = $cfg->lookup_name('test');
194         my $num = $ibx->mm->num_for('do.want@example.com');
195         ok(defined $num, 'List-ID matched for watch');
196         $num = $ibx->mm->num_for('do.not.want@example.com');
197         is($num, undef, 'unaccepted List-ID matched for watch');
198
199         $raw = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
200         $cfg = PublicInbox::Config->new(\$raw);
201         PublicInbox::Watch->new($cfg)->scan('full');
202         $ibx = $cfg->lookup_name('test');
203         $num = $ibx->mm->num_for('do.not.want@example.com');
204         ok(defined $num, 'X-Mailing-List matched');
205 }
206
207 done_testing;