]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
tests: fixup requirements for tests
[public-inbox.git] / t / plack.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 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;
23
24 foreach my $mod (qw(Plack::Test HTTP::Request::Common
25                          Mail::Thread URI::Escape)) {
26         eval "require $mod";
27         plan skip_all => "$mod missing for plack.t" if $@;
28 }
29
30 {
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";
41         my %cfg = (
42                 "$cfgpfx.address" => $addr,
43                 "$cfgpfx.mainrepo" => $maindir,
44         );
45         while (my ($k,$v) = each %cfg) {
46                 is(0, system(qw(git config --file), $pi_config, $k, $v),
47                         "setup $k");
48         }
49
50         local $ENV{HOME} = $home;
51         local $ENV{ORIGINAL_RECIPIENT} = $addr;
52
53         # ensure successful message delivery
54         {
55                 my $simple = Email::Simple->new(<<EOF);
56 From: Me <me\@example.com>
57 To: You <you\@example.com>
58 Cc: $addr
59 Message-Id: <blah\@example.com>
60 Subject: hihi
61 Date: Thu, 01 Jan 1970 00:00:00 +0000
62
63 zzzzzz
64 EOF
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");
70         }
71         my $app = require $psgi;
72
73         # redirect with trailing /
74         test_psgi($app, sub {
75                 my ($cb) = @_;
76                 my $from = 'http://example.com/test';
77                 my $to = "$from/";
78                 my $res = $cb->(GET($from));
79                 is(301, $res->code, 'is permanent redirect');
80                 is($to, $res->header('Location'), 'redirect location matches');
81         });
82
83         test_psgi($app, sub {
84                 my ($cb) = @_;
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/"!,
91                         'index generated');
92         });
93
94         my $pfx = 'http://example.com/test';
95         test_psgi($app, sub {
96                 my ($cb) = @_;
97                 my $res = $cb->(GET($pfx . '/atom.xml'));
98                 is(200, $res->code, 'success response received for atom');
99                 like($res->content,
100                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
101                         'atom feed generated correct URL');
102         });
103
104         foreach my $t (('', 'f/')) {
105                 test_psgi($app, sub {
106                         my ($cb) = @_;
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>!,
111                                 "HTML returned");
112                 });
113         }
114         test_psgi($app, sub {
115                 my ($cb) = @_;
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");
119         });
120
121         # legacy redirects
122         foreach my $t (qw(m f)) {
123                 test_psgi($app, sub {
124                         my ($cb) = @_;
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");
130                 });
131         }
132
133         my %umap = (
134                 'm' => '',
135                 'f' => 'f/',
136                 't' => 't/',
137         );
138         while (my ($t, $e) = each %umap) {
139                 test_psgi($app, sub {
140                         my ($cb) = @_;
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');
144                         like($location,
145                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
146                                 ".html redirected to new location");
147                 });
148         }
149         foreach my $sfx (qw(mbox mbox.gz)) {
150                 test_psgi($app, sub {
151                         my ($cb) = @_;
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');
155                         like($location,
156                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
157                              "$sfx redirected to /mbox.gz");
158                 });
159         }
160 }
161
162 done_testing();
163
164 sub run_with_env {
165         my ($env, @args) = @_;
166         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
167         run(@args, init => $init);
168 }