]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
96c627c384e8d3ce1acf1c3a83d3eba0140d280b
[public-inbox.git] / t / cgi.t
1 # Copyright (C) 2014-2020 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 PublicInbox::Eml;
9 use PublicInbox::TestCommon;
10 use PublicInbox::Import;
11 require_mods(qw(Plack::Handler::CGI Plack::Util));
12 my ($tmpdir, $for_destroy) = tmpdir();
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 $addr = 'test-public@example.com';
18
19 {
20         is(1, mkdir($home, 0755), "setup ~/ for testing");
21         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
22         PublicInbox::Import::init_bare($maindir);
23
24         open my $fh, '>', "$maindir/description" or die "open: $!\n";
25         print $fh "test for public-inbox\n";
26         close $fh or die "close: $!\n";
27         open $fh, '>>', $pi_config or die;
28         print $fh <<EOF or die;
29 [publicinbox "test"]
30         address = $addr
31         inboxdir = $maindir
32         indexlevel = basic
33 EOF
34         close $fh or die "close: $!\n";
35 }
36
37 use_ok 'PublicInbox::Inbox';
38 use_ok 'PublicInbox::InboxWritable';
39 use_ok 'PublicInbox::Config';
40 my $cfg = PublicInbox::Config->new($pi_config);
41 my $ibx = $cfg->lookup_name('test');
42 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
43
44 {
45         local $ENV{HOME} = $home;
46
47         # inject some messages:
48         my $mime = PublicInbox::Eml->new(<<EOF);
49 From: Me <me\@example.com>
50 To: You <you\@example.com>
51 Cc: $addr
52 Message-Id: <blah\@example.com>
53 Subject: hihi
54 Date: Thu, 01 Jan 1970 00:00:00 +0000
55
56 zzzzzz
57 EOF
58         ok($im->add($mime), 'added initial message');
59
60         $mime->header_set('Message-ID', '<toobig@example.com>');
61         $mime->body_str_set("z\n" x 1024);
62         ok($im->add($mime), 'added big message');
63
64         # deliver a reply, too
65         $mime = PublicInbox::Eml->new(<<EOF);
66 From: You <you\@example.com>
67 To: Me <me\@example.com>
68 Cc: $addr
69 In-Reply-To: <blah\@example.com>
70 Message-Id: <blahblah\@example.com>
71 Subject: Re: hihi
72 Date: Thu, 01 Jan 1970 00:00:01 +0000
73
74 Me wrote:
75 > zzzzzz
76
77 what?
78 EOF
79         ok($im->add($mime), 'added reply');
80
81         my $slashy_mid = 'slashy/asdf@example.com';
82         my $slashy = PublicInbox::Eml->new(<<EOF);
83 From: You <you\@example.com>
84 To: Me <me\@example.com>
85 Cc: $addr
86 Message-Id: <$slashy_mid>
87 Subject: Re: hihi
88 Date: Thu, 01 Jan 1970 00:00:01 +0000
89
90 slashy
91 EOF
92         ok($im->add($slashy), 'added slash');
93         $im->done;
94
95         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
96         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
97                 "slashy mid raw hit");
98 }
99
100 # retrieve thread as an mbox
101 {
102         local $ENV{HOME} = $home;
103         my $path = "/test/blahblah\@example.com/t.mbox.gz";
104         my $res = cgi_run($path);
105         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
106         my $cmd = ['-index', $ibx->{inboxdir}, '--max-size=2k'];
107         my $opt = { 2 => \(my $err) };
108         my $indexed = run_script($cmd, undef, $opt);
109         if ($indexed) {
110                 $res = cgi_run($path);
111                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
112                 eval {
113                         require IO::Uncompress::Gunzip;
114                         my $in = $res->{body};
115                         my $out;
116                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
117                         like($out, qr/^From /m, "From lines in mbox");
118                 };
119                 $res = cgi_run('/test/toobig@example.com/');
120                 like($res->{head}, qr/^Status: 300 /,
121                         'did not index or return >max-size message');
122                 like($err, qr/skipping [a-f0-9]{40,}/,
123                         'warned about skipping large OID');
124         } else {
125                 like($res->{head}, qr/^Status: 501 /, "search not available");
126                 SKIP: { skip 'DBD::SQLite not available', 4 };
127         }
128
129         my $have_xml_treepp = eval { require XML::TreePP; 1 } if $indexed;
130         if ($have_xml_treepp) {
131                 $path = "/test/blahblah\@example.com/t.atom";
132                 $res = cgi_run($path);
133                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
134                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
135                         "search returned atom");
136                 my $t = XML::TreePP->new->parse($res->{body});
137                 is(scalar @{$t->{feed}->{entry}}, 3, "parsed three entries");
138                 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
139                                 'looks like an an Atom feed');
140         } else {
141                 SKIP: { skip 'DBD::SQLite or XML::TreePP missing', 2 };
142         }
143 }
144
145 done_testing();
146
147 sub cgi_run {
148         my %env = (
149                 PATH_INFO => $_[0],
150                 QUERY_STRING => $_[1] || "",
151                 SCRIPT_NAME => '',
152                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
153                 REQUEST_METHOD => $_[2] || "GET",
154                 GATEWAY_INTERFACE => 'CGI/1.1',
155                 HTTP_ACCEPT => '*/*',
156                 HTTP_HOST => 'test.example.com',
157         );
158         my ($in, $out, $err) = ("", "", "");
159         my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
160         run_script(['.cgi'], \%env, $rdr);
161         die "unexpected error: \$?=$? ($err)" if $?;
162         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
163         { head => $head, body => $body, err => $err }
164 }