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;
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::Eml->new(<<'EOF');
24 Subject: this is a subject
25 Date: Fri, 02 Oct 1993 00:00:00 +0000
27 List-ID: <test.example.com>
31 my $main_bin = getcwd()."/t/main-bin";
32 my $fail_bin = getcwd()."/t/fail-bin";
33 local $ENV{PI_DIR} = "$tmpdir/foo";
34 my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
35 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
36 my $faildir = "$tmpdir/fail";
37 local $ENV{PI_EMERGENCY} = $faildir;
39 my @cmd = (qw(-init), "-V$V", $ibx->{name},
40 $ibx->{inboxdir}, 'http://localhost/test',
41 $ibx->{address}->[0]);
42 ok(run_script(\@cmd), 'initialized v2 inbox');
44 my $rdr = { 0 => \($mime->as_string) };
45 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
46 ok(run_script(['-mda'], undef, $rdr), 'mda delivered a message');
48 $ibx = PublicInbox::Inbox->new($ibx);
51 ok(run_script([ '-index', "$tmpdir/inbox" ]), 'v1 indexed');
53 my $msgs = $ibx->over->recent;
54 is(scalar(@$msgs), 1, 'only got one message');
55 my $eml = $ibx->smsg_eml($msgs->[0]);
56 is($eml->as_string, $mime->as_string, 'injected message');
59 my @new = glob("$faildir/new/*");
60 is_deeply(\@new, [], 'nothing in faildir');
61 local $ENV{PATH} = $fail_path;
62 $mime->header_set('Message-ID', '<bar@foo>');
63 $rdr->{0} = \($mime->as_string);
64 ok(run_script(['-mda'], undef, $rdr), 'mda did not die on "spam"');
65 @new = glob("$faildir/new/*");
66 is(scalar(@new), 1, 'got a message in faildir');
67 $msgs = $ibx->over->recent;
68 is(scalar(@$msgs), 1, 'no new message');
70 my $config = "$ENV{PI_DIR}/config";
71 ok(-f $config, 'config exists');
72 my $k = 'publicinboxmda.spamcheck';
73 is(xsys('git', 'config', "--file=$config", $k, 'none'), 0,
74 'disabled spamcheck for mda');
76 ok(run_script(['-mda'], undef, $rdr), 'mda did not die');
77 my @again = glob("$faildir/new/*");
78 is_deeply(\@again, \@new, 'no new message in faildir');
79 $msgs = $ibx->over->recent;
80 is(scalar(@$msgs), 2, 'new message added OK');
84 my $patch = 't/data/0001.patch';
85 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
86 $rdr->{0} = \(do { local $/; <$fh> });
87 ok(run_script(['-mda'], undef, $rdr), 'mda delivered a patch');
88 my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
89 is($post->size, 1, 'got one result for dfpost');
90 my $pre = $ibx->search->mset('dfpre:090d998');
91 is($pre->size, 1, 'got one result for dfpre');
92 $pre = $ibx->search->mset_to_smsg($ibx, $pre);
93 $post = $ibx->search->mset_to_smsg($ibx, $post);
94 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');