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