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