1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
7 use File::Temp qw/tempdir/;
11 use constant CGI => "blib/script/public-inbox.cgi";
12 my $mda = "blib/script/public-inbox-mda";
13 my $tmpdir = tempdir(CLEANUP => 1);
14 my $home = "$tmpdir/pi-home";
15 my $pi_home = "$home/.public-inbox";
16 my $pi_config = "$pi_home/config";
17 my $maindir = "$tmpdir/main.git";
18 my $main_bin = getcwd()."/t/main-bin";
19 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
20 my $addr = 'test-public@example.com';
21 my $cfgpfx = "publicinbox.test";
24 ok(-x "$main_bin/spamc",
25 "spamc ham mock found (run in top of source tree");
26 ok(-x $mda, "$mda is executable");
27 is(1, mkdir($home, 0755), "setup ~/ for testing");
28 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
29 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
32 "$cfgpfx.address" => $addr,
33 "$cfgpfx.mainrepo" => $maindir,
34 "$cfgpfx.description" => 'test for public-inbox',
36 while (my ($k,$v) = each %cfg) {
37 is(0, system(qw(git config --file), $pi_config, $k, $v),
42 my $failbox = "$home/fail.mbox";
43 local $ENV{PI_EMERGENCY} = $failbox;
45 local $ENV{HOME} = $home;
46 local $ENV{ORIGINAL_RECIPIENT} = $addr;
48 # ensure successful message delivery
50 my $simple = Email::Simple->new(<<EOF);
51 From: Me <me\@example.com>
52 To: You <you\@example.com>
54 Message-Id: <blah\@example.com>
56 Date: Thu, 01 Jan 1970 00:00:00 +0000
60 my $in = $simple->as_string;
61 run_with_env({PATH => $main_path}, [$mda], \$in);
62 local $ENV{GIT_DIR} = $maindir;
63 my $rev = `git rev-list HEAD`;
64 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
67 # deliver a reply, too
69 my $reply = Email::Simple->new(<<EOF);
70 From: You <you\@example.com>
71 To: Me <me\@example.com>
73 In-Reply-To: <blah\@example.com>
74 Message-Id: <blahblah\@example.com>
76 Date: Thu, 01 Jan 1970 00:00:01 +0000
83 my $in = $reply->as_string;
84 run_with_env({PATH => $main_path}, [$mda], \$in);
85 local $ENV{GIT_DIR} = $maindir;
86 my $rev = `git rev-list HEAD`;
87 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
92 # obvious failures, first
94 local $ENV{HOME} = $home;
95 my $res = cgi_run("/", "", "PUT");
96 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
99 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
104 local $ENV{HOME} = $home;
105 my $res = cgi_run("/test/atom.xml");
106 like($res->{body}, qr/<title>test for public-inbox/,
107 "set title in XML feed");
109 qr!http://test\.example\.com/test/m/blah%40example\.com!,
111 like($res->{body}, qr/what\?/, "reply included");
116 local $ENV{HOME} = $home;
117 my $res = cgi_run("/test/");
118 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
120 my $idx = cgi_run("/test/index.html");
121 $idx->{body} =~ s!/index.html(\?r=)!/$1!; # dirty...
122 is_deeply($res, $idx,
123 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
124 # more checks in t/feed.t
129 local $ENV{HOME} = $home;
130 my $slashy_mid = 'slashy/asdf@example.com';
131 my $reply = Email::Simple->new(<<EOF);
132 From: You <you\@example.com>
133 To: Me <me\@example.com>
135 Message-Id: <$slashy_mid>
137 Date: Thu, 01 Jan 1970 00:00:01 +0000
141 my $in = $reply->as_string;
144 local $ENV{HOME} = $home;
145 local $ENV{ORIGINAL_RECIPIENT} = $addr;
146 run_with_env({PATH => $main_path}, [$mda], \$in);
148 local $ENV{GIT_DIR} = $maindir;
150 my $res = cgi_run("/test/m/slashy%2fasdf%40example.com.txt");
151 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
152 "slashy mid.txt hit");
154 $res = cgi_run("/test/m/blahblah\@example.com.txt");
155 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
157 $res = cgi_run("/test/m/blahblah\@example.con.txt");
158 like($res->{head}, qr/Status: 404 Not Found/, "mid.txt miss");
160 $res = cgi_run("/test/m/blahblah\@example.com.html");
161 like($res->{body}, qr/\A<html>/, "mid.html hit");
162 like($res->{head}, qr/Status: 200 OK/, "200 response");
163 $res = cgi_run("/test/m/blahblah\@example.con.html");
164 like($res->{head}, qr/Status: 404 Not Found/, "mid.html miss");
166 $res = cgi_run("/test/f/blahblah\@example.com.html");
167 like($res->{body}, qr/\A<html>/, "mid.html hit");
168 like($res->{head}, qr/Status: 200 OK/, "200 response");
169 $res = cgi_run("/test/f/blahblah\@example.con.html");
170 like($res->{head}, qr/Status: 404 Not Found/, "mid.html miss");
172 $res = cgi_run("/test/");
173 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
174 "slashy URL generated correctly");
177 # redirect list-name-only URLs
179 local $ENV{HOME} = $home;
180 my $res = cgi_run("/test");
181 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
182 like($res->{head}, qr!/test/!, "redirected with slash");
188 my ($env, @args) = @_;
189 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
190 run(@args, init => $init);
196 QUERY_STRING => $_[1] || "",
197 REQUEST_METHOD => $_[2] || "GET",
198 GATEWAY_INTERFACE => 'CGI/1.1',
199 HTTP_ACCEPT => '*/*',
200 HTTP_HOST => 'test.example.com',
202 my ($in, $out, $err) = ("", "", "");
203 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
204 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
205 { head => $head, body => $body, rc => $rc, err => $err }