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