]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir.t
remove most internal Email::MIME usage
[public-inbox.git] / t / watch_maildir.t
1 # Copyright (C) 2016-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_mods(qw(Filesys::Notify::Simple));
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $git_dir = "$tmpdir/test.git";
13 my $maildir = "$tmpdir/md";
14 my $spamdir = "$tmpdir/spam";
15 use_ok 'PublicInbox::WatchMaildir';
16 use_ok 'PublicInbox::Emergency';
17 my $cfgpfx = "publicinbox.test";
18 my $addr = 'test-public@example.com';
19 PublicInbox::Import::init_bare($git_dir);
20
21 my $msg = <<EOF;
22 From: user\@example.com
23 To: $addr
24 Subject: spam
25 Message-Id: <a\@b.com>
26 Date: Sat, 18 Jun 2016 00:00:00 +0000
27
28 something
29 EOF
30 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
31 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
32         'create FIFO to ensure we do not get stuck on it :P');
33 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
34
35 my $config = PublicInbox::Config->new(\<<EOF);
36 $cfgpfx.address=$addr
37 $cfgpfx.inboxdir=$git_dir
38 $cfgpfx.watch=maildir:$maildir
39 $cfgpfx.filter=PublicInbox::Filter::Vger
40 publicinboxlearn.watchspam=maildir:$spamdir
41 EOF
42
43 PublicInbox::WatchMaildir->new($config)->scan('full');
44 my $git = PublicInbox::Git->new($git_dir);
45 my @list = $git->qx(qw(rev-list refs/heads/master));
46 is(scalar @list, 1, 'one revision in rev-list');
47
48 my $write_spam = sub {
49         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
50         $sem->prepare(\$msg);
51         $sem->commit;
52         my @new = glob("$spamdir/new/*");
53         is(scalar @new, 1);
54         my @p = split(m!/+!, $new[0]);
55         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
56         is(unlink($new[0]), 1);
57 };
58 $write_spam->();
59 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
60 PublicInbox::WatchMaildir->new($config)->scan('full');
61 @list = $git->qx(qw(rev-list refs/heads/master));
62 is(scalar @list, 2, 'two revisions in rev-list');
63 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
64 is(scalar @list, 0, 'tree is empty');
65
66 # check with scrubbing
67 {
68         $msg .= qq(--
69 To unsubscribe from this list: send the line "unsubscribe git" in
70 the body of a message to majordomo\@vger.kernel.org
71 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
72         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
73         PublicInbox::WatchMaildir->new($config)->scan('full');
74         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
75         is(scalar @list, 1, 'tree has one file');
76         my $mref = $git->cat_file('HEAD:'.$list[0]);
77         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
78
79         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
80         $write_spam->();
81         PublicInbox::WatchMaildir->new($config)->scan('full');
82         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
83         is(scalar @list, 0, 'tree is empty');
84         @list = $git->qx(qw(rev-list refs/heads/master));
85         is(scalar @list, 4, 'four revisions in rev-list');
86 }
87
88 {
89         my $fail_bin = getcwd()."/t/fail-bin";
90         ok(-x "$fail_bin/spamc", "mock spamc exists");
91         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
92         local $ENV{PATH} = $fail_path;
93         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
94         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
95         {
96                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
97                 PublicInbox::WatchMaildir->new($config)->scan('full');
98         }
99         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
100         is(scalar @list, 0, 'tree has no files spamc checked');
101         is(unlink(glob("$maildir/new/*")), 1);
102 }
103
104 {
105         my $main_bin = getcwd()."/t/main-bin";
106         ok(-x "$main_bin/spamc", "mock spamc exists");
107         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
108         local $ENV{PATH} = $main_path;
109         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
110         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
111         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
112         PublicInbox::WatchMaildir->new($config)->scan('full');
113         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
114         is(scalar @list, 1, 'tree has one file after spamc checked');
115
116         # XXX: workaround some weird caching/memoization in cat-file,
117         # shouldn't be an issue in real-world use, though...
118         $git = PublicInbox::Git->new($git_dir);
119
120         my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
121         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
122 }
123
124 sub is_maildir {
125         my ($dir) = @_;
126         PublicInbox::WatchMaildir::is_maildir($dir);
127 }
128
129 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
130 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
131 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
132
133 done_testing;