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