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