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