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