1 # Copyright (C) 2018-2020 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;
10 use PublicInbox::MID qw(mids);
11 require_mods(qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12 URI::Escape Plack::Builder Email::MIME));
13 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
14 use_ok 'PublicInbox::WWW';
15 use_ok 'PublicInbox::V2Writable';
16 my ($inboxdir, $for_destroy) = tmpdir();
18 inboxdir => $inboxdir,
19 name => 'test-v2writable',
21 -primary_address => 'test@example.com',
23 $ibx = PublicInbox::Inbox->new($ibx);
26 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 my $mime = PublicInbox::MIME->new(<<'EOF');
30 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct 2 00:00:00 1993
33 Subject: this is a subject
35 Date: Fri, 02 Oct 1993 00:00:00 +0000
39 ok($im->add($mime), 'added one message');
40 $mime->body_set("hello world!\n");
43 local $SIG{__WARN__} = sub { push @warn, @_ };
44 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
45 ok($im->add($mime), 'added duplicate-but-different message');
46 is(scalar(@warn), 1, 'got one warning');
47 my $mids = mids($mime->header_obj);
48 $new_mid = $mids->[1];
51 my $msg = $ibx->msg_by_mid('a-mid@b');
52 like($$msg, qr/\AFrom oldbug/s,
53 '"From_" line stored to test old bug workaround');
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/description'));
66 like($res->content, qr!\$INBOX_DIR/description missing!,
67 'got v2 description missing message');
68 $res = $cb->(GET('/v2test/a-mid@b/raw'));
70 unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
71 like($raw, qr/^hello world$/m, 'got first message');
72 like($raw, qr/^hello world!$/m, 'got second message');
73 @from_ = ($raw =~ m/^From /mg);
74 is(scalar(@from_), 2, 'two From_ lines');
76 $res = $cb->(GET("/v2test/$new_mid/raw"));
78 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
79 @from_ = ($raw =~ m/^From /mg);
80 is(scalar(@from_), 1, 'only one From_ line');
82 # Atom feed should sort by Date: (if Received is missing)
83 $res = $cb->(GET('/v2test/new.atom'));
84 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
85 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
86 'Atom ordering is chronological');
88 # new.html should sort by Date:, too (if Received is missing)
89 $res = $cb->(GET('/v2test/new.html'));
90 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
91 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
92 'new.html ordering is chronological');
95 $mime->header_set('Message-Id', 'a-mid@b');
96 $mime->body_set("hello ghosts\n");
97 ok($im->add($mime), 'added 3rd duplicate-but-different message');
98 is(scalar(@warn), 2, 'got another warning');
99 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
100 is($warn[0], $warn[1], 'both warnings are the same');
102 $mids = mids($mime->header_obj);
103 my $third = $mids->[-1];
106 test_psgi(sub { $www->call(@_) }, sub {
108 $res = $cb->(GET("/v2test/$third/raw"));
109 $raw = $res->content;
110 like($raw, qr/^hello ghosts$/m, 'got third message');
111 @from_ = ($raw =~ m/^From /mg);
112 is(scalar(@from_), 1, 'one From_ line');
114 $res = $cb->(GET('/v2test/a-mid@b/raw'));
115 $raw = $res->content;
116 like($raw, qr/^hello world$/m, 'got first message');
117 like($raw, qr/^hello world!$/m, 'got second message');
118 like($raw, qr/^hello ghosts$/m, 'got third message');
119 @from_ = ($raw =~ m/^From /mg);
120 is(scalar(@from_), 3, 'three From_ lines');
121 $config->each_inbox(sub { $_[0]->search->reopen });
124 eval { require IO::Uncompress::Gunzip };
125 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
127 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
129 my $in = $res->content;
130 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
131 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
132 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
133 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
134 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
135 @from_ = ($out =~ m/^From /mg);
136 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
139 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
141 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
142 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
143 like($out, qr/^hello world$/m, 'got first in mbox POST');
144 like($out, qr/^hello world!$/m, 'got second in mbox POST');
145 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
146 @from_ = ($out =~ m/^From /mg);
147 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
149 # all.mbox.gz interface
150 $res = $cb->(GET('/v2test/all.mbox.gz'));
152 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
153 unlike($out, qr/^From oldbug/sm, 'buggy "From_" line omitted');
154 like($out, qr/^hello world$/m, 'got first in all.mbox');
155 like($out, qr/^hello world!$/m, 'got second in all.mbox');
156 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
157 @from_ = ($out =~ m/^From /mg);
158 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
161 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
162 is($res->code, 200, 'success with threaded search');
163 my $raw = $res->content;
164 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
165 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
166 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
168 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
169 is($res->code, 200, 'success with Atom search');
171 require_mods(qw(XML::TreePP), 2);
172 my $t = XML::TreePP->new->parse($res->content);
173 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
174 'looks like an an Atom feed');
175 is(scalar @{$t->{feed}->{entry}}, 3, 'parsed three entries');
178 local $SIG{__WARN__} = 'DEFAULT';
179 $res = $cb->(GET('/v2test/a-mid@b/'));
180 $raw = $res->content;
181 like($raw, qr/^hello world$/m, 'got first message');
182 like($raw, qr/^hello world!$/m, 'got second message');
183 like($raw, qr/^hello ghosts$/m, 'got third message');
184 @from_ = ($raw =~ m/>From: /mg);
185 is(scalar(@from_), 3, 'three From: lines');
186 foreach my $mid ('a-mid@b', $new_mid, $third) {
187 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
189 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
191 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
192 $mime->header_set('Message-Id', @$exp);
193 $mime->header_set('Subject', '4th dupe');
194 local $SIG{__WARN__} = sub {};
195 ok($im->add($mime), 'added one message');
197 my @h = $mime->header('Message-ID');
198 is_deeply($exp, \@h, 'reused existing Message-ID');
200 $config->each_inbox(sub { $_[0]->search->reopen });
202 $res = $cb->(GET('/v2test/new.atom'));
203 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
206 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
208 $res = $cb->(GET('/v2test/reuse@mid/T/'));
209 $raw = $res->content;
210 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
211 @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
212 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
213 'duplicate messages share the same root');
215 $res = $cb->(GET('/v2test/reuse@mid/t/'));
216 $raw = $res->content;
217 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
219 $res = $cb->(GET('/v2test/0/info/refs'));
220 is($res->code, 200, 'got info refs for dumb clones');
221 $res = $cb->(GET('/v2test/0.git/info/refs'));
222 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
223 $res = $cb->(GET('/v2test/info/refs'));
224 is($res->code, 404, 'v2 git URL w/o shard fails');
226 # ensure conflicted attachments can be resolved
227 foreach my $body (qw(old new)) {
230 attributes => { content_type => 'text/plain' },
235 filename => 'attach.txt',
236 content_type => 'text/plain',
241 $mime = Email::MIME->create(
243 header_str => [ From => 'root@z',
244 'Message-ID' => '<a@dup>',
247 ok($im->add($mime), "added attachment $body");
250 $config->each_inbox(sub { $_[0]->search->reopen });
251 $res = $cb->(GET('/v2test/a@dup/'));
252 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
253 is(scalar(@links), 2, 'both attachment links exist');
254 isnt($links[0], $links[1], 'attachment links are different');
256 my $old = $cb->(GET('/v2test/' . $links[0]));
257 my $new = $cb->(GET('/v2test/' . $links[1]));
258 is($old->content, 'old', 'got expected old content');
259 is($new->content, 'new', 'got expected new content');
261 $res = $cb->(GET('/v2test/?t=1970'.'01'.'01'.'000000'));
262 is($res->code, 404, '404 for out-of-range t= param');