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