]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
31c4178bd06aafb89243aba0f3d361d85f571ad6
[public-inbox.git] / t / psgi_v2.t
1 # Copyright (C) 2018 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 File::Temp qw/tempdir/;
7 use PublicInbox::MIME;
8 use PublicInbox::Config;
9 use PublicInbox::WWW;
10 use PublicInbox::MID qw(mids);
11 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12                 URI::Escape Plack::Builder);
13 foreach my $mod (@mods) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
16 }
17 use_ok($_) for @mods;
18 use_ok 'PublicInbox::V2Writable';
19 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 my $ibx = {
21         mainrepo => $mainrepo,
22         name => 'test-v2writable',
23         version => 2,
24         -primary_address => 'test@example.com',
25 };
26 $ibx = PublicInbox::Inbox->new($ibx);
27 my $new_mid;
28
29 my $im = PublicInbox::V2Writable->new($ibx, 1);
30 $im->{parallel} = 0;
31
32 my $mime = PublicInbox::MIME->create(
33         header => [
34                 From => 'a@example.com',
35                 To => 'test@example.com',
36                 Subject => 'this is a subject',
37                 'Message-ID' => '<a-mid@b>',
38                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
39         ],
40         body => "hello world\n",
41 );
42 ok($im->add($mime), 'added one message');
43 $mime->body_set("hello world!\n");
44
45 my @warn;
46 local $SIG{__WARN__} = sub { push @warn, @_ };
47 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
48 ok($im->add($mime), 'added duplicate-but-different message');
49 is(scalar(@warn), 1, 'got one warning');
50 my $mids = mids($mime->header_obj);
51 $new_mid = $mids->[1];
52 $im->done;
53
54 my $cfgpfx = "publicinbox.v2test";
55 my $cfg = {
56         "$cfgpfx.address" => $ibx->{-primary_address},
57         "$cfgpfx.mainrepo" => $mainrepo,
58 };
59 my $config = PublicInbox::Config->new($cfg);
60 my $www = PublicInbox::WWW->new($config);
61 my ($res, $raw, @from_);
62 test_psgi(sub { $www->call(@_) }, sub {
63         my ($cb) = @_;
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
117         SKIP: {
118                 eval { require IO::Uncompress::Gunzip };
119                 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
120
121                 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
122                 my $out;
123                 my $in = $res->content;
124                 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
125                 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
126                 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
127                 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
128                 @from_ = ($raw =~ m/^From /mg);
129                 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
130         };
131
132         local $SIG{__WARN__} = 'DEFAULT';
133         $res = $cb->(GET('/v2test/a-mid@b/'));
134         $raw = $res->content;
135         like($raw, qr/^hello world$/m, 'got first message');
136         like($raw, qr/^hello world!$/m, 'got second message');
137         like($raw, qr/^hello ghosts$/m, 'got third message');
138         @from_ = ($raw =~ m/>From: /mg);
139         is(scalar(@from_), 3, 'three From: lines');
140         foreach my $mid ('a-mid@b', $new_mid, $third) {
141                 like($raw, qr/&lt;\Q$mid\E&gt;/s, "Message-ID $mid shown");
142         }
143         like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
144
145         my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
146         $mime->header_set('Message-Id', @$exp);
147         $mime->header_set('Subject', '4th dupe');
148         local $SIG{__WARN__} = sub {};
149         ok($im->add($mime), 'added one message');
150         $im->done;
151         my @h = $mime->header('Message-ID');
152         is_deeply($exp, \@h, 'reused existing Message-ID');
153
154         $config->each_inbox(sub { $_[0]->search->reopen });
155
156         $res = $cb->(GET('/v2test/new.atom'));
157         my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
158         my %ids;
159         $ids{$_}++ for @ids;
160         is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
161
162         $res = $cb->(GET('/v2test/reuse@mid/T/'));
163         $raw = $res->content;
164         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
165
166         $res = $cb->(GET('/v2test/reuse@mid/t/'));
167         $raw = $res->content;
168         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
169
170         $res = $cb->(GET('/v2test/0/info/refs'));
171         is($res->code, 200, 'got info refs for dumb clones');
172         $res = $cb->(GET('/v2test/info/refs'));
173         is($res->code, 404, 'unpartitioned git URL fails');
174
175         # ensure conflicted attachments can be resolved
176         foreach my $body (qw(old new)) {
177                 my $parts = [
178                         PublicInbox::MIME->create(
179                                 attributes => { content_type => 'text/plain' },
180                                 body => 'blah',
181                         ),
182                         PublicInbox::MIME->create(
183                                 attributes => {
184                                         filename => 'attach.txt',
185                                         content_type => 'text/plain',
186                                 },
187                                 body => $body
188                         )
189                 ];
190                 $mime = PublicInbox::MIME->create(
191                         parts => $parts,
192                         header_str => [ From => 'root@z',
193                                 'Message-ID' => '<a@dup>',
194                                 Subject => 'hi']
195                 );
196                 ok($im->add($mime), "added attachment $body");
197         }
198         $im->done;
199         $config->each_inbox(sub { $_[0]->search->reopen });
200         $res = $cb->(GET('/v2test/a@dup/'));
201         my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
202         is(scalar(@links), 2, 'both attachment links exist');
203         isnt($links[0], $links[1], 'attachment links are different');
204         {
205                 my $old = $cb->(GET('/v2test/' . $links[0]));
206                 my $new = $cb->(GET('/v2test/' . $links[1]));
207                 is($old->content, 'old', 'got expected old content');
208                 is($new->content, 'new', 'got expected new content');
209         }
210 });
211
212 done_testing();
213
214 1;