]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
extmsg: use Xapian only for partial matches
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # FIXME: this test is too slow and most non-CGI-requirements
4 # should be moved over to things which use test_psgi
5 use strict;
6 use warnings;
7 use Test::More;
8 use Email::MIME;
9 use File::Temp qw/tempdir/;
10 use Cwd;
11 eval { require IPC::Run };
12 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
13
14 use constant CGI => "blib/script/public-inbox.cgi";
15 my $index = "blib/script/public-inbox-index";
16 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $home = "$tmpdir/pi-home";
18 my $pi_home = "$home/.public-inbox";
19 my $pi_config = "$pi_home/config";
20 my $maindir = "$tmpdir/main.git";
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
23
24 {
25         is(1, mkdir($home, 0755), "setup ~/ for testing");
26         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
27         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
28
29         open my $fh, '>', "$maindir/description" or die "open: $!\n";
30         print $fh "test for public-inbox\n";
31         close $fh or die "close: $!\n";
32         my %cfg = (
33                 "$cfgpfx.address" => $addr,
34                 "$cfgpfx.mainrepo" => $maindir,
35         );
36         while (my ($k,$v) = each %cfg) {
37                 is(0, system(qw(git config --file), $pi_config, $k, $v),
38                         "setup $k");
39         }
40 }
41
42 use_ok 'PublicInbox::Git';
43 use_ok 'PublicInbox::Import';
44 use_ok 'Email::MIME';
45 my $git = PublicInbox::Git->new($maindir);
46 my $im = PublicInbox::Import->new($git, 'test', $addr);
47
48 {
49         local $ENV{HOME} = $home;
50
51         # ensure successful message delivery
52         {
53                 my $mime = Email::MIME->new(<<EOF);
54 From: Me <me\@example.com>
55 To: You <you\@example.com>
56 Cc: $addr
57 Message-Id: <blah\@example.com>
58 Subject: hihi
59 Date: Thu, 01 Jan 1970 00:00:00 +0000
60
61 zzzzzz
62 EOF
63                 $im->add($mime);
64                 $im->done;
65                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
66                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
67         }
68
69         # deliver a reply, too
70         {
71                 my $reply = Email::MIME->new(<<EOF);
72 From: You <you\@example.com>
73 To: Me <me\@example.com>
74 Cc: $addr
75 In-Reply-To: <blah\@example.com>
76 Message-Id: <blahblah\@example.com>
77 Subject: Re: hihi
78 Date: Thu, 01 Jan 1970 00:00:01 +0000
79
80 Me wrote:
81 > zzzzzz
82
83 what?
84 EOF
85                 $im->add($reply);
86                 $im->done;
87                 my $rev = `git --git-dir=$maindir rev-list HEAD`;
88                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
89         }
90
91 }
92
93 # obvious failures, first
94 {
95         local $ENV{HOME} = $home;
96         my $res = cgi_run("/", "", "PUT");
97         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
98
99         $res = cgi_run("/");
100         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
101 }
102
103 # dumb HTTP support
104 {
105         local $ENV{HOME} = $home;
106         my $path = "/test/info/refs";
107         my $res = cgi_run($path);
108         like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
109         my $orig = $res->{body};
110
111         local $ENV{HTTP_RANGE} = 'bytes=5-10';
112         $res = cgi_run($path);
113         like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
114         is($res->{body}, substr($orig, 5, 6), 'partial body OK');
115
116         local $ENV{HTTP_RANGE} = 'bytes=5-';
117         $res = cgi_run($path);
118         like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
119         is($res->{body}, substr($orig, 5), 'partial body OK past end');
120 }
121 use Data::Dumper;
122 # atom feeds
123 {
124         local $ENV{HOME} = $home;
125         my $res = cgi_run("/test/atom.xml");
126         like($res->{body}, qr/<title>test for public-inbox/,
127                 "set title in XML feed");
128         like($res->{body},
129                 qr!http://test\.example\.com/test/blah\@example\.com/!,
130                 "link id set");
131         like($res->{body}, qr/what\?/, "reply included");
132 }
133
134 # message-id pages
135 {
136         local $ENV{HOME} = $home;
137         my $slashy_mid = 'slashy/asdf@example.com';
138         my $reply = Email::MIME->new(<<EOF);
139 From: You <you\@example.com>
140 To: Me <me\@example.com>
141 Cc: $addr
142 Message-Id: <$slashy_mid>
143 Subject: Re: hihi
144 Date: Thu, 01 Jan 1970 00:00:01 +0000
145
146 slashy
147 EOF
148         $im->add($reply);
149         $im->done;
150
151         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
152         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
153                 "slashy mid raw hit");
154
155         $res = cgi_run("/test/blahblah\@example.com/raw");
156         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
157                 "mid raw hit");
158
159         $res = cgi_run("/test/blahblah\@example.com/");
160         like($res->{body}, qr/\A<html>/, "mid html hit");
161         like($res->{head}, qr/Status: 200 OK/, "200 response");
162
163         $res = cgi_run("/test/blahblah\@example.com/f/");
164         like($res->{head}, qr/Status: 301 Moved/, "301 response");
165         like($res->{head},
166                 qr!^Location: http://[^/]+/test/blahblah\@example\.com/\r\n!ms,
167                 '301 redirect location');
168
169         $res = cgi_run("/test/new.html");
170         like($res->{body}, qr/slashy%2Fasdf\@example\.com/,
171                 "slashy URL generated correctly");
172 }
173
174 # retrieve thread as an mbox
175 {
176         local $ENV{HOME} = $home;
177         my $path = "/test/blahblah\@example.com/t.mbox.gz";
178         my $res = cgi_run($path);
179         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
180         my $indexed = system($index, $maindir) == 0;
181         if ($indexed) {
182                 $res = cgi_run($path);
183                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
184                 eval {
185                         require IO::Uncompress::Gunzip;
186                         my $in = $res->{body};
187                         my $out;
188                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
189                         like($out, qr/^From /m, "From lines in mbox");
190                 };
191         } else {
192                 like($res->{head}, qr/^Status: 501 /, "search not available");
193         }
194
195         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
196         if ($have_xml_feed) {
197                 $path = "/test/blahblah\@example.com/t.atom";
198                 $res = cgi_run($path);
199                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
200                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
201                         "search returned atom");
202                 my $p = XML::Feed->parse(\($res->{body}));
203                 is($p->format, "Atom", "parsed atom feed");
204                 is(scalar $p->entries, 3, "parsed three entries");
205         }
206 }
207
208 # redirect list-name-only URLs
209 {
210         local $ENV{HOME} = $home;
211         my $res = cgi_run("/test");
212         like($res->{head}, qr/Status: 301 Moved/, "redirected status");
213         like($res->{head}, qr!/test/!, "redirected with slash");
214 }
215
216 done_testing();
217
218 sub run_with_env {
219         my ($env, @args) = @_;
220         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
221         IPC::Run::run(@args, init => $init);
222 }
223
224 sub cgi_run {
225         my %env = (
226                 PATH_INFO => $_[0],
227                 QUERY_STRING => $_[1] || "",
228                 REQUEST_METHOD => $_[2] || "GET",
229                 GATEWAY_INTERFACE => 'CGI/1.1',
230                 HTTP_ACCEPT => '*/*',
231                 HTTP_HOST => 'test.example.com',
232         );
233         my ($in, $out, $err) = ("", "", "");
234         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
235         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
236         { head => $head, body => $body, rc => $rc, err => $err }
237 }