]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_attach.t
view: do not offer links to 0-byte multipart attachments
[public-inbox.git] / t / psgi_attach.t
1 # Copyright (C) 2016-2020 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 $maindir = "$tmpdir/main.git";
9 my $addr = 'test-public@example.com';
10 my $cfgpfx = "publicinbox.test";
11 my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
12 require_mods(@mods);
13 use_ok $_ foreach @mods;
14 use_ok 'PublicInbox::WWW';
15 use PublicInbox::Import;
16 use PublicInbox::Git;
17 use PublicInbox::Config;
18 use PublicInbox::Eml;
19 use_ok 'PublicInbox::WwwAttach';
20 my $config = PublicInbox::Config->new(\<<EOF);
21 $cfgpfx.address=$addr
22 $cfgpfx.inboxdir=$maindir
23 EOF
24 my $git = PublicInbox::Git->new($maindir);
25 my $im = PublicInbox::Import->new($git, 'test', $addr);
26 $im->init_bare;
27
28 {
29         my $qp = "abcdef=g\n==blah\n";
30         my $b64 = "b64\xde\xad\xbe\xef\n";
31         my $txt = "plain\ntext\npass\nthrough\n";
32         my $dot = "dotfile\n";
33         $im->add(eml_load('t/psgi_attach.eml'));
34         $im->add(eml_load('t/data/message_embed.eml'));
35         $im->done;
36
37         my $www = PublicInbox::WWW->new($config);
38         test_psgi(sub { $www->call(@_) }, sub {
39                 my ($cb) = @_;
40                 my $res;
41                 $res = $cb->(GET('/test/Z%40B/'));
42                 my @href = ($res->content =~ /^href="([^"]+)"/gms);
43                 @href = grep(/\A[\d\.]+-/, @href);
44                 is_deeply([qw(1-queue-pee 2-bayce-sixty-four 3-noop.txt
45                                 4-a.txt)],
46                         \@href, 'attachment links generated');
47
48                 $res = $cb->(GET('/test/Z%40B/1-queue-pee'));
49                 my $qp_res = $res->content;
50                 ok(length($qp_res) >= length($qp), 'QP length is close');
51                 like($qp_res, qr/\n\z/s, 'trailing newline exists');
52                 # is(index($qp_res, $qp), 0, 'QP trailing newline is there');
53                 $qp_res =~ s/\r\n/\n/g;
54                 is(index($qp_res, $qp), 0, 'QP trailing newline is there');
55
56                 $res = $cb->(GET('/test/Z%40B/2-base-sixty-four'));
57                 is(quotemeta($res->content), quotemeta($b64),
58                         'Base64 matches exactly');
59
60                 $res = $cb->(GET('/test/Z%40B/3-noop.txt'));
61                 my $txt_res = $res->content;
62                 ok(length($txt_res) >= length($txt),
63                         'plain text almost matches');
64                 like($txt_res, qr/\n\z/s, 'trailing newline exists in text');
65                 is(index($txt_res, $txt), 0, 'plain text not truncated');
66
67                 $res = $cb->(GET('/test/Z%40B/4-a.txt'));
68                 my $dot_res = $res->content;
69                 ok(length($dot_res) >= length($dot), 'dot almost matches');
70                 $res = $cb->(GET('/test/Z%40B/4-any-filename.txt'));
71                 is($res->content, $dot_res, 'user-specified filename is OK');
72
73                 my $mid = '20200418222508.GA13918@dcvr';
74                 my $irt = '20200418222020.GA2745@dcvr';
75                 $res = $cb->(GET("/test/$mid/"));
76                 unlike($res->content, qr! multipart/mixed, Size: 0 bytes!,
77                         '0-byte download not offered');
78                 like($res->content, qr/\bhref="2-embed2x\.eml"/s,
79                         'href to message/rfc822 attachment visible');
80                 like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,
81                         'href to nested message/rfc822 attachment visible');
82
83                 $res = $cb->(GET("/test/$mid/2-embed2x.eml"));
84                 my $eml = PublicInbox::Eml->new(\($res->content));
85                 is_deeply([ $eml->header_raw('Message-ID') ], [ "<$irt>" ],
86                         'got attached eml');
87                 my @subs = $eml->subparts;
88                 is(scalar(@subs), 2, 'attachment had 2 subparts');
89                 like($subs[0]->body_str, qr/^testing embedded message\n*\z/sm,
90                         '1st attachment is as expected');
91                 is($subs[1]->header('Content-Type'), 'message/rfc822',
92                         '2nd attachment is as expected');
93
94                 $res = $cb->(GET("/test/$mid/2.1.2-test.eml"));
95                 $eml = PublicInbox::Eml->new(\($res->content));
96                 is_deeply([ $eml->header_raw('Message-ID') ],
97                         [ '<20200418214114.7575-1-e@yhbt.net>' ],
98                         'nested eml retrieved');
99         });
100 }
101 done_testing();