]> Sergey Matveev's repositories - public-inbox.git/blob - t/view.t
update copyright headers and email addresses
[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 PublicInbox::View;
8
9 # plain text
10 {
11         my $body = <<EOF;
12 So and so wrote:
13 > keep this inline
14
15 OK
16
17 > Long and wordy reply goes here and it is split across multiple lines.
18 > We generate links to a separate full page where quoted-text is inline.
19 > This is
20 >
21 > Currently 12 lines
22 > See MAX_INLINE_QUOTED
23 > See MAX_INLINE_QUOTED
24 > See MAX_INLINE_QUOTED
25 > See MAX_INLINE_QUOTED
26 > See MAX_INLINE_QUOTED
27 > See MAX_INLINE_QUOTED
28 > See MAX_INLINE_QUOTED
29 > See MAX_INLINE_QUOTED
30
31 hello world
32 EOF
33         my $s = Email::Simple->create(
34                 header => [
35                         From => 'a@example.com',
36                         To => 'b@example.com',
37                         'Content-Type' => 'text/plain',
38                         'Message-ID' => '<hello@example.com>',
39                         Subject => 'this is a subject',
40                 ],
41                 body => $body,
42         )->as_string;
43         my $mime = Email::MIME->new($s);
44         my $html = PublicInbox::View::msg_html(undef, $mime);
45
46         # ghetto tests
47         like($html, qr!<a\nhref="\.\./raw"!s, "raw link present");
48         like($html, qr/hello world\b/, "body present");
49         like($html, qr/&gt; keep this inline/, "short quoted text is inline");
50         like($html, qr/<a\nid=[^>]+><\/a>&gt; Long and wordy/,
51                 "long quoted text is anchored");
52
53         # short page
54         my $pfx = "../hello%40example.com/f/";
55         $mime = Email::MIME->new($s);
56         my $short = PublicInbox::View::msg_html(undef, $mime, $pfx);
57         like($short, qr!<a\nhref="\.\./hello%40example\.com/f/!s,
58                 "MID link present");
59         like($short, qr/\n&gt; keep this inline/,
60                 "short quoted text is inline");
61         like($short, qr/<a\nhref="\Q$pfx\E#[^>]+>Long and wordy/,
62                 "long quoted text is made into a link");
63         ok(length($short) < length($html), "short page is shorter");
64 }
65
66 # multipart crap
67 {
68         my $parts = [
69                 Email::MIME->create(
70                         attributes => { content_type => 'text/plain', },
71                         body => 'hi',
72                 ),
73                 Email::MIME->create(
74                         attributes => { content_type => 'text/plain', },
75                         body => 'bye',
76                 )
77         ];
78         my $mime = Email::MIME->create(
79                 header_str => [
80                         From => 'a@example.com',
81                         Subject => 'blargh',
82                         'Message-ID' => '<blah@example.com>',
83                         'In-Reply-To' => '<irp@example.com>',
84                         ],
85                 parts => $parts,
86         );
87
88         my $html = PublicInbox::View::msg_html(undef, $mime);
89         like($html, qr/hi\n-+ part #2 -+\nbye\n/, "multipart split");
90 }
91
92 # multipart email with attached patch
93 {
94         my $parts = [
95                 Email::MIME->create(
96                         attributes => { content_type => 'text/plain', },
97                         body => 'hi, see attached patch',
98                 ),
99                 Email::MIME->create(
100                         attributes => {
101                                 content_type => 'text/plain',
102                                 filename => "foo.patch",
103                         },
104                         body => "--- a/file\n+++ b/file\n" .
105                                 "@@ -49, 7 +49,34 @@\n",
106                 ),
107         ];
108         my $mime = Email::MIME->create(
109                 header_str => [
110                         From => 'a@example.com',
111                         Subject => '[PATCH] asdf',
112                         'Message-ID' => '<patch@example.com>',
113                         ],
114                 parts => $parts,
115         );
116
117         my $html = PublicInbox::View::msg_html(undef, $mime);
118         like($html, qr!see attached patch\n-+ foo\.patch -+\n--- a/file\n!,
119                 "parts split with filename");
120 }
121
122 # multipart collapsed to single quoted-printable text/plain
123 {
124         my $parts = [
125                 Email::MIME->create(
126                         attributes => {
127                                 content_type => 'text/plain',
128                                 encoding => 'quoted-printable',
129                         },
130                         body => 'hi = bye',
131                 )
132         ];
133         my $mime = Email::MIME->create(
134                 header_str => [
135                         From => 'qp@example.com',
136                         Subject => 'QP',
137                         'Message-ID' => '<qp@example.com>',
138                         ],
139                 parts => $parts,
140         );
141
142         my $orig = $mime->body_raw;
143         my $html = PublicInbox::View::msg_html(undef, $mime);
144         like($orig, qr/hi =3D bye=/, "our test used QP correctly");
145         like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
146 }
147
148
149 {       # XXX dirty hack
150         use PublicInbox::MID qw/mid_compress/;
151         like(mid_compress('foo%bar@wtf'), qr/\A[a-f0-9]{40}\z/,
152                 "percent always converted to sha1 to workaround buggy httpds");
153         is(mid_compress('foobar@wtf'), 'foobar@wtf',
154                 'regular MID not compressed');
155 }
156
157 done_testing();