]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
update copyright headers and email addresses
[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(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                 local $ENV{GIT_DIR} = $maindir;
66                 my $rev = `git rev-list HEAD`;
67                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
68         }
69
70         # deliver a reply, too
71         {
72                 my $reply = Email::Simple->new(<<EOF);
73 From: You <you\@example.com>
74 To: Me <me\@example.com>
75 Cc: $addr
76 In-Reply-To: <blah\@example.com>
77 Message-Id: <blahblah\@example.com>
78 Subject: Re: hihi
79 Date: Thu, 01 Jan 1970 00:00:01 +0000
80
81 Me wrote:
82 > zzzzzz
83
84 what?
85 EOF
86                 my $in = $reply->as_string;
87                 run_with_env({PATH => $main_path}, [$mda], \$in);
88                 local $ENV{GIT_DIR} = $maindir;
89                 my $rev = `git rev-list HEAD`;
90                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
91         }
92
93 }
94
95 # obvious failures, first
96 {
97         local $ENV{HOME} = $home;
98         my $res = cgi_run("/", "", "PUT");
99         like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
100
101         $res = cgi_run("/");
102         like($res->{head}, qr/Status:\s*404/i, "index returns 404");
103 }
104
105 # atom feeds
106 {
107         local $ENV{HOME} = $home;
108         my $res = cgi_run("/test/atom.xml");
109         like($res->{body}, qr/<title>test for public-inbox/,
110                 "set title in XML feed");
111         like($res->{body},
112                 qr!http://test\.example\.com/test/blah%40example\.com/!,
113                 "link id set");
114         like($res->{body}, qr/what\?/, "reply included");
115 }
116
117 # indices
118 {
119         local $ENV{HOME} = $home;
120         my $res = cgi_run("/test/");
121         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
122
123         my $idx = cgi_run("/test/index.html");
124         $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
125         $idx->{body} = [ split(/\n/, $idx->{body}) ];
126         $res->{body} = [ split(/\n/, $res->{body}) ];
127         is_deeply($res, $idx,
128                 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
129         # more checks in t/feed.t
130 }
131
132 # message-id pages
133 {
134         local $ENV{HOME} = $home;
135         my $slashy_mid = 'slashy/asdf@example.com';
136         my $reply = Email::Simple->new(<<EOF);
137 From: You <you\@example.com>
138 To: Me <me\@example.com>
139 Cc: $addr
140 Message-Id: <$slashy_mid>
141 Subject: Re: hihi
142 Date: Thu, 01 Jan 1970 00:00:01 +0000
143
144 slashy
145 EOF
146         my $in = $reply->as_string;
147
148         {
149                 local $ENV{HOME} = $home;
150                 local $ENV{ORIGINAL_RECIPIENT} = $addr;
151                 run_with_env({PATH => $main_path}, [$mda], \$in);
152         }
153         local $ENV{GIT_DIR} = $maindir;
154
155         my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
156         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
157                 "slashy mid raw hit");
158
159         $res = cgi_run("/test/blahblah\@example.com/raw");
160         like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
161                 "mid raw hit");
162         $res = cgi_run("/test/blahblah\@example.con/raw");
163         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
164
165         $res = cgi_run("/test/blahblah\@example.com/");
166         like($res->{body}, qr/\A<html>/, "mid html hit");
167         like($res->{head}, qr/Status: 200 OK/, "200 response");
168         $res = cgi_run("/test/blahblah\@example.con/");
169         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
170
171         $res = cgi_run("/test/blahblah\@example.com/f/");
172         like($res->{body}, qr/\A<html>/, "mid html");
173         like($res->{head}, qr/Status: 200 OK/, "200 response");
174         $res = cgi_run("/test/blahblah\@example.con/f/");
175         like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
176
177         $res = cgi_run("/test/");
178         like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
179                 "slashy URL generated correctly");
180 }
181
182 # retrieve thread as an mbox
183 {
184         local $ENV{HOME} = $home;
185         local $ENV{PATH} = $main_path;
186         my $path = "/test/blahblah%40example.com/t.mbox.gz";
187         my $res = cgi_run($path);
188         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
189         my $indexed = system($index, $maindir) == 0;
190         if ($indexed) {
191                 $res = cgi_run($path);
192                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
193                 eval {
194                         require IO::Uncompress::Gunzip;
195                         my $in = $res->{body};
196                         my $out;
197                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
198                         like($out, qr/^From /m, "From lines in mbox");
199                 };
200         } else {
201                 like($res->{head}, qr/^Status: 501 /, "search not available");
202         }
203
204         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
205         if ($have_xml_feed) {
206                 $path = "/test/blahblah%40example.com/t.atom";
207                 $res = cgi_run($path);
208                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
209                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
210                         "search returned atom");
211                 my $p = XML::Feed->parse(\($res->{body}));
212                 is($p->format, "Atom", "parsed atom feed");
213                 is(scalar $p->entries, 3, "parsed three entries");
214         }
215 }
216
217 # redirect list-name-only URLs
218 {
219         local $ENV{HOME} = $home;
220         my $res = cgi_run("/test");
221         like($res->{head}, qr/Status: 301 Moved/, "redirected status");
222         like($res->{head}, qr!/test/!, "redirected with slash");
223 }
224
225 done_testing();
226
227 sub run_with_env {
228         my ($env, @args) = @_;
229         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
230         run(@args, init => $init);
231 }
232
233 sub cgi_run {
234         my %env = (
235                 PATH_INFO => $_[0],
236                 QUERY_STRING => $_[1] || "",
237                 REQUEST_METHOD => $_[2] || "GET",
238                 GATEWAY_INTERFACE => 'CGI/1.1',
239                 HTTP_ACCEPT => '*/*',
240                 HTTP_HOST => 'test.example.com',
241         );
242         my ($in, $out, $err) = ("", "", "");
243         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
244         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
245         { head => $head, body => $body, rc => $rc, err => $err }
246 }