]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
remove GIT_DIR env usage in favor of --git-dir
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use File::Temp qw/tempdir/;
8 use Cwd;
9 use IPC::Run qw/run/;
10
11 use constant CGI => "blib/script/public-inbox.cgi";
12 my $mda = "blib/script/public-inbox-mda";
13 my $index = "blib/script/public-inbox-index";
14 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $home = "$tmpdir/pi-home";
16 my $pi_home = "$home/.public-inbox";
17 my $pi_config = "$pi_home/config";
18 my $maindir = "$tmpdir/main.git";
19 my $main_bin = getcwd()."/t/main-bin";
20 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
23
24 {
25         ok(-x "$main_bin/spamc",
26                 "spamc ham mock found (run in top of source tree");
27         ok(-x $mda, "$mda is executable");
28         is(1, mkdir($home, 0755), "setup ~/ for testing");
29         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
30         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
31
32         open my $fh, '>', "$maindir/description" or die "open: $!\n";
33         print $fh "test for public-inbox\n";
34         close $fh or die "close: $!\n";
35         my %cfg = (
36                 "$cfgpfx.address" => $addr,
37                 "$cfgpfx.mainrepo" => $maindir,
38         );
39         while (my ($k,$v) = each %cfg) {
40                 is(0, system(qw(git config --file), $pi_config, $k, $v),
41                         "setup $k");
42         }
43 }
44
45 my $failbox = "$home/fail.mbox";
46 local $ENV{PI_EMERGENCY} = $failbox;
47 {
48         local $ENV{HOME} = $home;
49         local $ENV{ORIGINAL_RECIPIENT} = $addr;
50
51         # ensure successful message delivery
52         {
53                 my $simple = Email::Simple->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                 my $in = $simple->as_string;
64                 run_with_env({PATH => $main_path}, [$mda], \$in);
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::Simple->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                 my $in = $reply->as_string;
86                 run_with_env({PATH => $main_path}, [$mda], \$in);
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
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%40example\.com/!,
130                 "link id set");
131         like($res->{body}, qr/what\?/, "reply included");
132 }
133
134 # indices
135 {
136         local $ENV{HOME} = $home;
137         my $res = cgi_run("/test/");
138         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
139
140         my $idx = cgi_run("/test/index.html");
141         $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
142         $idx->{body} = [ split(/\n/, $idx->{body}) ];
143         $res->{body} = [ split(/\n/, $res->{body}) ];
144         is_deeply($res, $idx,
145                 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
146         # more checks in t/feed.t
147 }
148
149 # message-id pages
150 {
151         local $ENV{HOME} = $home;
152         my $slashy_mid = 'slashy/asdf@example.com';
153         my $reply = Email::Simple->new(<<EOF);
154 From: You <you\@example.com>
155 To: Me <me\@example.com>
156 Cc: $addr
157 Message-Id: <$slashy_mid>
158 Subject: Re: hihi
159 Date: Thu, 01 Jan 1970 00:00:01 +0000
160
161 slashy
162 EOF
163         my $in = $reply->as_string;
164
165         {
166                 local $ENV{HOME} = $home;
167                 local $ENV{ORIGINAL_RECIPIENT} = $addr;
168                 run_with_env({PATH => $main_path}, [$mda], \$in);
169         }
170
171         my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
172         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
173                 "slashy mid raw hit");
174
175         $res = cgi_run("/test/blahblah\@example.com/raw");
176         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
177                 "mid raw hit");
178         $res = cgi_run("/test/blahblah\@example.con/raw");
179         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
180
181         $res = cgi_run("/test/blahblah\@example.com/");
182         like($res->{body}, qr/\A<html>/, "mid html hit");
183         like($res->{head}, qr/Status: 200 OK/, "200 response");
184         $res = cgi_run("/test/blahblah\@example.con/");
185         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
186
187         $res = cgi_run("/test/blahblah\@example.com/f/");
188         like($res->{head}, qr/Status: 301 Moved/, "301 response");
189         like($res->{head},
190                 qr!^Location: http://[^/]+/test/blahblah%40example\.com/\r\n!ms,
191                 '301 redirect location');
192         $res = cgi_run("/test/blahblah\@example.con/");
193         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
194
195         $res = cgi_run("/test/");
196         like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
197                 "slashy URL generated correctly");
198 }
199
200 # retrieve thread as an mbox
201 {
202         local $ENV{HOME} = $home;
203         local $ENV{PATH} = $main_path;
204         my $path = "/test/blahblah%40example.com/t.mbox.gz";
205         my $res = cgi_run($path);
206         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
207         my $indexed = system($index, $maindir) == 0;
208         if ($indexed) {
209                 $res = cgi_run($path);
210                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
211                 eval {
212                         require IO::Uncompress::Gunzip;
213                         my $in = $res->{body};
214                         my $out;
215                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
216                         like($out, qr/^From /m, "From lines in mbox");
217                 };
218         } else {
219                 like($res->{head}, qr/^Status: 501 /, "search not available");
220         }
221
222         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
223         if ($have_xml_feed) {
224                 $path = "/test/blahblah%40example.com/t.atom";
225                 $res = cgi_run($path);
226                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
227                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
228                         "search returned atom");
229                 my $p = XML::Feed->parse(\($res->{body}));
230                 is($p->format, "Atom", "parsed atom feed");
231                 is(scalar $p->entries, 3, "parsed three entries");
232         }
233 }
234
235 # redirect list-name-only URLs
236 {
237         local $ENV{HOME} = $home;
238         my $res = cgi_run("/test");
239         like($res->{head}, qr/Status: 301 Moved/, "redirected status");
240         like($res->{head}, qr!/test/!, "redirected with slash");
241 }
242
243 done_testing();
244
245 sub run_with_env {
246         my ($env, @args) = @_;
247         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
248         run(@args, init => $init);
249 }
250
251 sub cgi_run {
252         my %env = (
253                 PATH_INFO => $_[0],
254                 QUERY_STRING => $_[1] || "",
255                 REQUEST_METHOD => $_[2] || "GET",
256                 GATEWAY_INTERFACE => 'CGI/1.1',
257                 HTTP_ACCEPT => '*/*',
258                 HTTP_HOST => 'test.example.com',
259         );
260         my ($in, $out, $err) = ("", "", "");
261         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
262         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
263         { head => $head, body => $body, rc => $rc, err => $err }
264 }