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