RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / sendmail.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5
6 use File::Spec ();
7 use Email::Abstract;
8
9 # We're not testing acls here.
10 my $everyone = RT::Group->new(RT->SystemUser);
11 $everyone->LoadSystemInternalGroup('Everyone');
12 $everyone->PrincipalObj->GrantRight( Right =>'SuperUser' );
13
14 # some utils
15 sub first_txn    { return $_[0]->Transactions->First }
16 sub first_attach { return first_txn($_[0])->Attachments->First }
17 sub count_attachs { return first_txn($_[0])->Attachments->Count }
18
19 sub mail_in_ticket {
20     my ($filename) = @_;
21     my $path = RT::Test::get_relocatable_file($filename,
22         (File::Spec->updir(), 'data', 'emails'));
23     my $content = RT::Test->file_content($path);
24
25     RT::Test->clean_caught_mails;
26     my ($status, $id) = RT::Test->send_via_mailgate( $content );
27     ok( !$status, "Fed $filename into mailgate");
28
29     my $ticket = RT::Ticket->new(RT->SystemUser);
30     $ticket->Load($id);
31     ok( $ticket->Id, "Successfully created ticket ".$ticket->Id);
32
33     my @mail = map {Email::Abstract->new($_)->cast('MIME::Entity')}
34         RT::Test->fetch_caught_mails;
35     return ($ticket, @mail);
36 }
37
38 {
39     my ($ticket) = mail_in_ticket('multipart-report');
40     like( first_txn($ticket)->Content , qr/The original message was received/, "It's the bounce");
41 }
42
43 for my $encoding ('ISO-8859-1', 'UTF-8') {
44     RT->Config->Set( EmailOutputEncoding => $encoding );
45
46     my ($ticket, @mail) = mail_in_ticket('new-ticket-from-iso-8859-1');
47     like (first_txn($ticket)->Content , qr/H\x{e5}vard/, "It's signed by havard. yay");
48
49     is(@mail, 1);
50     like( $mail[0]->head->get('Content-Type'), qr/multipart\/alternative/,
51           "Its content type is multipart/alternative" );
52
53     # The text/html part is guaranteed to not have had non-latin-1
54     # characters introduced by the HTML-to-text conversion, so it is
55     # guaranteed to be able to be represented in latin-1
56     like( $mail[0]->parts(1)->head->get('Content-Type'), qr/text\/html.+?$encoding/,
57           "Second part's content type is text/html $encoding" );
58     my $message_as_string = $mail[0]->parts(1)->bodyhandle->as_string();
59     $message_as_string = Encode::decode($encoding, $message_as_string);
60     like( $message_as_string , qr/H\x{e5}vard/,
61           "The message's content contains havard's name in $encoding");
62
63     # The text/plain part may have utf-8 characters in it.  Accept either encoding.
64     like( $mail[0]->parts(0)->head->get('Content-Type'), qr/text\/plain.+?(ISO-8859-1|UTF-8)/i,
65           "First part's content type is text/plain (ISO-8859-1 or UTF-8)" );
66
67     # Make sure it checks out in whatever encoding it ended up in
68     $mail[0]->parts(0)->head->get('Content-Type') =~ /text\/plain.+?(ISO-8859-1|UTF-8)/i;
69     my $found = $1 || $encoding;
70     $message_as_string = $mail[0]->parts(0)->bodyhandle->as_string();
71     $message_as_string = Encode::decode($found, $message_as_string);
72     like( $message_as_string , qr/H\x{e5}vard/,
73           "The message's content contains havard's name in $encoding");
74 }
75
76 {
77     my ($ticket) = mail_in_ticket('multipart-alternative-with-umlaut');
78     like( first_txn($ticket)->Content, qr/causes Error/,
79           "We recorded the content as containing 'causes error'");
80     is( count_attachs($ticket), 3,
81         "Has three attachments, presumably a text-plain, a text-html and a multipart alternative");
82 }
83
84 {
85     my ($ticket, @mail) = mail_in_ticket('text-html-with-umlaut');
86     like( first_attach($ticket)->Content, qr/causes Error/,
87           "We recorded the content as containing 'causes error'");
88     like( first_attach($ticket)->ContentType , qr/text\/html/,
89           "We recorded the content as text/html");
90     is (count_attachs($ticket), 1,
91         "Has one attachment, just a text-html");
92
93     is(@mail, 1);
94     is( $mail[0]->parts, 2, "generated correspondence mime entity has parts");
95     is( $mail[0]->parts(0)->head->mime_type , "text/plain", "The first part mime type is a plain");
96     is( $mail[0]->parts(1)->head->mime_type , "text/html", "The second part mime type is an html");
97 }
98
99 {
100     my @InputEncodings = RT->Config->Get('EmailInputEncodings');
101     RT->Config->Set( EmailInputEncodings => 'koi8-r', @InputEncodings );
102     RT->Config->Set( EmailOutputEncoding => 'koi8-r' );
103
104     my ($ticket, @mail) = mail_in_ticket('russian-subject-no-content-type');
105     like( first_attach($ticket)->ContentType, qr/text\/plain/,
106           "We recorded the content type right");
107     is( count_attachs($ticket), 1,
108         "Has one attachment, presumably a text-plain");
109     is( $ticket->Subject, Encode::decode("UTF-8","тест тест"),
110         "Recorded the subject right");
111
112     is(@mail, 1);
113     is( $mail[0]->head->mime_type , "multipart/alternative", "The top part is multipart/alternative");
114     is( $mail[0]->parts, 2, "generated correspondnece mime entity has parts");
115     is( $mail[0]->parts(0)->head->mime_type , "text/plain", "The first part is a plain");
116     is( $mail[0]->parts(1)->head->mime_type , "text/html", "The second part is an html");
117     like( $mail[0]->head->get("subject"), qr/\Q=?KOI8-R?B?W2V4YW1wbGUuY29tICM2XSBBdXRvUmVwbHk6INTF09Qg1MXT1A==?=\E/,
118           "The subject is encoded correctly");
119
120     RT->Config->Set(EmailInputEncodings => @InputEncodings );
121     RT->Config->Set(EmailOutputEncoding => 'utf-8');
122 }
123
124 {
125     my ($ticket, @mail) = mail_in_ticket('nested-rfc-822');
126     is( $ticket->Subject, "[Jonas Liljegren] Re: [Para] Niv\x{e5}er?");
127     like( first_attach($ticket)->ContentType, qr/multipart\/mixed/,
128           "We recorded the content type right");
129     is( count_attachs($ticket), 5,
130         "Has five attachments, presumably a text-plain and a message RFC 822 and another plain");
131
132     is(@mail, 1);
133     is( $mail[0]->head->mime_type , "multipart/alternative", "The top part is multipart/alternative");
134     is( $mail[0]->parts, 2, "generated correspondnece mime entity has parts");
135     is( $mail[0]->parts(0)->head->mime_type , "text/plain", "The first part is a plain");
136     is( $mail[0]->parts(1)->head->mime_type , "text/html", "The second part is an html");
137
138     my $encoded_subject = $mail[0]->head->get("Subject");
139     chomp $encoded_subject;
140     my $subject = Encode::decode('MIME-Header',$encoded_subject);
141     like($subject, qr/Niv\x{e5}er/, "The subject matches the word - $subject");
142 }
143
144 {
145     my ($ticket) = mail_in_ticket('notes-uuencoded');
146     like( first_txn($ticket)->Content, qr/from Lotus Notes/,
147          "We recorded the content right");
148     is( count_attachs($ticket), 3, "Has three attachments");
149 }
150
151 {
152     my ($ticket) = mail_in_ticket('crashes-file-based-parser');
153     like( first_txn($ticket)->Content, qr/FYI/, "We recorded the content right");
154     is( count_attachs($ticket), 5, "Has five attachments");
155 }
156
157 {
158     my ($ticket) = mail_in_ticket('rt-send-cc');
159     my $cc = first_attach($ticket)->GetHeader('RT-Send-Cc');
160     like ($cc, qr/test$_/, "Found test $_") for 1..5;
161 }
162
163 {
164     diag "Regression test for #5248 from rt3.fsck.com";
165     my ($ticket) = mail_in_ticket('subject-with-folding-ws');
166     is ($ticket->Subject, 'test', 'correct subject');
167 }
168
169 {
170     diag "Regression test for #5248 from rt3.fsck.com";
171     my ($ticket) = mail_in_ticket('very-long-subject');
172     is ($ticket->Subject, '0123456789'x20, 'correct subject');
173 }
174
175 done_testing;