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