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>
6 use PublicInbox::TestCommon;
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";
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"]
24 address = test\@example.com
26 close $fh or BAIL_OUT $!;
30 my ($client, $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) = tcp_host_port($sock);
39 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
40 Plack::Test::ExternalServer::test_psgi(client => $client);
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;
47 is($e, '', 'no errors');
52 inboxdir => $inboxdir,
53 name => 'test-v2writable',
55 -primary_address => 'test@example.com',
57 $ibx = PublicInbox::Inbox->new($ibx);
60 my $im = PublicInbox::V2Writable->new($ibx, 1);
63 my $mime = PublicInbox::Eml->new(<<'EOF');
64 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct 2 00:00:00 1993
67 Subject: this is a subject
69 Date: Fri, 02 Oct 1993 00:00:00 +0000
73 ok($im->add($mime), 'added one message');
74 $mime->body_set("hello world!\n");
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];
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');
89 my $cfgpfx = "publicinbox.v2test";
90 my $cfg = PublicInbox::Config->new(\<<EOF);
91 $cfgpfx.address=$ibx->{-primary_address}
92 $cfgpfx.inboxdir=$inboxdir
94 my $www = PublicInbox::WWW->new($cfg);
95 my ($res, $raw, @from_);
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');
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');
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');
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');
127 test_psgi(sub { $www->call(@_) }, $client0);
128 $run_httpd->($client0, 9);
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');
137 $mids = mids($mime->header_obj);
138 my $third = $mids->[-1];
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');
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 });
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');
165 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
167 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
168 is($out, $raw, 'gzip response matches');
170 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
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');
181 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
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');
191 # all.mbox.gz interface
192 $res = $cb->(GET('/v2test/all.mbox.gz'));
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');
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');
210 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
211 is($res->code, 200, 'success with Atom search');
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');
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");
231 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
234 test_psgi(sub { $www->call(@_) }, $client1);
235 $run_httpd->($client1, 38);
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');
244 my @h = $mime->header('Message-ID');
245 is_deeply($exp, \@h, 'reused existing Message-ID');
246 $cfg->each_inbox(sub { $_[0]->search->reopen });
251 my $res = $cb->(GET('/v2test/new.atom'));
252 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
255 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
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');
264 $res = $cb->(GET('/v2test/reuse@mid/t/'));
265 $raw = $res->content;
266 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
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');
276 test_psgi(sub { $www->call(@_) }, $client2);
277 $run_httpd->($client2, 8);
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");
285 $cfg->each_inbox(sub { $_[0]->search->reopen });
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');
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');
300 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
301 is($res->code, 404, '404 for out-of-range t= param');
303 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'));
304 is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
306 test_psgi(sub { $www->call(@_) }, $client3);
307 $run_httpd->($client3, 4);