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