2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use Fcntl qw(SEEK_SET);
9 use PublicInbox::TestCommon;
14 require_mods(qw(DBD::SQLite Search::Xapian));
15 use_ok 'PublicInbox::V2Writable';
16 my ($tmpdir, $for_destroy) = tmpdir();
18 inboxdir => "$tmpdir/inbox",
19 name => 'test-v2writable',
20 address => [ 'test@example.com' ],
22 my $mime = PublicInbox::Eml->new(<<'EOF');
25 Subject: this is a subject
26 Date: Fri, 02 Oct 1993 00:00:00 +0000
28 List-ID: <test.example.com>
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);
50 my $msgs = $ibx->over->recent;
51 is(scalar(@$msgs), 1, 'only got one message');
52 my $eml = $ibx->smsg_eml($msgs->[0]);
53 is($eml->as_string, $mime->as_string, 'injected message');
56 my @new = glob("$faildir/new/*");
57 is_deeply(\@new, [], 'nothing in faildir');
58 local $ENV{PATH} = $fail_path;
59 $mime->header_set('Message-ID', '<bar@foo>');
60 $rdr->{0} = \($mime->as_string);
61 ok(run_script(['-mda'], undef, $rdr), 'mda did not die on "spam"');
62 @new = glob("$faildir/new/*");
63 is(scalar(@new), 1, 'got a message in faildir');
64 $msgs = $ibx->over->recent;
65 is(scalar(@$msgs), 1, 'no new message');
67 my $config = "$ENV{PI_DIR}/config";
68 ok(-f $config, 'config exists');
69 my $k = 'publicinboxmda.spamcheck';
70 is(xsys('git', 'config', "--file=$config", $k, 'none'), 0,
71 'disabled spamcheck for mda');
73 ok(run_script(['-mda'], undef, $rdr), 'mda did not die');
74 my @again = glob("$faildir/new/*");
75 is_deeply(\@again, \@new, 'no new message in faildir');
76 $msgs = $ibx->over->recent;
77 is(scalar(@$msgs), 2, 'new message added OK');
81 my $patch = 't/data/0001.patch';
82 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
83 $rdr->{0} = \(do { local $/; <$fh> });
84 ok(run_script(['-mda'], undef, $rdr), 'mda delivered a patch');
85 my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
86 is($post->size, 1, 'got one result for dfpost');
87 my $pre = $ibx->search->mset('dfpre:090d998');
88 is($pre->size, 1, 'got one result for dfpre');
89 $pre = $ibx->search->mset_to_smsg($ibx, $pre);
90 $post = $ibx->search->mset_to_smsg($ibx, $post);
91 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
93 # git patch-id --stable <t/data/0001.patch | awk '{print $1}'
94 my $patchid = '91ee6b761fc7f47cad9f2b09b10489f313eb5b71';
95 my $mset = $ibx->search->mset("patchid:$patchid");
96 is($mset->size, 1, 'patchid search works');