]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mda.t
mda: v2: ensure message bodies are indexed
[public-inbox.git] / t / v2mda.t
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use File::Temp qw/tempdir/;
8 use Fcntl qw(SEEK_SET);
9 use Cwd;
10
11 my $V = 2;
12 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for v2mda.t" if $@;
15 }
16 use_ok 'PublicInbox::V2Writable';
17 my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $ibx = {
19         mainrepo => "$tmpdir/inbox",
20         name => 'test-v2writable',
21         address => [ 'test@example.com' ],
22 };
23 my $mime = PublicInbox::MIME->create(
24         header => [
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>',
31         ],
32         body => "hello world\n",
33 );
34
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 local $ENV{PI_DIR} = "$tmpdir/foo";
39 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
40 local $ENV{PI_EMERGENCY} = "$tmpdir/fail";
41 ok(mkdir "$tmpdir/fail");
42
43 my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
44                 $ibx->{mainrepo}, 'http://localhost/test',
45                 $ibx->{address}->[0]);
46 ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
47
48 open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
49 ok($tmp->print($mime->as_string), 'wrote to temporary file');
50 ok($tmp->flush, 'flushed temporary file');
51 ok($tmp->sysseek(0, SEEK_SET), 'seeked');
52
53 my $rdr = { 0 => fileno($tmp) };
54 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
55 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
56         'mda delivered a message');
57
58 $ibx = PublicInbox::Inbox->new($ibx);
59
60 if ($V == 1) {
61         my $cmd = [ 'public-inbox-index', "$tmpdir/inbox" ];
62         ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
63 }
64 my $msgs = $ibx->search->query('');
65 my $saved = $ibx->smsg_mime($msgs->[0]);
66 is($saved->{mime}->as_string, $mime->as_string, 'injected message');
67
68 my $patch = 't/data/0001.patch';
69 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
70 $rdr = { 0 => fileno($fh) };
71 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
72         'mda delivered a patch');
73 my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
74 is(scalar(@$post), 1, 'got one result for dfpost');
75 my $pre = $ibx->search->query('dfpre:090d998');
76 is(scalar(@$pre), 1, 'got one result for dfpre');
77 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
78 done_testing();