1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use File::Temp qw/tempdir/;
8 use Fcntl qw(SEEK_SET);
12 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
14 plan skip_all => "$mod missing for v2mda.t" if $@;
16 use_ok 'PublicInbox::V2Writable';
17 my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 mainrepo => "$tmpdir/inbox",
20 name => 'test-v2writable',
21 address => [ 'test@example.com' ],
23 my $mime = PublicInbox::MIME->create(
25 From => 'a@example.com',
26 To => 'test@example.com',
27 Subject => 'this is a subject',
28 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29 'Message-ID' => '<foo@bar>',
30 'List-ID' => '<test.example.com>',
32 body => "hello world\n",
35 my $mda = "blib/script/public-inbox-mda";
36 ok(-f "blib/script/public-inbox-mda", '-mda exists');
37 my $main_bin = getcwd()."/t/main-bin";
38 my $fail_bin = getcwd()."/t/fail-bin";
39 local $ENV{PI_DIR} = "$tmpdir/foo";
40 my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
41 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
42 my $faildir = "$tmpdir/fail";
43 local $ENV{PI_EMERGENCY} = $faildir;
45 my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
46 $ibx->{mainrepo}, 'http://localhost/test',
47 $ibx->{address}->[0]);
48 ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
50 open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
51 ok($tmp->print($mime->as_string), 'wrote to temporary file');
52 ok($tmp->flush, 'flushed temporary file');
53 ok($tmp->sysseek(0, SEEK_SET), 'seeked');
55 my $rdr = { 0 => fileno($tmp) };
56 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
57 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
58 'mda delivered a message');
60 $ibx = PublicInbox::Inbox->new($ibx);
63 my $cmd = [ 'public-inbox-index', "$tmpdir/inbox" ];
64 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
66 my $msgs = $ibx->search->query('');
67 is(scalar(@$msgs), 1, 'only got one message');
68 my $saved = $ibx->smsg_mime($msgs->[0]);
69 is($saved->{mime}->as_string, $mime->as_string, 'injected message');
72 my @new = glob("$faildir/new/*");
73 is_deeply(\@new, [], 'nothing in faildir');
74 local $ENV{PATH} = $fail_path;
75 $mime->header_set('Message-ID', '<bar@foo>');
76 ok($tmp->sysseek(0, SEEK_SET) &&
78 $tmp->print($mime->as_string) &&
80 $tmp->sysseek(0, SEEK_SET),
81 'rewound and rewrite temporary file');
82 my $cmd = ['public-inbox-mda'];
83 ok(PublicInbox::Import::run_die($cmd, undef, $rdr),
84 'mda did not die on "spam"');
85 @new = glob("$faildir/new/*");
86 is(scalar(@new), 1, 'got a message in faildir');
87 $msgs = $ibx->search->reopen->query('');
88 is(scalar(@$msgs), 1, 'no new message');
90 my $config = "$ENV{PI_DIR}/config";
91 ok(-f $config, 'config exists');
92 my $k = 'publicinboxmda.spamcheck';
93 is(system('git', 'config', "--file=$config", $k, 'none'), 0,
94 'disabled spamcheck for mda');
95 ok($tmp->sysseek(0, SEEK_SET), 'rewound input file');
97 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'mda did not die');
98 my @again = glob("$faildir/new/*");
99 is_deeply(\@again, \@new, 'no new message in faildir');
100 $msgs = $ibx->search->reopen->query('');
101 is(scalar(@$msgs), 2, 'new message added OK');
105 my $patch = 't/data/0001.patch';
106 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
107 $rdr = { 0 => fileno($fh) };
108 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
109 'mda delivered a patch');
110 my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
111 is(scalar(@$post), 1, 'got one result for dfpost');
112 my $pre = $ibx->search->query('dfpre:090d998');
113 is(scalar(@$pre), 1, 'got one result for dfpre');
114 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');