]> Sergey Matveev's repositories - public-inbox.git/blob - t/cgi.t
52f80e88e2fbd1545ed105b806eb6a5ec4fa72e1
[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 Email::MIME;
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;
43
44 {
45         local $ENV{HOME} = $home;
46
47         # inject some messages:
48         my $mime = Email::MIME->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         $im->add($mime);
59
60         # deliver a reply, too
61         my $reply = Email::MIME->new(<<EOF);
62 From: You <you\@example.com>
63 To: Me <me\@example.com>
64 Cc: $addr
65 In-Reply-To: <blah\@example.com>
66 Message-Id: <blahblah\@example.com>
67 Subject: Re: hihi
68 Date: Thu, 01 Jan 1970 00:00:01 +0000
69
70 Me wrote:
71 > zzzzzz
72
73 what?
74 EOF
75         $im->add($reply);
76
77         my $slashy_mid = 'slashy/asdf@example.com';
78         my $slashy = Email::MIME->new(<<EOF);
79 From: You <you\@example.com>
80 To: Me <me\@example.com>
81 Cc: $addr
82 Message-Id: <$slashy_mid>
83 Subject: Re: hihi
84 Date: Thu, 01 Jan 1970 00:00:01 +0000
85
86 slashy
87 EOF
88         $im->add($slashy);
89         $im->done;
90
91         my $res = cgi_run("/test/slashy/asdf\@example.com/raw");
92         like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
93                 "slashy mid raw hit");
94 }
95
96 # retrieve thread as an mbox
97 {
98         local $ENV{HOME} = $home;
99         my $path = "/test/blahblah\@example.com/t.mbox.gz";
100         my $res = cgi_run($path);
101         like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
102         my $indexed;
103         eval {
104                 require DBD::SQLite;
105                 require PublicInbox::SearchIdx;
106                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
107                 $s->index_sync;
108                 $indexed = 1;
109         };
110         if ($indexed) {
111                 $res = cgi_run($path);
112                 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
113                 eval {
114                         require IO::Uncompress::Gunzip;
115                         my $in = $res->{body};
116                         my $out;
117                         IO::Uncompress::Gunzip::gunzip(\$in => \$out);
118                         like($out, qr/^From /m, "From lines in mbox");
119                 };
120         } else {
121                 like($res->{head}, qr/^Status: 501 /, "search not available");
122                 SKIP: { skip 'DBD::SQLite not available', 2 };
123         }
124
125         my $have_xml_treepp = eval { require XML::TreePP; 1 } if $indexed;
126         if ($have_xml_treepp) {
127                 $path = "/test/blahblah\@example.com/t.atom";
128                 $res = cgi_run($path);
129                 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
130                 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
131                         "search returned atom");
132                 my $t = XML::TreePP->new->parse($res->{body});
133                 is(scalar @{$t->{feed}->{entry}}, 3, "parsed three entries");
134                 like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
135                                 'looks like an an Atom feed');
136         } else {
137                 SKIP: { skip 'DBD::SQLite or XML::TreePP missing', 2 };
138         }
139 }
140
141 done_testing();
142
143 sub cgi_run {
144         my %env = (
145                 PATH_INFO => $_[0],
146                 QUERY_STRING => $_[1] || "",
147                 SCRIPT_NAME => '',
148                 REQUEST_URI => $_[0] . ($_[1] ? "?$_[1]" : ''),
149                 REQUEST_METHOD => $_[2] || "GET",
150                 GATEWAY_INTERFACE => 'CGI/1.1',
151                 HTTP_ACCEPT => '*/*',
152                 HTTP_HOST => 'test.example.com',
153         );
154         my ($in, $out, $err) = ("", "", "");
155         my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
156         run_script(['.cgi'], \%env, $rdr);
157         die "unexpected error: \$?=$?" if $?;
158         my ($head, $body) = split(/\r\n\r\n/, $out, 2);
159         { head => $head, body => $body, err => $err }
160 }