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