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>
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) = ($sock->sockhost, $sock->sockport);
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 is(do { local $/; <$fh> }, '', 'no errors');
48 inboxdir => $inboxdir,
49 name => 'test-v2writable',
51 -primary_address => 'test@example.com',
53 $ibx = PublicInbox::Inbox->new($ibx);
56 my $im = PublicInbox::V2Writable->new($ibx, 1);
59 my $mime = PublicInbox::Eml->new(<<'EOF');
60 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct 2 00:00:00 1993
63 Subject: this is a subject
65 Date: Fri, 02 Oct 1993 00:00:00 +0000
69 ok($im->add($mime), 'added one message');
70 $mime->body_set("hello world!\n");
73 local $SIG{__WARN__} = sub { push @warn, @_ };
74 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
75 ok($im->add($mime), 'added duplicate-but-different message');
76 is(scalar(@warn), 1, 'got one warning');
77 my $mids = mids($mime->header_obj);
78 $new_mid = $mids->[1];
81 my $msg = $ibx->msg_by_mid('a-mid@b');
82 like($$msg, qr/\AFrom oldbug/s,
83 '"From_" line stored to test old bug workaround');
85 my $cfgpfx = "publicinbox.v2test";
86 my $cfg = PublicInbox::Config->new(\<<EOF);
87 $cfgpfx.address=$ibx->{-primary_address}
88 $cfgpfx.inboxdir=$inboxdir
90 my $www = PublicInbox::WWW->new($cfg);
91 my ($res, $raw, @from_);
94 $res = $cb->(GET('/v2test/description'));
95 like($res->content, qr!\$INBOX_DIR/description missing!,
96 'got v2 description missing message');
97 $res = $cb->(GET('/v2test/a-mid@b/raw'));
99 unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
100 like($raw, qr/^hello world$/m, 'got first message');
101 like($raw, qr/^hello world!$/m, 'got second message');
102 @from_ = ($raw =~ m/^From /mg);
103 is(scalar(@from_), 2, 'two From_ lines');
105 $res = $cb->(GET("/v2test/$new_mid/raw"));
106 $raw = $res->content;
107 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
108 @from_ = ($raw =~ m/^From /mg);
109 is(scalar(@from_), 1, 'only one From_ line');
111 # Atom feed should sort by Date: (if Received is missing)
112 $res = $cb->(GET('/v2test/new.atom'));
113 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
114 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
115 'Atom ordering is chronological');
117 # new.html should sort by Date:, too (if Received is missing)
118 $res = $cb->(GET('/v2test/new.html'));
119 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
120 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
121 'new.html ordering is chronological');
123 test_psgi(sub { $www->call(@_) }, $client0);
124 $run_httpd->($client0, 9);
126 $mime->header_set('Message-Id', 'a-mid@b');
127 $mime->body_set("hello ghosts\n");
128 ok($im->add($mime), 'added 3rd duplicate-but-different message');
129 is(scalar(@warn), 2, 'got another warning');
130 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
131 is($warn[0], $warn[1], 'both warnings are the same');
133 $mids = mids($mime->header_obj);
134 my $third = $mids->[-1];
139 $res = $cb->(GET("/v2test/$third/raw"));
140 $raw = $res->content;
141 like($raw, qr/^hello ghosts$/m, 'got third message');
142 @from_ = ($raw =~ m/^From /mg);
143 is(scalar(@from_), 1, 'one From_ line');
145 $res = $cb->(GET('/v2test/a-mid@b/raw'));
146 $raw = $res->content;
147 like($raw, qr/^hello world$/m, 'got first message');
148 like($raw, qr/^hello world!$/m, 'got second message');
149 like($raw, qr/^hello ghosts$/m, 'got third message');
150 @from_ = ($raw =~ m/^From /mg);
151 is(scalar(@from_), 3, 'three From_ lines');
152 $cfg->each_inbox(sub { $_[0]->search->reopen });
155 eval { require IO::Uncompress::Gunzip };
156 skip 'IO::Uncompress::Gunzip missing', 6 if $@;
157 my ($in, $out, $status);
158 my $req = GET('/v2test/a-mid@b/raw');
159 $req->header('Accept-Encoding' => 'gzip');
161 is($res->header('Content-Encoding'), 'gzip', 'gzip encoding');
163 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
164 is($out, $raw, 'gzip response matches');
166 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
168 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
169 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
170 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
171 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
172 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
173 @from_ = ($out =~ m/^From /mg);
174 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
177 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
179 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
180 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
181 like($out, qr/^hello world$/m, 'got first in mbox POST');
182 like($out, qr/^hello world!$/m, 'got second in mbox POST');
183 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
184 @from_ = ($out =~ m/^From /mg);
185 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
187 # all.mbox.gz interface
188 $res = $cb->(GET('/v2test/all.mbox.gz'));
190 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
191 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
192 like($out, qr/^hello world$/m, 'got first in all.mbox');
193 like($out, qr/^hello world!$/m, 'got second in all.mbox');
194 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
195 @from_ = ($out =~ m/^From /mg);
196 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
199 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
200 is($res->code, 200, 'success with threaded search');
201 my $raw = $res->content;
202 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
203 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
204 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
206 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
207 is($res->code, 200, 'success with Atom search');
209 require_mods(qw(XML::TreePP), 2);
210 my $t = XML::TreePP->new->parse($res->content);
211 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
212 'looks like an an Atom feed');
213 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
216 local $SIG{__WARN__} = 'DEFAULT';
217 $res = $cb->(GET('/v2test/a-mid@b/'));
218 $raw = $res->content;
219 like($raw, qr/^hello world$/m, 'got first message');
220 like($raw, qr/^hello world!$/m, 'got second message');
221 like($raw, qr/^hello ghosts$/m, 'got third message');
222 @from_ = ($raw =~ m/>From: /mg);
223 is(scalar(@from_), 3, 'three From: lines');
224 foreach my $mid ('a-mid@b', $new_mid, $third) {
225 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
227 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
230 test_psgi(sub { $www->call(@_) }, $client1);
231 $run_httpd->($client1, 38);
234 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
235 $mime->header_set('Message-Id', @$exp);
236 $mime->header_set('Subject', '4th dupe');
237 local $SIG{__WARN__} = sub {};
238 ok($im->add($mime), 'added one message');
240 my @h = $mime->header('Message-ID');
241 is_deeply($exp, \@h, 'reused existing Message-ID');
242 $cfg->each_inbox(sub { $_[0]->search->reopen });
247 my $res = $cb->(GET('/v2test/new.atom'));
248 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
251 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
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 my @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
257 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
258 'duplicate messages share the same root');
260 $res = $cb->(GET('/v2test/reuse@mid/t/'));
261 $raw = $res->content;
262 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
264 $res = $cb->(GET('/v2test/0/info/refs'));
265 is($res->code, 200, 'got info refs for dumb clones');
266 $res = $cb->(GET('/v2test/0.git/info/refs'));
267 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
268 $res = $cb->(GET('/v2test/info/refs'));
269 is($res->code, 404, 'v2 git URL w/o shard fails');
272 test_psgi(sub { $www->call(@_) }, $client2);
273 $run_httpd->($client2, 8);
275 # ensure conflicted attachments can be resolved
276 foreach my $body (qw(old new)) {
277 $mime = eml_load "t/psgi_v2-$body.eml";
278 ok($im->add($mime), "added attachment $body");
281 $cfg->each_inbox(sub { $_[0]->search->reopen });
286 my $res = $cb->(GET('/v2test/a@dup/'));
287 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
288 is(scalar(@links), 2, 'both attachment links exist');
289 isnt($links[0], $links[1], 'attachment links are different');
291 my $old = $cb->(GET('/v2test/' . $links[0]));
292 my $new = $cb->(GET('/v2test/' . $links[1]));
293 is($old->content, 'old', 'got expected old content');
294 is($new->content, 'new', 'got expected new content');
296 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
297 is($res->code, 404, '404 for out-of-range t= param');
299 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'));
300 is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
302 test_psgi(sub { $www->call(@_) }, $client3);
303 $run_httpd->($client3, 4);