]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
update copyrights for 2021
[public-inbox.git] / t / psgi_attach.t
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 my ($tmpdir, $for_destroy) = tmpdir();
8 my $inboxdir = "$tmpdir/main.git";
9 my $addr = 'test-public@example.com';
10 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
11 require_mods(@mods);
12 use_ok $_ foreach @mods;
13 use_ok 'PublicInbox::WWW';
14 use PublicInbox::Import;
15 use PublicInbox::Git;
16 use PublicInbox::Config;
17 use PublicInbox::Eml;
18 use_ok 'PublicInbox::WwwAttach';
19
20 my $cfgpath = "$tmpdir/config";
21 open my $fh, '>', $cfgpath or BAIL_OUT $!;
22 print $fh <<EOF or BAIL_OUT $!;
23 [publicinbox "test"]
24         address = $addr
25         inboxdir = $inboxdir
26 EOF
27 close $fh or BAIL_OUT $!;
28 my $config = PublicInbox::Config->new($cfgpath);
29 my $git = PublicInbox::Git->new($inboxdir);
30 my $im = PublicInbox::Import->new($git, 'test', $addr);
31 $im->init_bare;
32
33 my $qp = "abcdef=g\n==blah\n";
34 my $b64 = "b64\xde\xad\xbe\xef\n";
35 my $txt = "plain\ntext\npass\nthrough\n";
36 my $dot = "dotfile\n";
37 $im->add(eml_load('t/psgi_attach.eml'));
38 $im->add(eml_load('t/data/message_embed.eml'));
39 $im->done;
40
41 my $www = PublicInbox::WWW->new($config);
42 my $client = sub {
43         my ($cb) = @_;
44         my $res;
45         $res = $cb->(GET('/test/Z%40B/'));
46         my @href = ($res->content =~ /^href="([^"]+)"/gms);
47         @href = grep(/\A[\d\.]+-/, @href);
48         is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
49                         4-a.txt)],
50                 \@href, 'attachment links generated');
51
52         $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
53         my $qp_res = $res->content;
54         ok(length($qp_res) >= length($qp), 'QP length is close');
55         like($qp_res, qr/\n\z/s, 'trailing newline exists');
56         # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
57         $qp_res =~ s/\r\n/\n/g;
58         is(index($qp_res, $qp), 0, 'QP trailing newline is there');
59
60         $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
61         is(quotemeta($res->content), quotemeta($b64),
62                 'Base64 matches exactly');
63
64         $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
65         my $txt_res = $res->content;
66         ok(length($txt_res) >= length($txt),
67                 'plain text almost matches');
68         like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
69         is(index($txt_res, $txt), 0, 'plain text not truncated');
70
71         $res = $cb->(GET('/test/Z%40B/4-a.txt'));
72         my $dot_res = $res->content;
73         ok(length($dot_res) >= length($dot), 'dot almost matches');
74         $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
75         is($res->content, $dot_res, 'user-specified filename is OK');
76
77         my $mid = '20200418222508.GA13918@dcvr';
78         my $irt = '20200418222020.GA2745@dcvr';
79         $res = $cb->(GET("/test/$mid/"));
80         unlike($res->content, qr! multipart/mixed, Size: 0 bytes!,
81                 '0-byte download not offered');
82         like($res->content, qr/\bhref="2-embed2x\.eml"/s,
83                 'href to message/rfc822 attachment visible');
84         like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,
85                 'href to nested message/rfc822 attachment visible');
86
87         $res = $cb->(GET("/test/$mid/2-embed2x.eml"));
88         my $eml = PublicInbox::Eml->new(\($res->content));
89         is_deeply([ $eml->header_raw('Message-ID') ], [ "<$irt>" ],
90                 'got attached eml');
91         my @subs = $eml->subparts;
92         is(scalar(@subs), 2, 'attachment had 2 subparts');
93         like($subs[0]->body_str, qr/^testing embedded message\n*\z/sm,
94                 '1st attachment is as expected');
95         is($subs[1]->header('Content-Type'), 'message/rfc822',
96                 '2nd attachment is as expected');
97
98         $res = $cb->(GET("/test/$mid/2.1.2-test.eml"));
99         $eml = PublicInbox::Eml->new(\($res->content));
100         is_deeply([ $eml->header_raw('Message-ID') ],
101                 [ '<20200418214114.7575-1-e@yhbt.net>' ],
102                 'nested eml retrieved');
103 };
104
105 test_psgi(sub { $www->call(@_) }, $client);
106 SKIP: {
107         diag 'testing with index indexed';
108         require_mods('DBD::SQLite', 19);
109         my $env = { PI_CONFIG => $cfgpath };
110         ok(run_script(['-index', $inboxdir], $env), 'indexed');
111
112         test_psgi(sub { $www->call(@_) }, $client);
113
114         require_mods(qw(Plack::Test::ExternalServer), 18);
115         my $sock = tcp_server() or die;
116         my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
117         my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
118         my $td = start_script($cmd, $env, { 3 => $sock });
119         my ($h, $p) = ($sock->sockhost, $sock->sockport);
120         local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
121         Plack::Test::ExternalServer::test_psgi(client => $client);
122 }
123 done_testing();