]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
reduce scope of mbox From_ line removal
[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::MIME;
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 $ibx = {
18         inboxdir => $inboxdir,
19         name => 'test-v2writable',
20         version => 2,
21         -primary_address => 'test@example.com',
22 };
23 $ibx = PublicInbox::Inbox->new($ibx);
24 my $new_mid;
25
26 my $im = PublicInbox::V2Writable->new($ibx, 1);
27 $im->{parallel} = 0;
28
29 my $mime = PublicInbox::MIME->new(<<'EOF');
30 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct  2 00:00:00 1993
31 From: a@example.com
32 To: test@example.com
33 Subject: this is a subject
34 Message-ID: <a-mid@b>
35 Date: Fri, 02 Oct 1993 00:00:00 +0000
36
37 hello world
38 EOF
39 ok($im->add($mime), 'added one message');
40 $mime->body_set("hello world!\n");
41
42 my @warn;
43 local $SIG{__WARN__} = sub { push @warn, @_ };
44 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
45 ok($im->add($mime), 'added duplicate-but-different message');
46 is(scalar(@warn), 1, 'got one warning');
47 my $mids = mids($mime->header_obj);
48 $new_mid = $mids->[1];
49 $im->done;
50
51 my $msg = $ibx->msg_by_mid('a-mid@b');
52 like($$msg, qr/\AFrom oldbug/s,
53         '"From_" line stored to test old bug workaround');
54
55 my $cfgpfx = "publicinbox.v2test";
56 my $cfg = <<EOF;
57 $cfgpfx.address=$ibx->{-primary_address}
58 $cfgpfx.inboxdir=$inboxdir
59 EOF
60 my $config = PublicInbox::Config->new(\$cfg);
61 my $www = PublicInbox::WWW->new($config);
62 my ($res, $raw, @from_);
63 test_psgi(sub { $www->call(@_) }, sub {
64         my ($cb) = @_;
65         $res = $cb->(GET('/v2test/description'));
66         like($res->content, qr!\$INBOX_DIR/description missing!,
67                 'got v2 description missing message');
68         $res = $cb->(GET('/v2test/a-mid@b/raw'));
69         $raw = $res->content;
70         unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
71         like($raw, qr/^hello world$/m, 'got first message');
72         like($raw, qr/^hello world!$/m, 'got second message');
73         @from_ = ($raw =~ m/^From /mg);
74         is(scalar(@from_), 2, 'two From_ lines');
75
76         $res = $cb->(GET("/v2test/$new_mid/raw"));
77         $raw = $res->content;
78         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
79         @from_ = ($raw =~ m/^From /mg);
80         is(scalar(@from_), 1, 'only one From_ line');
81
82         # Atom feed should sort by Date: (if Received is missing)
83         $res = $cb->(GET('/v2test/new.atom'));
84         my @bodies = ($res->content =~ />(hello [^<]+)</mg);
85         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
86                 'Atom ordering is chronological');
87
88         # new.html should sort by Date:, too (if Received is missing)
89         $res = $cb->(GET('/v2test/new.html'));
90         @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
91         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
92                 'new.html ordering is chronological');
93 });
94
95 $mime->header_set('Message-Id', 'a-mid@b');
96 $mime->body_set("hello ghosts\n");
97 ok($im->add($mime), 'added 3rd duplicate-but-different message');
98 is(scalar(@warn), 2, 'got another warning');
99 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
100 is($warn[0], $warn[1], 'both warnings are the same');
101
102 $mids = mids($mime->header_obj);
103 my $third = $mids->[-1];
104 $im->done;
105
106 test_psgi(sub { $www->call(@_) }, sub {
107         my ($cb) = @_;
108         $res = $cb->(GET("/v2test/$third/raw"));
109         $raw = $res->content;
110         like($raw, qr/^hello ghosts$/m, 'got third message');
111         @from_ = ($raw =~ m/^From /mg);
112         is(scalar(@from_), 1, 'one From_ line');
113
114         $res = $cb->(GET('/v2test/a-mid@b/raw'));
115         $raw = $res->content;
116         like($raw, qr/^hello world$/m, 'got first message');
117         like($raw, qr/^hello world!$/m, 'got second message');
118         like($raw, qr/^hello ghosts$/m, 'got third message');
119         @from_ = ($raw =~ m/^From /mg);
120         is(scalar(@from_), 3, 'three From_ lines');
121         $config->each_inbox(sub { $_[0]->search->reopen });
122
123         SKIP: {
124                 eval { require IO::Uncompress::Gunzip };
125                 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
126
127                 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
128                 my $out;
129                 my $in = $res->content;
130                 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
131                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
132                 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
133                 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
134                 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
135                 @from_ = ($out =~ m/^From /mg);
136                 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
137
138                 # search interface
139                 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
140                 $in = $res->content;
141                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
142                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
143                 like($out, qr/^hello world$/m, 'got first in mbox POST');
144                 like($out, qr/^hello world!$/m, 'got second in mbox POST');
145                 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
146                 @from_ = ($out =~ m/^From /mg);
147                 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
148
149                 # all.mbox.gz interface
150                 $res = $cb->(GET('/v2test/all.mbox.gz'));
151                 $in = $res->content;
152                 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
153                 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
154                 like($out, qr/^hello world$/m, 'got first in all.mbox');
155                 like($out, qr/^hello world!$/m, 'got second in all.mbox');
156                 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
157                 @from_ = ($out =~ m/^From /mg);
158                 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
159         };
160
161         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
162         is($res->code, 200, 'success with threaded search');
163         my $raw = $res->content;
164         ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
165         my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
166         is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
167
168         $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
169         is($res->code, 200, 'success with Atom search');
170         SKIP: {
171                 require_mods(qw(XML::TreePP), 2);
172                 my $t = XML::TreePP->new->parse($res->content);
173                 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
174                         'looks like an an Atom feed');
175                 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
176         };
177
178         local $SIG{__WARN__} = 'DEFAULT';
179         $res = $cb->(GET('/v2test/a-mid@b/'));
180         $raw = $res->content;
181         like($raw, qr/^hello world$/m, 'got first message');
182         like($raw, qr/^hello world!$/m, 'got second message');
183         like($raw, qr/^hello ghosts$/m, 'got third message');
184         @from_ = ($raw =~ m/>From: /mg);
185         is(scalar(@from_), 3, 'three From: lines');
186         foreach my $mid ('a-mid@b', $new_mid, $third) {
187                 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
188         }
189         like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
190
191         my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
192         $mime->header_set('Message-Id', @$exp);
193         $mime->header_set('Subject', '4th dupe');
194         local $SIG{__WARN__} = sub {};
195         ok($im->add($mime), 'added one message');
196         $im->done;
197         my @h = $mime->header('Message-ID');
198         is_deeply($exp, \@h, 'reused existing Message-ID');
199
200         $config->each_inbox(sub { $_[0]->search->reopen });
201
202         $res = $cb->(GET('/v2test/new.atom'));
203         my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
204         my %ids;
205         $ids{$_}++ for @ids;
206         is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
207
208         $res = $cb->(GET('/v2test/reuse@mid/T/'));
209         $raw = $res->content;
210         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
211         @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
212         is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
213                 'duplicate messages share the same root');
214
215         $res = $cb->(GET('/v2test/reuse@mid/t/'));
216         $raw = $res->content;
217         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
218
219         $res = $cb->(GET('/v2test/0/info/refs'));
220         is($res->code, 200, 'got info refs for dumb clones');
221         $res = $cb->(GET('/v2test/0.git/info/refs'));
222         is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
223         $res = $cb->(GET('/v2test/info/refs'));
224         is($res->code, 404, 'v2 git URL w/o shard fails');
225
226         # ensure conflicted attachments can be resolved
227         foreach my $body (qw(old new)) {
228                 my $parts = [
229                         PublicInbox::MIME->create(
230                                 attributes => { content_type => 'text/plain' },
231                                 body => 'blah',
232                         ),
233                         PublicInbox::MIME->create(
234                                 attributes => {
235                                         filename => 'attach.txt',
236                                         content_type => 'text/plain',
237                                 },
238                                 body => $body
239                         )
240                 ];
241                 $mime = PublicInbox::MIME->create(
242                         parts => $parts,
243                         header_str => [ From => 'root@z',
244                                 'Message-ID' => '<a@dup>',
245                                 Subject => 'hi']
246                 );
247                 ok($im->add($mime), "added attachment $body");
248         }
249         $im->done;
250         $config->each_inbox(sub { $_[0]->search->reopen });
251         $res = $cb->(GET('/v2test/a@dup/'));
252         my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
253         is(scalar(@links), 2, 'both attachment links exist');
254         isnt($links[0], $links[1], 'attachment links are different');
255         {
256                 my $old = $cb->(GET('/v2test/' . $links[0]));
257                 my $new = $cb->(GET('/v2test/' . $links[1]));
258                 is($old->content, 'old', 'got expected old content');
259                 is($new->content, 'new', 'got expected new content');
260         }
261         $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
262         is($res->code, 404, '404 for out-of-range t= param');
263 });
264
265 done_testing();
266
267 1;