]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
t/search.t: update permissions check for OpenBSD
[public-inbox.git] / t / psgi_attach.t
1 # Copyright (C) 2016-2019 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 Email::MIME;
7 use PublicInbox::TestCommon;
8 my ($tmpdir, $for_destroy) = tmpdir();
9 my $maindir = "$tmpdir/main.git";
10 my $addr = 'test-public@example.com';
11 my $cfgpfx = "publicinbox.test";
12 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
13 require_mods(@mods);
14 use_ok $_ foreach @mods;
15 use PublicInbox::Import;
16 use PublicInbox::Git;
17 use PublicInbox::Config;
18 use PublicInbox::WWW;
19 use_ok 'PublicInbox::WwwAttach';
20 use Plack::Builder;
21 my $config = PublicInbox::Config->new(\<<EOF);
22 $cfgpfx.address=$addr
23 $cfgpfx.inboxdir=$maindir
24 EOF
25 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
26 my $git = PublicInbox::Git->new($maindir);
27 my $im = PublicInbox::Import->new($git, 'test', $addr);
28
29 {
30         open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
31         sysread($fh, my $buf, 8);
32         is(8, length($buf), 'read some random data');
33         my $qp = "abcdef=g\n==blah\n";
34         my $b64 = 'b64'.$buf."\n";
35         my $txt = "plain\ntext\npass\nthrough\n";
36         my $dot = "dotfile\n";
37         my $parts = [
38                 Email::MIME->create(
39                         attributes => {
40                                 filename => 'queue-pee',
41                                 content_type => 'text/plain',
42                                 encoding => 'quoted-printable'
43                         },
44                         body => $qp),
45                 Email::MIME->create(
46                         attributes => {
47                                 filename => 'bayce-sixty-four',
48                                 content_type => 'appication/octet-stream',
49                                 encoding => 'base64',
50                         },
51                         body => $b64),
52                 Email::MIME->create(
53                         attributes => {
54                                 filename => 'noop.txt',
55                                 content_type => 'text/plain',
56                         },
57                         body => $txt),
58                 Email::MIME->create(
59                         attributes => {
60                                 filename => '.dotfile',
61                                 content_type => 'text/plain',
62                         },
63                         body => $dot),
64         ];
65         my $mime = Email::MIME->create(
66                 parts => $parts,
67                 header_str => [ From => 'root@z', 'Message-Id' => '<Z@B>',
68                         Subject => 'hi']
69         );
70         $mime = $mime->as_string;
71         $mime =~ s/\r\n/\n/g; # normalize to LF only
72         $mime = Email::MIME->new($mime);
73         $im->add($mime);
74         $im->done;
75
76         my $www = PublicInbox::WWW->new($config);
77         test_psgi(sub { $www->call(@_) }, sub {
78                 my ($cb) = @_;
79                 my $res;
80                 $res = $cb->(GET('/test/Z%40B/'));
81                 my @href = ($res->content =~ /^href="([^"]+)"/gms);
82                 @href = grep(/\A[\d\.]+-/, @href);
83                 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
84                                 4-a.txt)],
85                         \@href, 'attachment links generated');
86
87                 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
88                 my $qp_res = $res->content;
89                 ok(length($qp_res) >= length($qp), 'QP length is close');
90                 like($qp_res, qr/\n\z/s, 'trailing newline exists');
91                 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
92                 $qp_res =~ s/\r\n/\n/g;
93                 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
94
95                 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
96                 is(quotemeta($res->content), quotemeta($b64),
97                         'Base64 matches exactly');
98
99                 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
100                 my $txt_res = $res->content;
101                 ok(length($txt_res) >= length($txt),
102                         'plain text almost matches');
103                 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
104                 is(index($txt_res, $txt), 0, 'plain text not truncated');
105
106                 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
107                 my $dot_res = $res->content;
108                 ok(length($dot_res) >= length($dot), 'dot almost matches');
109                 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
110                 is($res->content, $dot_res, 'user-specified filename is OK');
111
112         });
113 }
114 done_testing();