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 require_mods(qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
13 URI::Escape Plack::Builder));
14 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
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->create(
31 From => 'a@example.com',
32 To => 'test@example.com',
33 Subject => 'this is a subject',
34 'Message-ID' => '<a-mid@b>',
35 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
37 body => "hello world\n",
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 $cfgpfx = "publicinbox.v2test";
53 $cfgpfx.address=$ibx->{-primary_address}
54 $cfgpfx.inboxdir=$inboxdir
56 my $config = PublicInbox::Config->new(\$cfg);
57 my $www = PublicInbox::WWW->new($config);
58 my ($res, $raw, @from_);
59 test_psgi(sub { $www->call(@_) }, sub {
61 $res = $cb->(GET('/v2test/a-mid@b/raw'));
63 like($raw, qr/^hello world$/m, 'got first message');
64 like($raw, qr/^hello world!$/m, 'got second message');
65 @from_ = ($raw =~ m/^From /mg);
66 is(scalar(@from_), 2, 'two From_ lines');
68 $res = $cb->(GET("/v2test/$new_mid/raw"));
70 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
71 @from_ = ($raw =~ m/^From /mg);
72 is(scalar(@from_), 1, 'only one From_ line');
74 # Atom feed should sort by Date: (if Received is missing)
75 $res = $cb->(GET('/v2test/new.atom'));
76 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
77 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
78 'Atom ordering is chronological');
80 # new.html should sort by Date:, too (if Received is missing)
81 $res = $cb->(GET('/v2test/new.html'));
82 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
83 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
84 'new.html ordering is chronological');
87 $mime->header_set('Message-Id', 'a-mid@b');
88 $mime->body_set("hello ghosts\n");
89 ok($im->add($mime), 'added 3rd duplicate-but-different message');
90 is(scalar(@warn), 2, 'got another warning');
91 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
92 is($warn[0], $warn[1], 'both warnings are the same');
94 $mids = mids($mime->header_obj);
95 my $third = $mids->[-1];
98 test_psgi(sub { $www->call(@_) }, sub {
100 $res = $cb->(GET("/v2test/$third/raw"));
101 $raw = $res->content;
102 like($raw, qr/^hello ghosts$/m, 'got third message');
103 @from_ = ($raw =~ m/^From /mg);
104 is(scalar(@from_), 1, 'one From_ line');
106 $res = $cb->(GET('/v2test/a-mid@b/raw'));
107 $raw = $res->content;
108 like($raw, qr/^hello world$/m, 'got first message');
109 like($raw, qr/^hello world!$/m, 'got second message');
110 like($raw, qr/^hello ghosts$/m, 'got third message');
111 @from_ = ($raw =~ m/^From /mg);
112 is(scalar(@from_), 3, 'three From_ lines');
113 $config->each_inbox(sub { $_[0]->search->reopen });
116 eval { require IO::Uncompress::Gunzip };
117 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
119 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
121 my $in = $res->content;
122 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
123 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
124 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
125 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
126 @from_ = ($out =~ m/^From /mg);
127 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
130 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
132 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
133 like($out, qr/^hello world$/m, 'got first in mbox POST');
134 like($out, qr/^hello world!$/m, 'got second in mbox POST');
135 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
136 @from_ = ($out =~ m/^From /mg);
137 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
139 # all.mbox.gz interface
140 $res = $cb->(GET('/v2test/all.mbox.gz'));
142 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
143 like($out, qr/^hello world$/m, 'got first in all.mbox');
144 like($out, qr/^hello world!$/m, 'got second in all.mbox');
145 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
146 @from_ = ($out =~ m/^From /mg);
147 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
150 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
151 is($res->code, 200, 'success with threaded search');
152 my $raw = $res->content;
153 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
154 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
155 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
157 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=A'));
158 is($res->code, 200, 'success with Atom search');
160 require_mods(qw(XML::Feed), 2);
161 $raw = $res->content;
162 my $p = XML::Feed->parse(\$raw);
163 is($p->format, "Atom", "parsed atom feed");
164 is(scalar $p->entries, 3, "parsed three entries");
167 local $SIG{__WARN__} = 'DEFAULT';
168 $res = $cb->(GET('/v2test/a-mid@b/'));
169 $raw = $res->content;
170 like($raw, qr/^hello world$/m, 'got first message');
171 like($raw, qr/^hello world!$/m, 'got second message');
172 like($raw, qr/^hello ghosts$/m, 'got third message');
173 @from_ = ($raw =~ m/>From: /mg);
174 is(scalar(@from_), 3, 'three From: lines');
175 foreach my $mid ('a-mid@b', $new_mid, $third) {
176 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
178 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
180 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
181 $mime->header_set('Message-Id', @$exp);
182 $mime->header_set('Subject', '4th dupe');
183 local $SIG{__WARN__} = sub {};
184 ok($im->add($mime), 'added one message');
186 my @h = $mime->header('Message-ID');
187 is_deeply($exp, \@h, 'reused existing Message-ID');
189 $config->each_inbox(sub { $_[0]->search->reopen });
191 $res = $cb->(GET('/v2test/new.atom'));
192 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
195 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
197 $res = $cb->(GET('/v2test/reuse@mid/T/'));
198 $raw = $res->content;
199 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
200 @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
201 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
202 'duplicate messages share the same root');
204 $res = $cb->(GET('/v2test/reuse@mid/t/'));
205 $raw = $res->content;
206 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
208 $res = $cb->(GET('/v2test/0/info/refs'));
209 is($res->code, 200, 'got info refs for dumb clones');
210 $res = $cb->(GET('/v2test/0.git/info/refs'));
211 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
212 $res = $cb->(GET('/v2test/info/refs'));
213 is($res->code, 404, 'v2 git URL w/o shard fails');
215 # ensure conflicted attachments can be resolved
216 foreach my $body (qw(old new)) {
218 PublicInbox::MIME->create(
219 attributes => { content_type => 'text/plain' },
222 PublicInbox::MIME->create(
224 filename => 'attach.txt',
225 content_type => 'text/plain',
230 $mime = PublicInbox::MIME->create(
232 header_str => [ From => 'root@z',
233 'Message-ID' => '<a@dup>',
236 ok($im->add($mime), "added attachment $body");
239 $config->each_inbox(sub { $_[0]->search->reopen });
240 $res = $cb->(GET('/v2test/a@dup/'));
241 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
242 is(scalar(@links), 2, 'both attachment links exist');
243 isnt($links[0], $links[1], 'attachment links are different');
245 my $old = $cb->(GET('/v2test/' . $links[0]));
246 my $new = $cb->(GET('/v2test/' . $links[1]));
247 is($old->content, 'old', 'got expected old content');
248 is($new->content, 'new', 'got expected new content');