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>
7 use Fcntl qw(SEEK_SET);
9 require './t/common.perl';
13 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
15 plan skip_all => "$mod missing for v2mda.t" if $@;
17 use_ok 'PublicInbox::V2Writable';
18 my ($tmpdir, $for_destroy) = tmpdir();
20 inboxdir => "$tmpdir/inbox",
21 name => 'test-v2writable',
22 address => [ 'test@example.com' ],
24 my $mime = PublicInbox::MIME->create(
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>',
33 body => "hello world\n",
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;
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');
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');
53 $ibx = PublicInbox::Inbox->new($ibx);
56 ok(run_script([ '-index', "$tmpdir/inbox" ]), 'v1 indexed');
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');
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');
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');
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');
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');