]> Sergey Matveev's repositories - public-inbox.git/blob - t/view.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / view.t
1 # Copyright (C) 2013-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 Plack::Util;
8 use_ok 'PublicInbox::View';
9 use_ok 'PublicInbox::Config';
10
11 # FIXME: make this test less fragile
12 my $ctx = {
13         env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'http' },
14         -inbox => Plack::Util::inline_object(
15                 name => 'test',
16                 over => sub { undef },
17                 search => sub { undef },
18                 base_url => sub { 'http://example.com/' },
19                 cloneurl => sub {[]},
20                 nntp_url => sub {[]},
21                 max_git_epoch => sub { undef },
22                 description => sub { '' }),
23         www => Plack::Util::inline_object(style => sub { '' }),
24 };
25 $ctx->{-inbox}->{-primary_address} = 'test@example.com';
26
27 sub msg_html ($) {
28         my ($mime) = @_;
29
30         my $s = '';
31         my $r = PublicInbox::View::msg_html($ctx, $mime);
32         my $body = $r->[2];
33         while (defined(my $buf = $body->getline)) {
34                 $s .= $buf;
35         }
36         $body->close;
37         $s;
38 }
39
40 # plain text
41 {
42         my $body = <<EOF;
43 So and so wrote:
44 > keep this inline
45
46 OK
47
48 > Long and wordy reply goes here and it is split across multiple lines.
49 > We generate links to a separate full page where quoted-text is inline.
50 > This is
51 >
52 > Currently 12 lines
53 > See MAX_INLINE_QUOTED
54 > See MAX_INLINE_QUOTED
55 > See MAX_INLINE_QUOTED
56 > See MAX_INLINE_QUOTED
57 > See MAX_INLINE_QUOTED
58 > See MAX_INLINE_QUOTED
59 > See MAX_INLINE_QUOTED
60 > See MAX_INLINE_QUOTED
61
62 hello world
63 EOF
64         my $s = Email::Simple->create(
65                 header => [
66                         From => 'a@example.com',
67                         To => 'b@example.com',
68                         'Content-Type' => 'text/plain',
69                         'Message-ID' => '<hello@example.com>',
70                         Subject => 'this is a subject',
71                 ],
72                 body => $body,
73         )->as_string;
74         my $mime = Email::MIME->new($s);
75         my $html = msg_html($mime);
76
77         # ghetto tests
78         like($html, qr!<a\nhref="raw"!s, "raw link present");
79         like($html, qr/hello world\b/, "body present");
80         like($html, qr/&gt; keep this inline/, "short quoted text is inline");
81 }
82
83 # multipart crap
84 {
85         my $parts = [
86                 Email::MIME->create(
87                         attributes => { content_type => 'text/plain', },
88                         body => 'hi',
89                 ),
90                 Email::MIME->create(
91                         attributes => { content_type => 'text/plain', },
92                         body => 'bye',
93                 )
94         ];
95         my $mime = Email::MIME->create(
96                 header_str => [
97                         From => 'a@example.com',
98                         Subject => 'blargh',
99                         'Message-ID' => '<blah@example.com>',
100                         'In-Reply-To' => '<irp@example.com>',
101                         ],
102                 parts => $parts,
103         );
104
105         my $html = msg_html($mime);
106         like($html, qr/hi\n.*-- Attachment #2.*\nbye\n/s, "multipart split");
107 }
108
109 # multipart email with attached patch
110 {
111         my $parts = [
112                 Email::MIME->create(
113                         attributes => { content_type => 'text/plain', },
114                         body => 'hi, see attached patch',
115                 ),
116                 Email::MIME->create(
117                         attributes => {
118                                 content_type => 'text/plain',
119                                 filename => "foo&.patch",
120                         },
121                         body => "--- a/file\n+++ b/file\n" .
122                                 "@@ -49, 7 +49,34 @@\n",
123                 ),
124         ];
125         my $mime = Email::MIME->create(
126                 header_str => [
127                         From => 'a@example.com',
128                         Subject => '[PATCH] asdf',
129                         'Message-ID' => '<patch@example.com>',
130                         ],
131                 parts => $parts,
132         );
133
134         my $html = msg_html($mime);
135         like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
136                 "parts split with filename");
137 }
138
139 # multipart collapsed to single quoted-printable text/plain
140 {
141         my $parts = [
142                 Email::MIME->create(
143                         attributes => {
144                                 content_type => 'text/plain',
145                                 encoding => 'quoted-printable',
146                         },
147                         body => 'hi = bye',
148                 )
149         ];
150         my $mime = Email::MIME->create(
151                 header_str => [
152                         From => 'qp@example.com',
153                         Subject => 'QP',
154                         'Message-ID' => '<qp@example.com>',
155                         ],
156                 parts => $parts,
157         );
158
159         my $orig = $mime->body_raw;
160         my $html = msg_html($mime);
161         like($orig, qr/hi =3D bye=/, "our test used QP correctly");
162         like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
163 }
164
165 {
166         use PublicInbox::MID qw/id_compress/;
167
168         # n.b: this is probably invalid since we dropped CGI for PSGI:
169         like(id_compress('foo%bar@wtf'), qr/\A[a-f0-9]{40}\z/,
170                 "percent always converted to sha1 to workaround buggy httpds");
171
172         is(id_compress('foobar-wtf'), 'foobar-wtf',
173                 'regular ID not compressed');
174 }
175
176 {
177         my $cols = PublicInbox::View::COLS();
178         my @addr;
179         until (length(join(', ', @addr)) > ($cols * 2)) {
180                 push @addr, '"l, f" <a@a>';
181                 my $n = int(rand(20)) + 1;
182                 push @addr, ('x'x$n).'@x';
183         }
184         my $orig = join(', ', @addr);
185         my $res = PublicInbox::View::fold_addresses($orig.'');
186         isnt($res, $orig, 'folded result');
187         unlike($res, qr/l,\n\tf/s, '"last, first" no broken');
188         my @nospc = ($res, $orig);
189         s/\s+//g for @nospc;
190         is($nospc[0], $nospc[1], 'no addresses lost in translation');
191         my $tws = PublicInbox::View::fold_addresses($orig.' ');
192         # (Email::Simple drops leading whitespace, but not trailing)
193         $tws =~ s/ \z//;
194         is($tws, $res, 'not thrown off by trailing whitespace');
195 }
196
197 done_testing();