]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mda.t
0cd852b1908b97240e00bddf1a8ff84384997769
[public-inbox.git] / t / v2mda.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 strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use File::Temp qw/tempdir/;
8 use Fcntl qw(SEEK_SET);
9 use Cwd;
10 require './t/common.perl';
11 require_git(2.6);
12
13 my $V = 2;
14 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
15         eval "require $mod";
16         plan skip_all => "$mod missing for v2mda.t" if $@;
17 }
18 use_ok 'PublicInbox::V2Writable';
19 my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 my $ibx = {
21         inboxdir => "$tmpdir/inbox",
22         name => 'test-v2writable',
23         address => [ 'test@example.com' ],
24 };
25 my $mime = PublicInbox::MIME->create(
26         header => [
27                 From => 'a@example.com',
28                 To => 'test@example.com',
29                 Subject => 'this is a subject',
30                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
31                 'Message-ID' => '<foo@bar>',
32                 'List-ID' => '<test.example.com>',
33         ],
34         body => "hello world\n",
35 );
36
37 my $main_bin = getcwd()."/t/main-bin";
38 my $fail_bin = getcwd()."/t/fail-bin";
39 local $ENV{PI_DIR} = "$tmpdir/foo";
40 my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
41 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
42 my $faildir = "$tmpdir/fail";
43 local $ENV{PI_EMERGENCY} = $faildir;
44 ok(mkdir $faildir);
45 my @cmd = (qw(-init), "-V$V", $ibx->{name},
46                 $ibx->{inboxdir}, 'http://localhost/test',
47                 $ibx->{address}->[0]);
48 ok(run_script(\@cmd), 'initialized v2 inbox');
49
50 my $rdr = { 0 => \($mime->as_string) };
51 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
52 ok(run_script(['-mda'], undef, $rdr), 'mda delivered a message');
53
54 $ibx = PublicInbox::Inbox->new($ibx);
55
56 if ($V == 1) {
57         ok(run_script([ '-index', "$tmpdir/inbox" ]), 'v1 indexed');
58 }
59 my $msgs = $ibx->search->query('');
60 is(scalar(@$msgs), 1, 'only got one message');
61 my $saved = $ibx->smsg_mime($msgs->[0]);
62 is($saved->{mime}->as_string, $mime->as_string, 'injected message');
63
64 {
65         my @new = glob("$faildir/new/*");
66         is_deeply(\@new, [], 'nothing in faildir');
67         local $ENV{PATH} = $fail_path;
68         $mime->header_set('Message-ID', '<bar@foo>');
69         $rdr->{0} = \($mime->as_string);
70         ok(run_script(['-mda'], undef, $rdr), 'mda did not die on "spam"');
71         @new = glob("$faildir/new/*");
72         is(scalar(@new), 1, 'got a message in faildir');
73         $msgs = $ibx->search->reopen->query('');
74         is(scalar(@$msgs), 1, 'no new message');
75
76         my $config = "$ENV{PI_DIR}/config";
77         ok(-f $config, 'config exists');
78         my $k = 'publicinboxmda.spamcheck';
79         is(system('git', 'config', "--file=$config", $k, 'none'), 0,
80                 'disabled spamcheck for mda');
81
82         ok(run_script(['-mda'], undef, $rdr), 'mda did not die');
83         my @again = glob("$faildir/new/*");
84         is_deeply(\@again, \@new, 'no new message in faildir');
85         $msgs = $ibx->search->reopen->query('');
86         is(scalar(@$msgs), 2, 'new message added OK');
87 }
88
89 {
90         my $patch = 't/data/0001.patch';
91         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
92         $rdr->{0} = \(do { local $/; <$fh> });
93         ok(run_script(['-mda'], undef, $rdr), 'mda delivered a patch');
94         my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
95         is(scalar(@$post), 1, 'got one result for dfpost');
96         my $pre = $ibx->search->query('dfpre:090d998');
97         is(scalar(@$pre), 1, 'got one result for dfpre');
98         is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
99 }
100
101 done_testing();