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)");
31 open my $fh, '>', "$maindir/description" or die "open: $!\n";
32 print $fh "test for public-inbox\n";
33 close $fh or die "close: $!\n";
35 "$cfgpfx.address" => $addr,
36 "$cfgpfx.mainrepo" => $maindir,
38 while (my ($k,$v) = each %cfg) {
39 is(0, system(qw(git config --file), $pi_config, $k, $v),
44 my $failbox = "$home/fail.mbox";
45 local $ENV{PI_EMERGENCY} = $failbox;
47 local $ENV{HOME} = $home;
48 local $ENV{ORIGINAL_RECIPIENT} = $addr;
50 # ensure successful message delivery
52 my $simple = Email::Simple->new(<<EOF);
53 From: Me <me\@example.com>
54 To: You <you\@example.com>
56 Message-Id: <blah\@example.com>
58 Date: Thu, 01 Jan 1970 00:00:00 +0000
62 my $in = $simple->as_string;
63 run_with_env({PATH => $main_path}, [$mda], \$in);
64 local $ENV{GIT_DIR} = $maindir;
65 my $rev = `git rev-list HEAD`;
66 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
69 # deliver a reply, too
71 my $reply = Email::Simple->new(<<EOF);
72 From: You <you\@example.com>
73 To: Me <me\@example.com>
75 In-Reply-To: <blah\@example.com>
76 Message-Id: <blahblah\@example.com>
78 Date: Thu, 01 Jan 1970 00:00:01 +0000
85 my $in = $reply->as_string;
86 run_with_env({PATH => $main_path}, [$mda], \$in);
87 local $ENV{GIT_DIR} = $maindir;
88 my $rev = `git rev-list HEAD`;
89 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
94 # obvious failures, first
96 local $ENV{HOME} = $home;
97 my $res = cgi_run("/", "", "PUT");
98 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
101 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
106 local $ENV{HOME} = $home;
107 my $res = cgi_run("/test/atom.xml");
108 like($res->{body}, qr/<title>test for public-inbox/,
109 "set title in XML feed");
111 qr!http://test\.example\.com/test/m/blah%40example\.com!,
113 like($res->{body}, qr/what\?/, "reply included");
118 local $ENV{HOME} = $home;
119 my $res = cgi_run("/test/");
120 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
122 my $idx = cgi_run("/test/index.html");
123 $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
124 $idx->{body} = [ split(/\n/, $idx->{body}) ];
125 $res->{body} = [ split(/\n/, $res->{body}) ];
126 is_deeply($res, $idx,
127 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
128 # more checks in t/feed.t
133 local $ENV{HOME} = $home;
134 my $slashy_mid = 'slashy/asdf@example.com';
135 my $reply = Email::Simple->new(<<EOF);
136 From: You <you\@example.com>
137 To: Me <me\@example.com>
139 Message-Id: <$slashy_mid>
141 Date: Thu, 01 Jan 1970 00:00:01 +0000
145 my $in = $reply->as_string;
148 local $ENV{HOME} = $home;
149 local $ENV{ORIGINAL_RECIPIENT} = $addr;
150 run_with_env({PATH => $main_path}, [$mda], \$in);
152 local $ENV{GIT_DIR} = $maindir;
154 my $res = cgi_run("/test/m/slashy%2fasdf%40example.com.txt");
155 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
156 "slashy mid.txt hit");
158 $res = cgi_run("/test/m/blahblah\@example.com.txt");
159 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
161 $res = cgi_run("/test/m/blahblah\@example.con.txt");
162 like($res->{head}, qr/Status: 404 Not Found/, "mid.txt miss");
164 $res = cgi_run("/test/m/blahblah\@example.com.html");
165 like($res->{body}, qr/\A<html>/, "mid.html hit");
166 like($res->{head}, qr/Status: 200 OK/, "200 response");
167 $res = cgi_run("/test/m/blahblah\@example.con.html");
168 like($res->{head}, qr/Status: 404 Not Found/, "mid.html miss");
170 $res = cgi_run("/test/f/blahblah\@example.com.html");
171 like($res->{body}, qr/\A<html>/, "mid.html hit");
172 like($res->{head}, qr/Status: 200 OK/, "200 response");
173 $res = cgi_run("/test/f/blahblah\@example.con.html");
174 like($res->{head}, qr/Status: 404 Not Found/, "mid.html miss");
176 $res = cgi_run("/test/");
177 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
178 "slashy URL generated correctly");
181 # redirect list-name-only URLs
183 local $ENV{HOME} = $home;
184 my $res = cgi_run("/test");
185 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
186 like($res->{head}, qr!/test/!, "redirected with slash");
192 my ($env, @args) = @_;
193 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
194 run(@args, init => $init);
200 QUERY_STRING => $_[1] || "",
201 REQUEST_METHOD => $_[2] || "GET",
202 GATEWAY_INTERFACE => 'CGI/1.1',
203 HTTP_ACCEPT => '*/*',
204 HTTP_HOST => 'test.example.com',
206 my ($in, $out, $err) = ("", "", "");
207 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
208 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
209 { head => $head, body => $body, rc => $rc, err => $err }