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