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