]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
t/psgi_search: fix test around date boundaries
[public-inbox.git] / t / psgi_search.t
1 # Copyright (C) 2017-2021 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 IO::Uncompress::Gunzip qw(gunzip);
7 use PublicInbox::Eml;
8 use PublicInbox::Config;
9 use PublicInbox::Inbox;
10 use PublicInbox::InboxWritable;
11 use bytes (); # only for bytes::length
12 use PublicInbox::TestCommon;
13 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
14                 URI::Escape Plack::Builder);
15 require_mods(@mods);
16 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
17 use_ok 'PublicInbox::WWW';
18 use_ok 'PublicInbox::SearchIdx';
19 my ($tmpdir, $for_destroy) = tmpdir();
20 local $ENV{TZ} = 'UTC';
21
22 my $ibx = PublicInbox::Inbox->new({
23         inboxdir => $tmpdir,
24         address => 'git@vger.kernel.org',
25         name => 'test',
26 });
27 $ibx = PublicInbox::InboxWritable->new($ibx);
28 $ibx->init_inbox(1);
29 my $im = $ibx->importer(0);
30 my $digits = '10010260936330';
31 my $ua = 'Pine.LNX.4.10';
32 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
33
34 # n.b. these headers are not properly RFC2047-encoded
35 my $mime = PublicInbox::Eml->new(<<EOF);
36 Subject: test Ævar
37 Message-ID: <$mid>
38 From: Ævar Arnfjörð Bjarmason <avarab\@example>
39 To: git\@vger.kernel.org
40
41 EOF
42 $im->add($mime);
43
44 $im->add(PublicInbox::Eml->new(<<""));
45 Message-ID: <reply\@asdf>
46 From: replier <r\@example.com>
47 In-Reply-To: <$mid>
48 Subject: mismatch
49
50 $mime = PublicInbox::Eml->new(<<'EOF');
51 Subject:
52 Message-ID: <blank-subject@example.com>
53 From: blank subject <blank-subject@example.com>
54 To: git@vger.kernel.org
55
56 EOF
57 $im->add($mime);
58
59 $mime = PublicInbox::Eml->new(<<'EOF');
60 Message-ID: <no-subject-at-all@example.com>
61 From: no subject at all <no-subject-at-all@example.com>
62 To: git@vger.kernel.org
63
64 EOF
65 $im->add($mime);
66
67 $im->done;
68 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
69
70 my $cfgpfx = "publicinbox.test";
71 my $cfg = PublicInbox::Config->new(\<<EOF);
72 $cfgpfx.address=git\@vger.kernel.org
73 $cfgpfx.inboxdir=$tmpdir
74 EOF
75 my $www = PublicInbox::WWW->new($cfg);
76 test_psgi(sub { $www->call(@_) }, sub {
77         my ($cb) = @_;
78         my ($html, $res);
79         my $approxidate = 'now';
80         for my $req ('/test/?q=%C3%86var', '/test/?q=%25C3%2586var') {
81                 $res = $cb->(GET($req."+d:..$approxidate"));
82                 $html = $res->content;
83                 like($html, qr/<title>&#198;var d:\.\.\Q$approxidate\E/,
84                         'HTML escaped in title, "d:..$APPROXIDATE" preserved');
85                 my @res = ($html =~ m/\?q=(.+var)\+d:\.\.\Q$approxidate\E/g);
86                 ok(scalar(@res), 'saw query strings');
87                 my %uniq = map { $_ => 1 } @res;
88                 is(1, scalar keys %uniq, 'all query values identical in HTML');
89                 is('%C3%86var', (keys %uniq)[0], 'matches original query');
90                 ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason')
91                         >= 0, "displayed Ævar's name properly in HTML");
92                 like($html, qr/download mbox\.gz: .*?"full threads"/s,
93                         '"full threads" download option shown');
94         }
95         like($html, qr/Initial query\b.*?returned no.results, used:.*instead/s,
96                 'noted retry on double-escaped query {-uxs_retried}');
97
98         my $warn = [];
99         local $SIG{__WARN__} = sub { push @$warn, @_ };
100         $res = $cb->(GET('/test/?q=s:test&l=5e'));
101         is($res->code, 200, 'successful search result');
102         is_deeply([], $warn, 'no warnings from non-numeric comparison');
103
104         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
105         is($res->code, 404, 'failed search result gives 404');
106         is_deeply([], $warn, 'no warnings');
107
108         my $mid_re = qr/\Q$mid\E/o;
109         while (length($digits) > 8) {
110                 $res = $cb->(GET("/test/$ua.$digits/"));
111                 is($res->code, 300, 'partial match found while truncated');
112                 like($res->content, qr/\b1 partial match found\b/);
113                 like($res->content, $mid_re, 'found mid in response');
114                 chop($digits);
115         }
116
117         $res = $cb->(GET('/test/'));
118         $html = $res->content;
119         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
120                 'subject-less message linked from "/$INBOX/"');
121         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
122                 'blank subject message linked from "/$INBOX/"');
123         like($html, qr/test &#198;var/,
124                 "displayed Ævar's name properly in topic view");
125
126         $res = $cb->(GET('/test/?q=tc:git'));
127         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
128                 'subject-less message linked from "/$INBOX/?q=..."');
129         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
130                 'blank subject message linked from "/$INBOX/?q=..."');
131         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
132         like($res->header('Content-Disposition'),
133                 qr/filename=no-subject\.txt/);
134         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
135         like($res->header('Content-Disposition'),
136                 qr/filename=no-subject\.mbox\.gz/);
137
138         # "full threads" mbox.gz download
139         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
140         is($res->code, 200, 'successful mbox download with threads');
141         gunzip(\($res->content) => \(my $before));
142         is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
143                 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
144                 'got full thread');
145
146         # clobber has_threadid to emulate old versions:
147         {
148                 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
149                 my $xdb = $sidx->idx_acquire;
150                 $xdb->set_metadata('has_threadid', '0');
151                 $sidx->idx_release;
152         }
153         $cfg->each_inbox(sub { delete $_[0]->{search} });
154         $res = $cb->(GET('/test/?q=s:test'));
155         is($res->code, 200, 'successful search w/o has_threadid');
156         unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
157                 '"full threads" download option not shown w/o has_threadid');
158
159         # in case somebody uses curl to bypass <form>
160         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
161         is($res->code, 200, 'successful mbox download w/ threads');
162         gunzip(\($res->content) => \(my $after));
163         isnt($before, $after);
164 });
165
166 done_testing();