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