RT 4.0.22
[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/$encoding/,
51           "Its content type is $encoding" );
52     my $message_as_string = $mail[0]->bodyhandle->as_string();
53     $message_as_string = Encode::decode($encoding, $message_as_string);
54     like( $message_as_string , qr/H\x{e5}vard/,
55           "The message's content contains havard's name in $encoding");
56 }
57
58 {
59     my ($ticket) = mail_in_ticket('multipart-alternative-with-umlaut');
60     like( first_txn($ticket)->Content, qr/causes Error/,
61           "We recorded the content as containing 'causes error'");
62     is( count_attachs($ticket), 3,
63         "Has three attachments, presumably a text-plain, a text-html and a multipart alternative");
64 }
65
66 {
67     my ($ticket, @mail) = mail_in_ticket('text-html-with-umlaut');
68     like( first_attach($ticket)->Content, qr/causes Error/,
69           "We recorded the content as containing 'causes error'");
70     like( first_attach($ticket)->ContentType , qr/text\/html/,
71           "We recorded the content as text/html");
72     is (count_attachs($ticket), 1,
73         "Has one attachment, just a text-html");
74
75     is(@mail, 1);
76     is( $mail[0]->parts, 0, "generated correspondence mime entity does not have parts");
77     is( $mail[0]->head->mime_type , "text/plain", "The mime type is a plain");
78 }
79
80 {
81     my @InputEncodings = RT->Config->Get('EmailInputEncodings');
82     RT->Config->Set( EmailInputEncodings => 'koi8-r', @InputEncodings );
83     RT->Config->Set( EmailOutputEncoding => 'koi8-r' );
84
85     my ($ticket, @mail) = mail_in_ticket('russian-subject-no-content-type');
86     like( first_attach($ticket)->ContentType, qr/text\/plain/,
87           "We recorded the content type right");
88     is( count_attachs($ticket), 1,
89         "Has one attachment, presumably a text-plain");
90     is( $ticket->Subject, Encode::decode("UTF-8","тест тест"),
91         "Recorded the subject right");
92
93     is(@mail, 1);
94     is( $mail[0]->head->mime_type , "text/plain", "The only part is text/plain ");
95     like( $mail[0]->head->get("subject"), qr/\Q=?KOI8-R?B?W2V4YW1wbGUuY29tICM2XSBBdXRvUmVwbHk6INTF09Qg1MXT1A==?=\E/,
96           "The subject is encoded correctly");
97
98     RT->Config->Set(EmailInputEncodings => @InputEncodings );
99     RT->Config->Set(EmailOutputEncoding => 'utf-8');
100 }
101
102 {
103     my ($ticket, @mail) = mail_in_ticket('nested-rfc-822');
104     is( $ticket->Subject, "[Jonas Liljegren] Re: [Para] Niv\x{e5}er?");
105     like( first_attach($ticket)->ContentType, qr/multipart\/mixed/,
106           "We recorded the content type right");
107     is( count_attachs($ticket), 5,
108         "Has five attachments, presumably a text-plain and a message RFC 822 and another plain");
109
110     is(@mail, 1);
111     is( $mail[0]->head->mime_type , "text/plain", "The outgoing mail is plain text");
112
113     my $encoded_subject = $mail[0]->head->get("Subject");
114     chomp $encoded_subject;
115     my $subject = Encode::decode('MIME-Header',$encoded_subject);
116     like($subject, qr/Niv\x{e5}er/, "The subject matches the word - $subject");
117 }
118
119 {
120     my ($ticket) = mail_in_ticket('notes-uuencoded');
121     like( first_txn($ticket)->Content, qr/from Lotus Notes/,
122          "We recorded the content right");
123     is( count_attachs($ticket), 3, "Has three attachments");
124 }
125
126 {
127     my ($ticket) = mail_in_ticket('crashes-file-based-parser');
128     like( first_txn($ticket)->Content, qr/FYI/, "We recorded the content right");
129     is( count_attachs($ticket), 5, "Has five attachments");
130 }
131
132 {
133     my ($ticket) = mail_in_ticket('rt-send-cc');
134     my $cc = first_attach($ticket)->GetHeader('RT-Send-Cc');
135     like ($cc, qr/test$_/, "Found test $_") for 1..5;
136 }
137
138 {
139     diag "Regression test for #5248 from rt3.fsck.com";
140     my ($ticket) = mail_in_ticket('subject-with-folding-ws');
141     is ($ticket->Subject, 'test', 'correct subject');
142 }
143
144 {
145     diag "Regression test for #5248 from rt3.fsck.com";
146     my ($ticket) = mail_in_ticket('very-long-subject');
147     is ($ticket->Subject, '0123456789'x20, 'correct subject');
148 }
149
150 done_testing;