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