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