]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
81130df5bc418b97f2a1ceff8cf1e7a128e2d348
[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 eval { require IPC::Run };
11 plan skip_all => "missing IPC::Run for t/cgi.t" if $@;
12
13 use constant CGI => "blib/script/public-inbox.cgi";
14 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, 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 $addr = 'test-public@example.com';
20 my $cfgpfx = "publicinbox.test";
21
22 {
23         is(1, mkdir($home, 0755), "setup ~/ for testing");
24         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
25         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
26
27         open my $fh, '>', "$maindir/description" or die "open: $!\n";
28         print $fh "test for public-inbox\n";
29         close $fh or die "close: $!\n";
30         my %cfg = (
31                 "$cfgpfx.address" => $addr,
32                 "$cfgpfx.mainrepo" => $maindir,
33                 "$cfgpfx.indexlevel" => 'basic',
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 'PublicInbox::Inbox';
44 use_ok 'PublicInbox::InboxWritable';
45 use_ok 'PublicInbox::Config';
46 my $cfg = PublicInbox::Config->new($pi_config);
47 my $ibx = $cfg->lookup_name('test');
48 my $im = PublicInbox::InboxWritable->new($ibx)->importer;
49
50 {
51         local $ENV{HOME} = $home;
52
53         # inject some messages:
54         my $mime = Email::MIME->new(<<EOF);
55 From: Me <me\@example.com>
56 To: You <you\@example.com>
57 Cc: $addr
58 Message-Id: <blah\@example.com>
59 Subject: hihi
60 Date: Thu, 01 Jan 1970 00:00:00 +0000
61
62 zzzzzz
63 EOF
64         $im->add($mime);
65
66         # deliver a reply, too
67         my $reply = Email::MIME->new(<<EOF);
68 From: You <you\@example.com>
69 To: Me <me\@example.com>
70 Cc: $addr
71 In-Reply-To: <blah\@example.com>
72 Message-Id: <blahblah\@example.com>
73 Subject: Re: hihi
74 Date: Thu, 01 Jan 1970 00:00:01 +0000
75
76 Me wrote:
77 > zzzzzz
78
79 what?
80 EOF
81         $im->add($reply);
82
83         my $slashy_mid = 'slashy/asdf@example.com';
84         my $slashy = Email::MIME->new(<<EOF);
85 From: You <you\@example.com>
86 To: Me <me\@example.com>
87 Cc: $addr
88 Message-Id: <$slashy_mid>
89 Subject: Re: hihi
90 Date: Thu, 01 Jan 1970 00:00:01 +0000
91
92 slashy
93 EOF
94         $im->add($slashy);
95         $im->done;
96
97         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
98         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
99                 "slashy mid raw hit");
100 }
101
102 # retrieve thread as an mbox
103 {
104         local $ENV{HOME} = $home;
105         my $path = "/test/blahblah\@example.com/t.mbox.gz";
106         my $res = cgi_run($path);
107         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
108         my $indexed;
109         eval {
110                 require DBD::SQLite;
111                 require PublicInbox::SearchIdx;
112                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
113                 $s->index_sync;
114                 $indexed = 1;
115         };
116         if ($indexed) {
117                 $res = cgi_run($path);
118                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
119                 eval {
120                         require IO::Uncompress::Gunzip;
121                         my $in = $res->{body};
122                         my $out;
123                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
124                         like($out, qr/^From /m, "From lines in mbox");
125                 };
126         } else {
127                 like($res->{head}, qr/^Status: 501 /, "search not available");
128                 SKIP: { skip 'DBD::SQLite not available', 2 };
129         }
130
131         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
132         if ($have_xml_feed) {
133                 $path = "/test/blahblah\@example.com/t.atom";
134                 $res = cgi_run($path);
135                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
136                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
137                         "search returned atom");
138                 my $p = XML::Feed->parse(\($res->{body}));
139                 is($p->format, "Atom", "parsed atom feed");
140                 is(scalar $p->entries, 3, "parsed three entries");
141         } else {
142                 SKIP: { skip 'DBD::SQLite or XML::Feed missing', 2 };
143         }
144 }
145
146 done_testing();
147
148 sub run_with_env {
149         my ($env, @args) = @_;
150         IPC::Run::run(@args, init => sub { %ENV = (%ENV, %$env) });
151 }
152
153 sub cgi_run {
154         my %env = (
155                 PATH_INFO => $_[0],
156                 QUERY_STRING => $_[1] || "",
157                 SCRIPT_NAME => '',
158                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
159                 REQUEST_METHOD => $_[2] || "GET",
160                 GATEWAY_INTERFACE => 'CGI/1.1',
161                 HTTP_ACCEPT => '*/*',
162                 HTTP_HOST => 'test.example.com',
163         );
164         my ($in, $out, $err) = ("", "", "");
165         my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
166         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
167         { head => $head, body => $body, rc => $rc, err => $err }
168 }