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