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);
16 use_ok 'PublicInbox::V2Writable';
17 my ($inboxdir, $for_destroy) = tmpdir();
19 inboxdir => $inboxdir,
20 name => 'test-v2writable',
22 -primary_address => 'test@example.com',
24 $ibx = PublicInbox::Inbox->new($ibx);
27 my $im = PublicInbox::V2Writable->new($ibx, 1);
30 my $mime = PublicInbox::MIME->create(
32 From => 'a@example.com',
33 To => 'test@example.com',
34 Subject => 'this is a subject',
35 'Message-ID' => '<a-mid@b>',
36 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
38 body => "hello world\n",
40 ok($im->add($mime), 'added one message');
41 $mime->body_set("hello world!\n");
44 local $SIG{__WARN__} = sub { push @warn, @_ };
45 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
46 ok($im->add($mime), 'added duplicate-but-different message');
47 is(scalar(@warn), 1, 'got one warning');
48 my $mids = mids($mime->header_obj);
49 $new_mid = $mids->[1];
52 my $cfgpfx = "publicinbox.v2test";
54 $cfgpfx.address=$ibx->{-primary_address}
55 $cfgpfx.inboxdir=$inboxdir
57 my $config = PublicInbox::Config->new(\$cfg);
58 my $www = PublicInbox::WWW->new($config);
59 my ($res, $raw, @from_);
60 test_psgi(sub { $www->call(@_) }, sub {
62 $res = $cb->(GET('/v2test/a-mid@b/raw'));
64 like($raw, qr/^hello world$/m, 'got first message');
65 like($raw, qr/^hello world!$/m, 'got second message');
66 @from_ = ($raw =~ m/^From /mg);
67 is(scalar(@from_), 2, 'two From_ lines');
69 $res = $cb->(GET("/v2test/$new_mid/raw"));
71 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
72 @from_ = ($raw =~ m/^From /mg);
73 is(scalar(@from_), 1, 'only one From_ line');
75 # Atom feed should sort by Date: (if Received is missing)
76 $res = $cb->(GET('/v2test/new.atom'));
77 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
78 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
79 'Atom ordering is chronological');
81 # new.html should sort by Date:, too (if Received is missing)
82 $res = $cb->(GET('/v2test/new.html'));
83 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
84 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
85 'new.html ordering is chronological');
88 $mime->header_set('Message-Id', 'a-mid@b');
89 $mime->body_set("hello ghosts\n");
90 ok($im->add($mime), 'added 3rd duplicate-but-different message');
91 is(scalar(@warn), 2, 'got another warning');
92 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
93 is($warn[0], $warn[1], 'both warnings are the same');
95 $mids = mids($mime->header_obj);
96 my $third = $mids->[-1];
99 test_psgi(sub { $www->call(@_) }, sub {
101 $res = $cb->(GET("/v2test/$third/raw"));
102 $raw = $res->content;
103 like($raw, qr/^hello ghosts$/m, 'got third message');
104 @from_ = ($raw =~ m/^From /mg);
105 is(scalar(@from_), 1, 'one From_ line');
107 $res = $cb->(GET('/v2test/a-mid@b/raw'));
108 $raw = $res->content;
109 like($raw, qr/^hello world$/m, 'got first message');
110 like($raw, qr/^hello world!$/m, 'got second message');
111 like($raw, qr/^hello ghosts$/m, 'got third message');
112 @from_ = ($raw =~ m/^From /mg);
113 is(scalar(@from_), 3, 'three From_ lines');
114 $config->each_inbox(sub { $_[0]->search->reopen });
117 eval { require IO::Uncompress::Gunzip };
118 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
120 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
122 my $in = $res->content;
123 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
124 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
125 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
126 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
127 @from_ = ($out =~ m/^From /mg);
128 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
131 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
133 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
134 like($out, qr/^hello world$/m, 'got first in mbox POST');
135 like($out, qr/^hello world!$/m, 'got second in mbox POST');
136 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
137 @from_ = ($out =~ m/^From /mg);
138 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
140 # all.mbox.gz interface
141 $res = $cb->(GET('/v2test/all.mbox.gz'));
143 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
144 like($out, qr/^hello world$/m, 'got first in all.mbox');
145 like($out, qr/^hello world!$/m, 'got second in all.mbox');
146 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
147 @from_ = ($out =~ m/^From /mg);
148 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
151 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
152 is($res->code, 200, 'success with threaded search');
153 my $raw = $res->content;
154 ok($raw =~ s/\A.*>Results 1-3 of 3\b//s, 'got all results');
155 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ +(?:\d+\% )?(.+)$/gm);
156 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
158 local $SIG{__WARN__} = 'DEFAULT';
159 $res = $cb->(GET('/v2test/a-mid@b/'));
160 $raw = $res->content;
161 like($raw, qr/^hello world$/m, 'got first message');
162 like($raw, qr/^hello world!$/m, 'got second message');
163 like($raw, qr/^hello ghosts$/m, 'got third message');
164 @from_ = ($raw =~ m/>From: /mg);
165 is(scalar(@from_), 3, 'three From: lines');
166 foreach my $mid ('a-mid@b', $new_mid, $third) {
167 like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
169 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
171 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
172 $mime->header_set('Message-Id', @$exp);
173 $mime->header_set('Subject', '4th dupe');
174 local $SIG{__WARN__} = sub {};
175 ok($im->add($mime), 'added one message');
177 my @h = $mime->header('Message-ID');
178 is_deeply($exp, \@h, 'reused existing Message-ID');
180 $config->each_inbox(sub { $_[0]->search->reopen });
182 $res = $cb->(GET('/v2test/new.atom'));
183 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
186 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
188 $res = $cb->(GET('/v2test/reuse@mid/T/'));
189 $raw = $res->content;
190 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
191 @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
192 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
193 'duplicate messages share the same root');
195 $res = $cb->(GET('/v2test/reuse@mid/t/'));
196 $raw = $res->content;
197 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
199 $res = $cb->(GET('/v2test/0/info/refs'));
200 is($res->code, 200, 'got info refs for dumb clones');
201 $res = $cb->(GET('/v2test/0.git/info/refs'));
202 is($res->code, 200, 'got info refs for dumb clones w/ .git suffix');
203 $res = $cb->(GET('/v2test/info/refs'));
204 is($res->code, 404, 'v2 git URL w/o shard fails');
206 # ensure conflicted attachments can be resolved
207 foreach my $body (qw(old new)) {
209 PublicInbox::MIME->create(
210 attributes => { content_type => 'text/plain' },
213 PublicInbox::MIME->create(
215 filename => 'attach.txt',
216 content_type => 'text/plain',
221 $mime = PublicInbox::MIME->create(
223 header_str => [ From => 'root@z',
224 'Message-ID' => '<a@dup>',
227 ok($im->add($mime), "added attachment $body");
230 $config->each_inbox(sub { $_[0]->search->reopen });
231 $res = $cb->(GET('/v2test/a@dup/'));
232 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
233 is(scalar(@links), 2, 'both attachment links exist');
234 isnt($links[0], $links[1], 'attachment links are different');
236 my $old = $cb->(GET('/v2test/' . $links[0]));
237 my $new = $cb->(GET('/v2test/' . $links[1]));
238 is($old->content, 'old', 'got expected old content');
239 is($new->content, 'new', 'got expected new content');