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 $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";
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)");
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";
36 "$cfgpfx.address" => $addr,
37 "$cfgpfx.mainrepo" => $maindir,
39 while (my ($k,$v) = each %cfg) {
40 is(0, system(qw(git config --file), $pi_config, $k, $v),
45 my $failbox = "$home/fail.mbox";
46 local $ENV{PI_EMERGENCY} = $failbox;
48 local $ENV{HOME} = $home;
49 local $ENV{ORIGINAL_RECIPIENT} = $addr;
51 # ensure successful message delivery
53 my $simple = Email::Simple->new(<<EOF);
54 From: Me <me\@example.com>
55 To: You <you\@example.com>
57 Message-Id: <blah\@example.com>
59 Date: Thu, 01 Jan 1970 00:00:00 +0000
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");
70 # deliver a reply, too
72 my $reply = Email::Simple->new(<<EOF);
73 From: You <you\@example.com>
74 To: Me <me\@example.com>
76 In-Reply-To: <blah\@example.com>
77 Message-Id: <blahblah\@example.com>
79 Date: Thu, 01 Jan 1970 00:00:01 +0000
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");
95 # obvious failures, first
97 local $ENV{HOME} = $home;
98 my $res = cgi_run("/", "", "PUT");
99 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
102 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
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");
112 qr!http://test\.example\.com/test/blah%40example\.com/!,
114 like($res->{body}, qr/what\?/, "reply included");
119 local $ENV{HOME} = $home;
120 my $res = cgi_run("/test/");
121 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
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
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>
140 Message-Id: <$slashy_mid>
142 Date: Thu, 01 Jan 1970 00:00:01 +0000
146 my $in = $reply->as_string;
149 local $ENV{HOME} = $home;
150 local $ENV{ORIGINAL_RECIPIENT} = $addr;
151 run_with_env({PATH => $main_path}, [$mda], \$in);
153 local $ENV{GIT_DIR} = $maindir;
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");
159 $res = cgi_run("/test/blahblah\@example.com/raw");
160 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
162 $res = cgi_run("/test/blahblah\@example.con/raw");
163 like($res->{head}, qr/Status: 404 Not Found/, "mid raw miss");
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: 404 Not Found/, "mid html miss");
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: 404 Not Found/, "mid html miss");
177 $res = cgi_run("/test/");
178 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
179 "slashy URL generated correctly");
182 # retrieve thread as an mbox
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;
191 $res = cgi_run($path);
192 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
194 require IO::Uncompress::Gunzip;
195 my $in = $res->{body};
197 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
198 like($out, qr/^From /m, "From lines in mbox");
201 like($res->{head}, qr/^Status: 501 /, "search not available");
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/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");
217 # redirect list-name-only URLs
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");
228 my ($env, @args) = @_;
229 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
230 run(@args, init => $init);
236 QUERY_STRING => $_[1] || "",
237 REQUEST_METHOD => $_[2] || "GET",
238 GATEWAY_INTERFACE => 'CGI/1.1',
239 HTTP_ACCEPT => '*/*',
240 HTTP_HOST => 'test.example.com',
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 }