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