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