]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
t/*.t: use identifiable tempdir names
[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('pi-plack-XXXXXX', TMPDIR => 1, 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 my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test
24         Mail::Thread URI::Escape);
25 foreach my $mod (@mods) {
26         eval "require $mod";
27         plan skip_all => "$mod missing for plack.t" if $@;
28 }
29
30 foreach my $mod (@mods) { use_ok $mod; }
31 {
32         ok(-f $psgi, "psgi example file found");
33         ok(-x "$main_bin/spamc",
34                 "spamc ham mock found (run in top of source tree");
35         ok(-x $mda, "$mda is executable");
36         is(1, mkdir($home, 0755), "setup ~/ for testing");
37         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
38         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
39         open my $fh, '>', "$maindir/description" or die "open: $!\n";
40         print $fh "test for public-inbox\n";
41         close $fh or die "close: $!\n";
42         my %cfg = (
43                 "$cfgpfx.address" => $addr,
44                 "$cfgpfx.mainrepo" => $maindir,
45         );
46         while (my ($k,$v) = each %cfg) {
47                 is(0, system(qw(git config --file), $pi_config, $k, $v),
48                         "setup $k");
49         }
50
51         local $ENV{HOME} = $home;
52         local $ENV{ORIGINAL_RECIPIENT} = $addr;
53
54         # ensure successful message delivery
55         {
56                 my $simple = Email::Simple->new(<<EOF);
57 From: Me <me\@example.com>
58 To: You <you\@example.com>
59 Cc: $addr
60 Message-Id: <blah\@example.com>
61 Subject: hihi
62 Date: Thu, 01 Jan 1970 00:00:00 +0000
63
64 zzzzzz
65 EOF
66                 my $in = $simple->as_string;
67                 run_with_env({PATH => $main_path}, [$mda], \$in);
68                 local $ENV{GIT_DIR} = $maindir;
69                 my $rev = `git rev-list HEAD`;
70                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
71         }
72         my $app = require $psgi;
73
74         # redirect with trailing /
75         test_psgi($app, sub {
76                 my ($cb) = @_;
77                 my $from = 'http://example.com/test';
78                 my $to = "$from/";
79                 my $res = $cb->(GET($from));
80                 is(301, $res->code, 'is permanent redirect');
81                 is($to, $res->header('Location'), 'redirect location matches');
82         });
83
84         my $pfx = 'http://example.com/test';
85         foreach my $t (qw(t T)) {
86                 test_psgi($app, sub {
87                         my ($cb) = @_;
88                         my $u = $pfx . "/blah%40example.com/$t";
89                         my $res = $cb->(GET($u));
90                         is(301, $res->code, "redirect for missing /");
91                         my $location = $res->header('Location');
92                         like($location, qr!/\Q$t\E/#u\z!,
93                                 'redirected with missing /');
94                 });
95         }
96         foreach my $t (qw(f)) {
97                 test_psgi($app, sub {
98                         my ($cb) = @_;
99                         my $u = $pfx . "/blah%40example.com/$t";
100                         my $res = $cb->(GET($u));
101                         is(301, $res->code, "redirect for missing /");
102                         my $location = $res->header('Location');
103                         like($location, qr!/\Q$t\E/\z!,
104                                 'redirected with missing /');
105                 });
106         }
107
108         test_psgi($app, sub {
109                 my ($cb) = @_;
110                 my $atomurl = 'http://example.com/test/new.atom';
111                 my $res = $cb->(GET('http://example.com/test/'));
112                 is(200, $res->code, 'success response received');
113                 like($res->content, qr!href="\Q$atomurl\E"!,
114                         'atom URL generated');
115                 like($res->content, qr!href="blah%40example\.com/"!,
116                         'index generated');
117         });
118
119         test_psgi($app, sub {
120                 my ($cb) = @_;
121                 my $res = $cb->(GET($pfx . '/atom.xml'));
122                 is(200, $res->code, 'success response received for atom');
123                 like($res->content,
124                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
125                         'atom feed generated correct URL');
126         });
127
128         foreach my $t (('', 'f/')) {
129                 test_psgi($app, sub {
130                         my ($cb) = @_;
131                         my $path = "/blah%40example.com/$t";
132                         my $res = $cb->(GET($pfx . $path));
133                         is(200, $res->code, "success for $path");
134                         like($res->content, qr!<title>hihi - Me</title>!,
135                                 "HTML returned");
136                 });
137         }
138         test_psgi($app, sub {
139                 my ($cb) = @_;
140                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
141                 is(200, $res->code, 'success response received for /*/raw');
142                 like($res->content, qr!^From !sm, "mbox returned");
143         });
144
145         # legacy redirects
146         foreach my $t (qw(m f)) {
147                 test_psgi($app, sub {
148                         my ($cb) = @_;
149                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
150                         is(301, $res->code, "redirect for old $t .txt link");
151                         my $location = $res->header('Location');
152                         like($location, qr!/blah%40example\.com/raw\z!,
153                                 ".txt redirected to /raw");
154                 });
155         }
156
157         my %umap = (
158                 'm' => '',
159                 'f' => 'f/',
160                 't' => 't/',
161         );
162         while (my ($t, $e) = each %umap) {
163                 test_psgi($app, sub {
164                         my ($cb) = @_;
165                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
166                         is(301, $res->code, "redirect for old $t .html link");
167                         my $location = $res->header('Location');
168                         like($location,
169                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
170                                 ".html redirected to new location");
171                 });
172         }
173         foreach my $sfx (qw(mbox mbox.gz)) {
174                 test_psgi($app, sub {
175                         my ($cb) = @_;
176                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
177                         is(301, $res->code, 'redirect for old thread link');
178                         my $location = $res->header('Location');
179                         like($location,
180                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
181                              "$sfx redirected to /mbox.gz");
182                 });
183         }
184 }
185
186 done_testing();
187
188 sub run_with_env {
189         my ($env, @args) = @_;
190         my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
191         run(@args, init => $init);
192 }