]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
tests: move t/common.perl to PublicInbox::TestCommon
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2019 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 PublicInbox::TestCommon;
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $home = "$tmpdir/pi-home";
12 my $pi_home = "$home/.public-inbox";
13 my $pi_config = "$pi_home/config";
14 my $maindir = "$tmpdir/main.git";
15 my $addr = 'test-public@example.com';
16 my $cfgpfx = "publicinbox.test";
17
18 {
19         is(1, mkdir($home, 0755), "setup ~/ for testing");
20         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
21         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
22
23         open my $fh, '>', "$maindir/description" or die "open: $!\n";
24         print $fh "test for public-inbox\n";
25         close $fh or die "close: $!\n";
26         my %cfg = (
27                 "$cfgpfx.address" => $addr,
28                 "$cfgpfx.inboxdir" => $maindir,
29                 "$cfgpfx.indexlevel" => 'basic',
30         );
31         while (my ($k,$v) = each %cfg) {
32                 is(0, system(qw(git config --file), $pi_config, $k, $v),
33                         "setup $k");
34         }
35 }
36
37 use_ok 'PublicInbox::Git';
38 use_ok 'PublicInbox::Import';
39 use_ok 'PublicInbox::Inbox';
40 use_ok 'PublicInbox::InboxWritable';
41 use_ok 'PublicInbox::Config';
42 my $cfg = PublicInbox::Config->new($pi_config);
43 my $ibx = $cfg->lookup_name('test');
44 my $im = PublicInbox::InboxWritable->new($ibx)->importer;
45
46 {
47         local $ENV{HOME} = $home;
48
49         # inject some messages:
50         my $mime = Email::MIME->new(<<EOF);
51 From: Me <me\@example.com>
52 To: You <you\@example.com>
53 Cc: $addr
54 Message-Id: <blah\@example.com>
55 Subject: hihi
56 Date: Thu, 01 Jan 1970 00:00:00 +0000
57
58 zzzzzz
59 EOF
60         $im->add($mime);
61
62         # deliver a reply, too
63         my $reply = Email::MIME->new(<<EOF);
64 From: You <you\@example.com>
65 To: Me <me\@example.com>
66 Cc: $addr
67 In-Reply-To: <blah\@example.com>
68 Message-Id: <blahblah\@example.com>
69 Subject: Re: hihi
70 Date: Thu, 01 Jan 1970 00:00:01 +0000
71
72 Me wrote:
73 > zzzzzz
74
75 what?
76 EOF
77         $im->add($reply);
78
79         my $slashy_mid = 'slashy/asdf@example.com';
80         my $slashy = Email::MIME->new(<<EOF);
81 From: You <you\@example.com>
82 To: Me <me\@example.com>
83 Cc: $addr
84 Message-Id: <$slashy_mid>
85 Subject: Re: hihi
86 Date: Thu, 01 Jan 1970 00:00:01 +0000
87
88 slashy
89 EOF
90         $im->add($slashy);
91         $im->done;
92
93         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
94         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
95                 "slashy mid raw hit");
96 }
97
98 # retrieve thread as an mbox
99 {
100         local $ENV{HOME} = $home;
101         my $path = "/test/blahblah\@example.com/t.mbox.gz";
102         my $res = cgi_run($path);
103         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
104         my $indexed;
105         eval {
106                 require DBD::SQLite;
107                 require PublicInbox::SearchIdx;
108                 my $s = PublicInbox::SearchIdx->new($ibx, 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                 SKIP: { skip 'DBD::SQLite not available', 2 };
125         }
126
127         my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
128         if ($have_xml_feed) {
129                 $path = "/test/blahblah\@example.com/t.atom";
130                 $res = cgi_run($path);
131                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
132                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
133                         "search returned atom");
134                 my $p = XML::Feed->parse(\($res->{body}));
135                 is($p->format, "Atom", "parsed atom feed");
136                 is(scalar $p->entries, 3, "parsed three entries");
137         } else {
138                 SKIP: { skip 'DBD::SQLite or XML::Feed missing', 2 };
139         }
140 }
141
142 done_testing();
143
144 sub cgi_run {
145         my %env = (
146                 PATH_INFO => $_[0],
147                 QUERY_STRING => $_[1] || "",
148                 SCRIPT_NAME => '',
149                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
150                 REQUEST_METHOD => $_[2] || "GET",
151                 GATEWAY_INTERFACE => 'CGI/1.1',
152                 HTTP_ACCEPT => '*/*',
153                 HTTP_HOST => 'test.example.com',
154         );
155         my ($in, $out, $err) = ("", "", "");
156         my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
157         run_script(['.cgi'], \%env, $rdr);
158         die "unexpected error: \$?=$?" if $?;
159         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
160         { head => $head, body => $body, err => $err }
161 }