]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mda.t
run update-copyrights from gnulib for 2019
[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         mainrepo => "$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 $mda = "blib/script/public-inbox-mda";
38 ok(-f "blib/script/public-inbox-mda", '-mda exists');
39 my $main_bin = getcwd()."/t/main-bin";
40 my $fail_bin = getcwd()."/t/fail-bin";
41 local $ENV{PI_DIR} = "$tmpdir/foo";
42 my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
43 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
44 my $faildir = "$tmpdir/fail";
45 local $ENV{PI_EMERGENCY} = $faildir;
46 ok(mkdir $faildir);
47 my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
48                 $ibx->{mainrepo}, 'http://localhost/test',
49                 $ibx->{address}->[0]);
50 ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
51
52 open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
53 ok($tmp->print($mime->as_string), 'wrote to temporary file');
54 ok($tmp->flush, 'flushed temporary file');
55 ok($tmp->sysseek(0, SEEK_SET), 'seeked');
56
57 my $rdr = { 0 => fileno($tmp) };
58 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
59 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
60         'mda delivered a message');
61
62 $ibx = PublicInbox::Inbox->new($ibx);
63
64 if ($V == 1) {
65         my $cmd = [ 'public-inbox-index', "$tmpdir/inbox" ];
66         ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
67 }
68 my $msgs = $ibx->search->query('');
69 is(scalar(@$msgs), 1, 'only got one message');
70 my $saved = $ibx->smsg_mime($msgs->[0]);
71 is($saved->{mime}->as_string, $mime->as_string, 'injected message');
72
73 {
74         my @new = glob("$faildir/new/*");
75         is_deeply(\@new, [], 'nothing in faildir');
76         local $ENV{PATH} = $fail_path;
77         $mime->header_set('Message-ID', '<bar@foo>');
78         ok($tmp->sysseek(0, SEEK_SET) &&
79                         $tmp->truncate(0) &&
80                         $tmp->print($mime->as_string) &&
81                         $tmp->flush &&
82                         $tmp->sysseek(0, SEEK_SET),
83                 'rewound and rewrite temporary file');
84         my $cmd = ['public-inbox-mda'];
85         ok(PublicInbox::Import::run_die($cmd, undef, $rdr),
86                 'mda did not die on "spam"');
87         @new = glob("$faildir/new/*");
88         is(scalar(@new), 1, 'got a message in faildir');
89         $msgs = $ibx->search->reopen->query('');
90         is(scalar(@$msgs), 1, 'no new message');
91
92         my $config = "$ENV{PI_DIR}/config";
93         ok(-f $config, 'config exists');
94         my $k = 'publicinboxmda.spamcheck';
95         is(system('git', 'config', "--file=$config", $k, 'none'), 0,
96                 'disabled spamcheck for mda');
97         ok($tmp->sysseek(0, SEEK_SET), 'rewound input file');
98
99         ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'mda did not die');
100         my @again = glob("$faildir/new/*");
101         is_deeply(\@again, \@new, 'no new message in faildir');
102         $msgs = $ibx->search->reopen->query('');
103         is(scalar(@$msgs), 2, 'new message added OK');
104 }
105
106 {
107         my $patch = 't/data/0001.patch';
108         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
109         $rdr = { 0 => fileno($fh) };
110         ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
111                 'mda delivered a patch');
112         my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
113         is(scalar(@$post), 1, 'got one result for dfpost');
114         my $pre = $ibx->search->query('dfpre:090d998');
115         is(scalar(@$pre), 1, 'got one result for dfpre');
116         is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
117 }
118
119 done_testing();