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