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