1 # Copyright (C) 2018-2021 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);
49 my $msgs = $ibx->over->recent;
50 is(scalar(@$msgs), 1, 'only got one message');
51 my $eml = $ibx->smsg_eml($msgs->[0]);
52 is($eml->as_string, $mime->as_string, 'injected message');
55 my @new = glob("$faildir/new/*");
56 is_deeply(\@new, [], 'nothing in faildir');
57 local $ENV{PATH} = $fail_path;
58 $mime->header_set('Message-ID', '<bar@foo>');
59 $rdr->{0} = \($mime->as_string);
60 ok(run_script(['-mda'], undef, $rdr), 'mda did not die on "spam"');
61 @new = glob("$faildir/new/*");
62 is(scalar(@new), 1, 'got a message in faildir');
63 $msgs = $ibx->over->recent;
64 is(scalar(@$msgs), 1, 'no new message');
66 my $config = "$ENV{PI_DIR}/config";
67 ok(-f $config, 'config exists');
68 my $k = 'publicinboxmda.spamcheck';
69 is(xsys('git', 'config', "--file=$config", $k, 'none'), 0,
70 'disabled spamcheck for mda');
72 ok(run_script(['-mda'], undef, $rdr), 'mda did not die');
73 my @again = glob("$faildir/new/*");
74 is_deeply(\@again, \@new, 'no new message in faildir');
75 $msgs = $ibx->over->recent;
76 is(scalar(@$msgs), 2, 'new message added OK');
80 my $patch = 't/data/0001.patch';
81 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
82 $rdr->{0} = \(do { local $/; <$fh> });
83 ok(run_script(['-mda'], undef, $rdr), 'mda delivered a patch');
84 my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
85 is($post->size, 1, 'got one result for dfpost');
86 my $pre = $ibx->search->mset('dfpre:090d998');
87 is($pre->size, 1, 'got one result for dfpre');
88 $pre = $ibx->search->mset_to_smsg($ibx, $pre);
89 $post = $ibx->search->mset_to_smsg($ibx, $post);
90 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');