]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
2a798d6fe40e373b935e06d14bd9ea6a0b0fff3c
[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 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
11                 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
15 }
16 use_ok($_) for @mods;
17 use_ok 'PublicInbox::V2Writable';
18 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $ibx = {
20         mainrepo => $mainrepo,
21         name => 'test-v2writable',
22         version => 2,
23         -primary_address => 'test@example.com',
24 };
25 $ibx = PublicInbox::Inbox->new($ibx);
26 my $new_mid;
27
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 $im->{parallel} = 0;
30
31 my $mime = PublicInbox::MIME->create(
32         header => [
33                 From => 'a@example.com',
34                 To => 'test@example.com',
35                 Subject => 'this is a subject',
36                 'Message-ID' => '<a-mid@b>',
37                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
38         ],
39         body => "hello world\n",
40 );
41 ok($im->add($mime), 'added one message');
42 $mime->body_set("hello world!\n");
43
44 my @warn;
45 local $SIG{__WARN__} = sub { push @warn, @_ };
46 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
47 ok($im->add($mime), 'added duplicate-but-different message');
48 is(scalar(@warn), 1, 'got one warning');
49 my @mids = $mime->header_obj->header_raw('Message-Id');
50 $new_mid = PublicInbox::MID::mid_clean($mids[0]);
51 $im->done;
52
53 my $cfgpfx = "publicinbox.v2test";
54 my $cfg = {
55         "$cfgpfx.address" => $ibx->{-primary_address},
56         "$cfgpfx.mainrepo" => $mainrepo,
57 };
58 my $config = PublicInbox::Config->new($cfg);
59 my $www = PublicInbox::WWW->new($config);
60 my ($res, $raw, @from_);
61 test_psgi(sub { $www->call(@_) }, sub {
62         my ($cb) = @_;
63         $res = $cb->(GET('/v2test/a-mid@b/raw'));
64         $raw = $res->content;
65         like($raw, qr/^hello world$/m, 'got first message');
66         like($raw, qr/^hello world!$/m, 'got second message');
67         @from_ = ($raw =~ m/^From /mg);
68         is(scalar(@from_), 2, 'two From_ lines');
69
70         $res = $cb->(GET("/v2test/$new_mid/raw"));
71         $raw = $res->content;
72         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
73         @from_ = ($raw =~ m/^From /mg);
74         is(scalar(@from_), 1, 'only one From_ line');
75
76         # Atom feed should sort by Date: (if Received is missing)
77         $res = $cb->(GET('/v2test/new.atom'));
78         my @bodies = ($res->content =~ />(hello [^<]+)</mg);
79         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
80                 'Atom ordering is chronological');
81
82         # new.html should sort by Date:, too (if Received is missing)
83         $res = $cb->(GET('/v2test/new.html'));
84         @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
85         is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
86                 'new.html ordering is chronological');
87 });
88
89 $mime->header_set('Message-Id', 'a-mid@b');
90 $mime->body_set("hello ghosts\n");
91 ok($im->add($mime), 'added 3rd duplicate-but-different message');
92 is(scalar(@warn), 2, 'got another warning');
93 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
94 is($warn[0], $warn[1], 'both warnings are the same');
95
96 @mids = $mime->header_obj->header_raw('Message-Id');
97 my $third = PublicInbox::MID::mid_clean($mids[0]);
98 $im->done;
99
100 test_psgi(sub { $www->call(@_) }, sub {
101         my ($cb) = @_;
102         $res = $cb->(GET("/v2test/$third/raw"));
103         $raw = $res->content;
104         like($raw, qr/^hello ghosts$/m, 'got third message');
105         @from_ = ($raw =~ m/^From /mg);
106         is(scalar(@from_), 1, 'one From_ line');
107
108         $res = $cb->(GET('/v2test/a-mid@b/raw'));
109         $raw = $res->content;
110         like($raw, qr/^hello world$/m, 'got first message');
111         like($raw, qr/^hello world!$/m, 'got second message');
112         like($raw, qr/^hello ghosts$/m, 'got third message');
113         @from_ = ($raw =~ m/^From /mg);
114         is(scalar(@from_), 3, 'three From_ lines');
115
116         SKIP: {
117                 eval { require IO::Uncompress::Gunzip };
118                 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
119
120                 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
121                 my $out;
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_ = ($raw =~ m/^From /mg);
128                 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
129         };
130
131         local $SIG{__WARN__} = 'DEFAULT';
132         $res = $cb->(GET('/v2test/a-mid@b/'));
133         $raw = $res->content;
134         like($raw, qr/^hello world$/m, 'got first message');
135         like($raw, qr/^hello world!$/m, 'got second message');
136         like($raw, qr/^hello ghosts$/m, 'got third message');
137         @from_ = ($raw =~ m/>From: /mg);
138         is(scalar(@from_), 3, 'three From: lines');
139         foreach my $mid ('a-mid@b', $new_mid, $third) {
140                 like($raw, qr/&lt;\Q$mid\E&gt;/s, "Message-ID $mid shown");
141         }
142         like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
143
144         my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
145         $mime->header_set('Message-Id', @$exp);
146         $mime->header_set('Subject', '4th dupe');
147         local $SIG{__WARN__} = sub {};
148         ok($im->add($mime), 'added one message');
149         $im->done;
150         my @h = $mime->header('Message-ID');
151         is_deeply($exp, \@h, 'reused existing Message-ID');
152
153         $config->each_inbox(sub { $_[0]->search->reopen });
154
155         $res = $cb->(GET('/v2test/new.atom'));
156         my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
157         my %ids;
158         $ids{$_}++ for @ids;
159         is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
160
161         $res = $cb->(GET('/v2test/reuse@mid/T/'));
162         $raw = $res->content;
163         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
164
165         $res = $cb->(GET('/v2test/reuse@mid/t/'));
166         $raw = $res->content;
167         like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
168 });
169
170 done_testing();
171
172 1;