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)
7 use File::Temp qw/tempdir/;
10 my $psgi = "examples/public-inbox.psgi";
11 my $mda = "blib/script/public-inbox-mda";
12 my $tmpdir = tempdir(CLEANUP => 1);
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $main_bin = getcwd()."/t/main-bin";
18 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
19 my $addr = 'test-public@example.com';
20 my $cfgpfx = "publicinbox.test";
21 my $failbox = "$home/fail.mbox";
22 local $ENV{PI_EMERGENCY} = $failbox;
24 foreach my $mod (qw(Plack::Test HTTP::Request::Common
25 Mail::Thread URI::Escape)) {
27 plan skip_all => "$mod missing for plack.t" if $@;
31 ok(-f $psgi, "psgi example file found");
32 ok(-x "$main_bin/spamc",
33 "spamc ham mock found (run in top of source tree");
34 ok(-x $mda, "$mda is executable");
35 is(1, mkdir($home, 0755), "setup ~/ for testing");
36 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
37 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
38 open my $fh, '>', "$maindir/description" or die "open: $!\n";
39 print $fh "test for public-inbox\n";
40 close $fh or die "close: $!\n";
42 "$cfgpfx.address" => $addr,
43 "$cfgpfx.mainrepo" => $maindir,
45 while (my ($k,$v) = each %cfg) {
46 is(0, system(qw(git config --file), $pi_config, $k, $v),
50 local $ENV{HOME} = $home;
51 local $ENV{ORIGINAL_RECIPIENT} = $addr;
53 # ensure successful message delivery
55 my $simple = Email::Simple->new(<<EOF);
56 From: Me <me\@example.com>
57 To: You <you\@example.com>
59 Message-Id: <blah\@example.com>
61 Date: Thu, 01 Jan 1970 00:00:00 +0000
65 my $in = $simple->as_string;
66 run_with_env({PATH => $main_path}, [$mda], \$in);
67 local $ENV{GIT_DIR} = $maindir;
68 my $rev = `git rev-list HEAD`;
69 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
71 my $app = require $psgi;
73 # redirect with trailing /
76 my $from = 'http://example.com/test';
78 my $res = $cb->(GET($from));
79 is(301, $res->code, 'is permanent redirect');
80 is($to, $res->header('Location'), 'redirect location matches');
85 my $atomurl = 'http://example.com/test/new.atom';
86 my $res = $cb->(GET('http://example.com/test/'));
87 is(200, $res->code, 'success response received');
88 like($res->content, qr!href="\Q$atomurl\E"!,
89 'atom URL generated');
90 like($res->content, qr!href="blah%40example\.com/"!,
94 my $pfx = 'http://example.com/test';
97 my $res = $cb->(GET($pfx . '/atom.xml'));
98 is(200, $res->code, 'success response received for atom');
100 qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
101 'atom feed generated correct URL');
104 foreach my $t (('', 'f/')) {
105 test_psgi($app, sub {
107 my $path = "/blah%40example.com/$t";
108 my $res = $cb->(GET($pfx . $path));
109 is(200, $res->code, "success for $path");
110 like($res->content, qr!<title>hihi - Me</title>!,
114 test_psgi($app, sub {
116 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
117 is(200, $res->code, 'success response received for /*/raw');
118 like($res->content, qr!^From !sm, "mbox returned");
122 foreach my $t (qw(m f)) {
123 test_psgi($app, sub {
125 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
126 is(301, $res->code, "redirect for old $t .txt link");
127 my $location = $res->header('Location');
128 like($location, qr!/blah%40example\.com/raw\z!,
129 ".txt redirected to /raw");
138 while (my ($t, $e) = each %umap) {
139 test_psgi($app, sub {
141 my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
142 is(301, $res->code, "redirect for old $t .html link");
143 my $location = $res->header('Location');
145 qr!/blah%40example\.com/$e(?:#u)?\z!,
146 ".html redirected to new location");
149 foreach my $sfx (qw(mbox mbox.gz)) {
150 test_psgi($app, sub {
152 my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
153 is(301, $res->code, 'redirect for old thread link');
154 my $location = $res->header('Location');
156 qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
157 "$sfx redirected to /mbox.gz");
165 my ($env, @args) = @_;
166 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
167 run(@args, init => $init);