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