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>
6 use PublicInbox::TestCommon;
9 use PublicInbox::Config;
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) {
16 plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
19 use_ok 'PublicInbox::V2Writable';
20 my ($inboxdir, $for_destroy) = tmpdir();
22 inboxdir => $inboxdir,
23 name => 'test-v2writable',
25 -primary_address => 'test@example.com',
27 $ibx = PublicInbox::Inbox->new($ibx);
30 my $im = PublicInbox::V2Writable->new($ibx, 1);
33 my $mime = PublicInbox::MIME->create(
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',
41 body => "hello world\n",
43 ok($im->add($mime), 'added one message');
44 $mime->body_set("hello world!\n");
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];
55 my $cfgpfx = "publicinbox.v2test";
57 $cfgpfx.address=$ibx->{-primary_address}
58 $cfgpfx.inboxdir=$inboxdir
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 {
65 $res = $cb->(GET('/v2test/a-mid@b/raw'));
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');
72 $res = $cb->(GET("/v2test/$new_mid/raw"));
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');
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');
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');
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');
98 $mids = mids($mime->header_obj);
99 my $third = $mids->[-1];
102 test_psgi(sub { $www->call(@_) }, sub {
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');
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 });
120 eval { require IO::Uncompress::Gunzip };
121 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
123 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
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');
134 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
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');
143 # all.mbox.gz interface
144 $res = $cb->(GET('/v2test/all.mbox.gz'));
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');
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');
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");
172 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
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');
180 my @h = $mime->header('Message-ID');
181 is_deeply($exp, \@h, 'reused existing Message-ID');
183 $config->each_inbox(sub { $_[0]->search->reopen });
185 $res = $cb->(GET('/v2test/new.atom'));
186 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
189 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
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');
198 $res = $cb->(GET('/v2test/reuse@mid/t/'));
199 $raw = $res->content;
200 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
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');
209 # ensure conflicted attachments can be resolved
210 foreach my $body (qw(old new)) {
212 PublicInbox::MIME->create(
213 attributes => { content_type => 'text/plain' },
216 PublicInbox::MIME->create(
218 filename => 'attach.txt',
219 content_type => 'text/plain',
224 $mime = PublicInbox::MIME->create(
226 header_str => [ From => 'root@z',
227 'Message-ID' => '<a@dup>',
230 ok($im->add($mime), "added attachment $body");
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');
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');