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