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>
7 use Fcntl qw(SEEK_SET);
9 use PublicInbox::TestCommon;
13 require_mods(qw(DBD::SQLite Search::Xapian));
14 use_ok 'PublicInbox::V2Writable';
15 my ($tmpdir, $for_destroy) = tmpdir();
17 inboxdir => "$tmpdir/inbox",
18 name => 'test-v2writable',
19 address => [ 'test@example.com' ],
21 my $mime = PublicInbox::MIME->create(
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>',
30 body => "hello world\n",
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;
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');
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');
50 $ibx = PublicInbox::Inbox->new($ibx);
53 ok(run_script([ '-index', "$tmpdir/inbox" ]), 'v1 indexed');
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');
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');
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');
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');
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');