]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
mbox: async blob fetch for "single message" raw mboxrd
[public-inbox.git] / t / psgi_v2.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 require_git(2.6);
8 use PublicInbox::Eml;
9 use PublicInbox::Config;
10 use PublicInbox::MID qw(mids);
11 require_mods(qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12                 URI::Escape Plack::Builder));
13 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
14 use_ok 'PublicInbox::WWW';
15 use_ok 'PublicInbox::V2Writable';
16 my ($inboxdir, $for_destroy) = tmpdir();
17 my $ibx = {
18         inboxdir => $inboxdir,
19         name => 'test-v2writable',
20         version => 2,
21         -primary_address => 'test@example.com',
22 };
23 $ibx = PublicInbox::Inbox->new($ibx);
24 my $new_mid;
25
26 my $im = PublicInbox::V2Writable->new($ibx, 1);
27 $im->{parallel} = 0;
28
29 my $mime = PublicInbox::Eml->new(<<'EOF');
30 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct  2 00:00:00 1993
31 From: a@example.com
32 To: test@example.com
33 Subject: this is a subject
34 Message-ID: <a-mid@b>
35 Date: Fri, 02 Oct 1993 00:00:00 +0000
36
37 hello world
38 EOF
39 ok($im->add($mime), 'added one message');
40 $mime->body_set("hello world!\n");
41
42 my @warn;
43 local $SIG{__WARN__} = sub { push @warn, @_ };
44 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
45 ok($im->add($mime), 'added duplicate-but-different message');
46 is(scalar(@warn), 1, 'got one warning');
47 my $mids = mids($mime->header_obj);
48 $new_mid = $mids->[1];
49 $im->done;
50
51 my $msg = $ibx->msg_by_mid('a-mid@b');
52 like($$msg, qr/\AFrom oldbug/s,
53         '"From_" line stored to test old bug workaround');
54
55 my $cfgpfx = "publicinbox.v2test";
56 my $cfg = <<EOF;
57 $cfgpfx.address=$ibx->{-primary_address}
58 $cfgpfx.inboxdir=$inboxdir
59 EOF
60 my $config = PublicInbox::Config->new(\$cfg);
61 my $www = PublicInbox::WWW->new($config);
62 my ($res, $raw, @from_);
63 test_psgi(sub { $www->call(@_) }, sub {
64         my ($cb) = @_;
65         $res = $cb->(GET('/v2test/description'));
66         like($res->content, qr!\$INBOX_DIR/description missing!,
67                 'got v2 description missing message');
68         $res = $cb->(GET('/v2test/a-mid@b/raw'));
69         $raw = $res->content;
70         unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
71         like($raw, qr/^hello world$/m, 'got first message');
72         like($raw, qr/^hello world!$/m, 'got second message');
73         @from_ = ($raw =~ m/^From /mg);
74         is(scalar(@from_), 2, 'two From_ lines');
75
76         $res = $cb->(GET("/v2test/$new_mid/raw"));
77         $raw = $res->content;
78         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
79         @from_ = ($raw =~ m/^From /mg);
80         is(scalar(@from_), 1, 'only one From_ line');
81
82         # Atom feed should sort by Date: (if Received is missing)
83         $res = $cb->(GET('/v2test/new.atom'));
84         my @bodies = ($res->content =~ />(hello [^<]+)</mg);
85         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
86                 'Atom ordering is chronological');
87
88         # new.html should sort by Date:, too (if Received is missing)
89         $res = $cb->(GET('/v2test/new.html'));
90         @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
91         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
92                 'new.html ordering is chronological');
93 });
94
95 $mime->header_set('Message-Id', 'a-mid@b');
96 $mime->body_set("hello ghosts\n");
97 ok($im->add($mime), 'added 3rd duplicate-but-different message');
98 is(scalar(@warn), 2, 'got another warning');
99 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
100 is($warn[0], $warn[1], 'both warnings are the same');
101
102 $mids = mids($mime->header_obj);
103 my $third = $mids->[-1];
104 $im->done;
105
106 my $client = sub {
107         my ($cb) = @_;
108         $res = $cb->(GET("/v2test/$third/raw"));
109         $raw = $res->content;
110         like($raw, qr/^hello ghosts$/m, 'got third message');
111         @from_ = ($raw =~ m/^From /mg);
112         is(scalar(@from_), 1, 'one From_ line');
113
114         $res = $cb->(GET('/v2test/a-mid@b/raw'));
115         $raw = $res->content;
116         like($raw, qr/^hello world$/m, 'got first message');
117         like($raw, qr/^hello world!$/m, 'got second message');
118         like($raw, qr/^hello ghosts$/m, 'got third message');
119         @from_ = ($raw =~ m/^From /mg);
120         is(scalar(@from_), 3, 'three From_ lines');
121         $config->each_inbox(sub { $_[0]->search->reopen });
122
123         SKIP: {
124                 eval { require IO::Uncompress::Gunzip };
125                 skip 'IO::Uncompress::Gunzip missing', 6 if $@;
126                 my ($in, $out, $status);
127                 my $req = GET('/v2test/a-mid@b/raw');
128                 $req->header('Accept-Encoding' => 'gzip');
129                 $res = $cb->($req);
130                 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
131                 $in = $res->content;
132                 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
133                 is($out, $raw, 'gzip response matches');
134
135                 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
136                 $in = $res->content;
137                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
138                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
139                 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
140                 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
141                 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
142                 @from_ = ($out =~ m/^From /mg);
143                 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
144
145                 # search interface
146                 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
147                 $in = $res->content;
148                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
149                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
150                 like($out, qr/^hello world$/m, 'got first in mbox POST');
151                 like($out, qr/^hello world!$/m, 'got second in mbox POST');
152                 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
153                 @from_ = ($out =~ m/^From /mg);
154                 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
155
156                 # all.mbox.gz interface
157                 $res = $cb->(GET('/v2test/all.mbox.gz'));
158                 $in = $res->content;
159                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
160                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
161                 like($out, qr/^hello world$/m, 'got first in all.mbox');
162                 like($out, qr/^hello world!$/m, 'got second in all.mbox');
163                 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
164                 @from_ = ($out =~ m/^From /mg);
165                 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
166         };
167
168         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
169         is($res->code, 200, 'success with threaded search');
170         my $raw = $res->content;
171         ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
172         my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
173         is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
174
175         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
176         is($res->code, 200, 'success with Atom search');
177         SKIP: {
178                 require_mods(qw(XML::TreePP), 2);
179                 my $t = XML::TreePP->new->parse($res->content);
180                 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
181                         'looks like an an Atom feed');
182                 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
183         };
184
185         local $SIG{__WARN__} = 'DEFAULT';
186         $res = $cb->(GET('/v2test/a-mid@b/'));
187         $raw = $res->content;
188         like($raw, qr/^hello world$/m, 'got first message');
189         like($raw, qr/^hello world!$/m, 'got second message');
190         like($raw, qr/^hello ghosts$/m, 'got third message');
191         @from_ = ($raw =~ m/>From: /mg);
192         is(scalar(@from_), 3, 'three From: lines');
193         foreach my $mid ('a-mid@b', $new_mid, $third) {
194                 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
195         }
196         like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
197 };
198
199 test_psgi(sub { $www->call(@_) }, $client);
200 SKIP: {
201         require_mods(qw(Plack::Test::ExternalServer), 37);
202         my $cfgpath = "$inboxdir/$$.config";
203         open my $fh, '>', $cfgpath or BAIL_OUT $!;
204         print $fh <<EOF or BAIL_OUT $!;
205 [publicinbox "v2test"]
206         inboxdir = $inboxdir
207         address = test\@example.com
208 EOF
209         close $fh or BAIL_OUT $!;
210         my $env = { PI_CONFIG => $cfgpath };
211         my $sock = tcp_server() or die;
212         my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
213         my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
214         my $td = start_script($cmd, $env, { 3 => $sock });
215         my ($h, $p) = ($sock->sockhost, $sock->sockport);
216         local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
217         Plack::Test::ExternalServer::test_psgi(client => $client);
218         $td->join('TERM');
219         open $fh, '<', $err or BAIL_OUT $!;
220         is(do { local $/; <$fh> }, '', 'no errors');
221 };
222
223 test_psgi(sub { $www->call(@_) }, sub {
224         my ($cb) = @_;
225         my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
226         $mime->header_set('Message-Id', @$exp);
227         $mime->header_set('Subject', '4th dupe');
228         local $SIG{__WARN__} = sub {};
229         ok($im->add($mime), 'added one message');
230         $im->done;
231         my @h = $mime->header('Message-ID');
232         is_deeply($exp, \@h, 'reused existing Message-ID');
233
234         $config->each_inbox(sub { $_[0]->search->reopen });
235
236         $res = $cb->(GET('/v2test/new.atom'));
237         my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
238         my %ids;
239         $ids{$_}++ for @ids;
240         is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
241
242         $res = $cb->(GET('/v2test/reuse@mid/T/'));
243         $raw = $res->content;
244         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
245         my @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
246         is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
247                 'duplicate messages share the same root');
248
249         $res = $cb->(GET('/v2test/reuse@mid/t/'));
250         $raw = $res->content;
251         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
252
253         $res = $cb->(GET('/v2test/0/info/refs'));
254         is($res->code, 200, 'got info refs for dumb clones');
255         $res = $cb->(GET('/v2test/0.git/info/refs'));
256         is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
257         $res = $cb->(GET('/v2test/info/refs'));
258         is($res->code, 404, 'v2 git URL w/o shard fails');
259
260         # ensure conflicted attachments can be resolved
261         foreach my $body (qw(old new)) {
262                 $mime = eml_load "t/psgi_v2-$body.eml";
263                 ok($im->add($mime), "added attachment $body");
264         }
265         $im->done;
266         $config->each_inbox(sub { $_[0]->search->reopen });
267         $res = $cb->(GET('/v2test/a@dup/'));
268         my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
269         is(scalar(@links), 2, 'both attachment links exist');
270         isnt($links[0], $links[1], 'attachment links are different');
271         {
272                 my $old = $cb->(GET('/v2test/' . $links[0]));
273                 my $new = $cb->(GET('/v2test/' . $links[1]));
274                 is($old->content, 'old', 'got expected old content');
275                 is($new->content, 'new', 'got expected new content');
276         }
277         $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
278         is($res->code, 404, '404 for out-of-range t= param');
279 });
280
281 done_testing();
282
283 1;