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