]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir_v2.t
www: rework query responses to avoid COUNT in SQLite
[public-inbox.git] / t / watch_maildir_v2.t
1 # Copyright (C) 2018 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 my @mods = qw(Filesys::Notify::Simple PublicInbox::V2Writable);
9 foreach my $mod (@mods) {
10         eval "require $mod";
11         plan skip_all => "$mod missing for watch_maildir_v2.t" if $@;
12 }
13
14 my $tmpdir = tempdir('watch_maildir-v2-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $mainrepo = "$tmpdir/v2";
16 my $maildir = "$tmpdir/md";
17 my $spamdir = "$tmpdir/spam";
18 use_ok 'PublicInbox::WatchMaildir';
19 use_ok 'PublicInbox::Emergency';
20 my $cfgpfx = "publicinbox.test";
21 my $addr = 'test-public@example.com';
22 my @cmd = ('blib/script/public-inbox-init', '-V2', 'test', $mainrepo,
23         'http://example.com/v2list', $addr);
24 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
25 is(system(@cmd), 0, 'public-inbox init OK');
26
27 my $msg = <<EOF;
28 From: user\@example.com
29 To: $addr
30 Subject: spam
31 Message-Id: <a\@b.com>
32 Date: Sat, 18 Jun 2016 00:00:00 +0000
33
34 something
35 EOF
36 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
37 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
38         'create FIFO to ensure we do not get stuck on it :P');
39 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
40
41 my $config = PublicInbox::Config->new({
42         "$cfgpfx.address" => $addr,
43         "$cfgpfx.mainrepo" => $mainrepo,
44         "$cfgpfx.watch" => "maildir:$maildir",
45         "$cfgpfx.filter" => 'PublicInbox::Filter::Vger',
46         "publicinboxlearn.watchspam" => "maildir:$spamdir",
47 });
48 my $ibx = $config->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 my $srch = $ibx->search;
51
52 PublicInbox::WatchMaildir->new($config)->scan('full');
53 my ($total, undef) = $srch->reopen->query('');
54 is($total, 1, 'got one revision');
55
56 # my $git = PublicInbox::Git->new("$mainrepo/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::WatchMaildir->new($config)->scan('full');
73 is(($srch->reopen->query(''))[0], 0, 'deleted file');
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::WatchMaildir->new($config)->scan('full');
83         my ($nr, $msgs) = $srch->reopen->query('');
84         is($nr, 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::WatchMaildir->new($config)->scan('full');
91         ($nr, $msgs) = $srch->reopen->query('');
92         is($nr, 0, 'inbox is empty again');
93 }
94
95 {
96         my $fail_bin = getcwd()."/t/fail-bin";
97         ok(-x "$fail_bin/spamc", "mock spamc exists");
98         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
99         local $ENV{PATH} = $fail_path;
100         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
101         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
102         {
103                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
104                 PublicInbox::WatchMaildir->new($config)->scan('full');
105         }
106         ($nr, $msgs) = $srch->reopen->query('');
107         is($nr, 0, 'inbox is still empty');
108         is(unlink(glob("$maildir/new/*")), 1);
109 }
110
111 {
112         my $main_bin = getcwd()."/t/main-bin";
113         ok(-x "$main_bin/spamc", "mock spamc exists");
114         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
115         local $ENV{PATH} = $main_path;
116         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
117         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
118         PublicInbox::WatchMaildir->new($config)->scan('full');
119         ($nr, $msgs) = $srch->reopen->query('');
120         is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
121         my $mref = $ibx->msg_by_smsg($msgs->[0]);
122         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
123 }
124
125 done_testing;