]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
t/cgi.t: remove more redundant tests
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # FIXME: this test is too slow and most non-CGI-requirements
4 # should be moved over to things which use test_psgi
5 use strict;
6 use warnings;
7 use Test::More;
8 use Email::MIME;
9 use File::Temp qw/tempdir/;
10 use Cwd;
11 eval { require IPC::Run };
12 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
13
14 use constant CGI => "blib/script/public-inbox.cgi";
15 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 my $home = "$tmpdir/pi-home";
17 my $pi_home = "$home/.public-inbox";
18 my $pi_config = "$pi_home/config";
19 my $maindir = "$tmpdir/main.git";
20 my $addr = 'test-public@example.com';
21 my $cfgpfx = "publicinbox.test";
22
23 {
24         is(1, mkdir($home, 0755), "setup ~/ for testing");
25         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
26         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
27
28         open my $fh, '>', "$maindir/description" or die "open: $!\n";
29         print $fh "test for public-inbox\n";
30         close $fh or die "close: $!\n";
31         my %cfg = (
32                 "$cfgpfx.address" => $addr,
33                 "$cfgpfx.mainrepo" => $maindir,
34         );
35         while (my ($k,$v) = each %cfg) {
36                 is(0, system(qw(git config --file), $pi_config, $k, $v),
37                         "setup $k");
38         }
39 }
40
41 use_ok 'PublicInbox::Git';
42 use_ok 'PublicInbox::Import';
43 use_ok 'Email::MIME';
44 my $git = PublicInbox::Git->new($maindir);
45 my $im = PublicInbox::Import->new($git, 'test', $addr);
46
47 {
48         local $ENV{HOME} = $home;
49
50         # inject some messages:
51         my $mime = Email::MIME->new(<<EOF);
52 From: Me <me\@example.com>
53 To: You <you\@example.com>
54 Cc: $addr
55 Message-Id: <blah\@example.com>
56 Subject: hihi
57 Date: Thu, 01 Jan 1970 00:00:00 +0000
58
59 zzzzzz
60 EOF
61         $im->add($mime);
62
63         # deliver a reply, too
64         my $reply = Email::MIME->new(<<EOF);
65 From: You <you\@example.com>
66 To: Me <me\@example.com>
67 Cc: $addr
68 In-Reply-To: <blah\@example.com>
69 Message-Id: <blahblah\@example.com>
70 Subject: Re: hihi
71 Date: Thu, 01 Jan 1970 00:00:01 +0000
72
73 Me wrote:
74 > zzzzzz
75
76 what?
77 EOF
78         $im->add($reply);
79
80         my $slashy_mid = 'slashy/asdf@example.com';
81         my $slashy = Email::MIME->new(<<EOF);
82 From: You <you\@example.com>
83 To: Me <me\@example.com>
84 Cc: $addr
85 Message-Id: <$slashy_mid>
86 Subject: Re: hihi
87 Date: Thu, 01 Jan 1970 00:00:01 +0000
88
89 slashy
90 EOF
91         $im->add($slashy);
92         $im->done;
93
94         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
95         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
96                 "slashy mid raw hit");
97 }
98
99 # retrieve thread as an mbox
100 {
101         local $ENV{HOME} = $home;
102         my $path = "/test/blahblah\@example.com/t.mbox.gz";
103         my $res = cgi_run($path);
104         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
105         my $indexed;
106         eval {
107                 require PublicInbox::SearchIdx;
108                 my $s = PublicInbox::SearchIdx->new($maindir, 1);
109                 $s->index_sync;
110                 $indexed = 1;
111         };
112         if ($indexed) {
113                 $res = cgi_run($path);
114                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
115                 eval {
116                         require IO::Uncompress::Gunzip;
117                         my $in = $res->{body};
118                         my $out;
119                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
120                         like($out, qr/^From /m, "From lines in mbox");
121                 };
122         } else {
123                 like($res->{head}, qr/^Status: 501 /, "search not available");
124         }
125
126         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
127         if ($have_xml_feed) {
128                 $path = "/test/blahblah\@example.com/t.atom";
129                 $res = cgi_run($path);
130                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
131                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
132                         "search returned atom");
133                 my $p = XML::Feed->parse(\($res->{body}));
134                 is($p->format, "Atom", "parsed atom feed");
135                 is(scalar $p->entries, 3, "parsed three entries");
136         }
137 }
138
139 done_testing();
140
141 sub run_with_env {
142         my ($env, @args) = @_;
143         IPC::Run::run(@args, init => sub { %ENV = (%ENV, %$env) });
144 }
145
146 sub cgi_run {
147         my %env = (
148                 PATH_INFO => $_[0],
149                 QUERY_STRING => $_[1] || "",
150                 SCRIPT_NAME => '',
151                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
152                 REQUEST_METHOD => $_[2] || "GET",
153                 GATEWAY_INTERFACE => 'CGI/1.1',
154                 HTTP_ACCEPT => '*/*',
155                 HTTP_HOST => 'test.example.com',
156         );
157         my ($in, $out, $err) = ("", "", "");
158         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
159         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
160         { head => $head, body => $body, rc => $rc, err => $err }
161 }