5 use RT::Test tests => 141;
9 use RT::Action::SendEmail;
11 my @_outgoing_messages;
14 #We're not testing acls here.
15 my $everyone = RT::Group->new(RT->SystemUser);
16 $everyone->LoadSystemInternalGroup('Everyone');
17 $everyone->PrincipalObj->GrantRight( Right =>'SuperUser' );
20 is (__PACKAGE__, 'main', "We're operating in the main package");
23 no warnings qw/redefine/;
24 *RT::Action::SendEmail::SendMessage = sub {
28 main::_fired_scrip($self->ScripObj);
29 main::is(ref($MIME) , 'MIME::Entity', "hey, look. it's a mime entity");
34 sub first_txn { return $_[0]->Transactions->First }
35 sub first_attach { return first_txn($_[0])->Attachments->First }
37 sub count_txns { return $_[0]->Transactions->Count }
38 sub count_attachs { return first_txn($_[0])->Attachments->Count }
40 # instrument SendEmail to pass us what it's about to send.
41 # create a regular ticket
43 my $parser = RT::EmailParser->new();
45 # Let's test to make sure a multipart/report is processed correctly
46 my $multipart_report_email = RT::Test::get_relocatable_file('multipart-report',
47 (File::Spec->updir(), 'data', 'emails'));
48 my $content = RT::Test->file_content($multipart_report_email);
49 # be as much like the mail gateway as possible.
50 use RT::Interface::Email;
51 my %args = (message => $content, queue => 1, action => 'correspond');
52 my ($status, $msg) = RT::Interface::Email::Gateway(\%args);
53 ok($status, "successfuly used Email::Gateway interface") or diag("error: $msg");
54 my $tickets = RT::Tickets->new(RT->SystemUser);
55 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
56 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
57 my $tick= $tickets->First();
58 isa_ok($tick, "RT::Ticket", "got a ticket object");
59 ok ($tick->Id, "found ticket ".$tick->Id);
60 like (first_txn($tick)->Content , qr/The original message was received/, "It's the bounce");
63 # make sure it fires scrips.
64 is ($#scrips_fired, 1, "Fired 2 scrips on ticket creation");
71 $parser->ParseMIMEEntityFromScalar('From: root@localhost
73 Subject: This is a test of new ticket creation as an unknown user
81 my $ticket = RT::Ticket->new(RT->SystemUser);
82 my ($id, undef, $create_msg ) = $ticket->Create(Requestor => ['root@localhost'], Queue => 'general', Subject => 'I18NTest', MIMEObj => $parser->Entity);
84 $tickets = RT::Tickets->new(RT->SystemUser);
85 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
86 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
87 $tick = $tickets->First();
88 ok ($tick->Id, "found ticket ".$tick->Id);
89 is ($tick->Subject , 'I18NTest', "failed to create the new ticket from an unprivileged account");
91 # make sure it fires scrips.
92 is ($#scrips_fired, 1, "Fired 2 scrips on ticket creation");
93 # make sure it sends an autoreply
94 # make sure it sends a notification to adminccs
97 # we need to swap out SendMessage to test the new things we care about;
98 &utf8_redef_sendmessage;
100 # create an iso 8859-1 ticket
103 my $iso_8859_1_ticket_email = RT::Test::get_relocatable_file(
104 'new-ticket-from-iso-8859-1', (File::Spec->updir(), 'data', 'emails'));
105 $content = RT::Test->file_content($iso_8859_1_ticket_email);
109 $parser->ParseMIMEEntityFromScalar($content);
112 # be as much like the mail gateway as possible.
113 use RT::Interface::Email;
115 %args = (message => $content, queue => 1, action => 'correspond');
116 RT::Interface::Email::Gateway(\%args);
117 $tickets = RT::Tickets->new(RT->SystemUser);
118 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
119 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
120 $tick = $tickets->First();
121 ok ($tick->Id, "found ticket ".$tick->Id);
123 like (first_txn($tick)->Content , qr/H\x{e5}vard/, "It's signed by havard. yay");
126 # make sure it fires scrips.
127 is ($#scrips_fired, 1, "Fired 2 scrips on ticket creation");
128 # make sure it sends an autoreply
131 # make sure it sends a notification to adminccs
133 # If we correspond, does it do the right thing to the outbound messages?
135 $parser->ParseMIMEEntityFromScalar($content);
136 ($id, $msg) = $tick->Comment(MIMEObj => $parser->Entity);
139 $parser->ParseMIMEEntityFromScalar($content);
140 ($id, $msg) = $tick->Correspond(MIMEObj => $parser->Entity);
147 # we need to swap out SendMessage to test the new things we care about;
148 &iso8859_redef_sendmessage;
149 RT->Config->Set( EmailOutputEncoding => 'iso-8859-1' );
150 # create an iso 8859-1 ticket
153 $content = RT::Test->file_content($iso_8859_1_ticket_email);
154 # be as much like the mail gateway as possible.
155 use RT::Interface::Email;
157 %args = (message => $content, queue => 1, action => 'correspond');
158 RT::Interface::Email::Gateway(\%args);
159 $tickets = RT::Tickets->new(RT->SystemUser);
160 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
161 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
162 $tick = $tickets->First();
163 ok ($tick->Id, "found ticket ".$tick->Id);
165 like (first_txn($tick)->Content , qr/H\x{e5}vard/, "It's signed by havard. yay");
168 # make sure it fires scrips.
169 is ($#scrips_fired, 1, "Fired 2 scrips on ticket creation");
170 # make sure it sends an autoreply
173 # make sure it sends a notification to adminccs
176 # If we correspond, does it do the right thing to the outbound messages?
178 $parser->ParseMIMEEntityFromScalar($content);
179 ($id, $msg) = $tick->Comment(MIMEObj => $parser->Entity);
182 $parser->ParseMIMEEntityFromScalar($content);
183 ($id, $msg) = $tick->Correspond(MIMEObj => $parser->Entity);
189 push @scrips_fired, $scrip;
192 sub utf8_redef_sendmessage {
193 no warnings qw/redefine/;
194 *RT::Action::SendEmail::SendMessage = sub {
198 my $scrip = $self->ScripObj->id;
199 ok(1, $self->ScripObj->ConditionObj->Name . " ".$self->ScripObj->ActionObj->Name);
200 main::_fired_scrip($self->ScripObj);
201 $MIME->make_singlepart;
202 main::is( ref($MIME) , 'MIME::Entity',
203 "hey, look. it's a mime entity" );
204 main::is( ref( $MIME->head ) , 'MIME::Head',
205 "its mime header is a mime header. yay" );
206 main::like( $MIME->head->get('Content-Type') , qr/utf-8/,
207 "Its content type is utf-8" );
208 my $message_as_string = $MIME->bodyhandle->as_string();
210 $message_as_string = Encode::decode_utf8($message_as_string);
212 $message_as_string , qr/H\x{e5}vard/,
213 "The message's content contains havard's name. this will fail if it's not utf8 out");
218 sub iso8859_redef_sendmessage {
219 no warnings qw/redefine/;
220 *RT::Action::SendEmail::SendMessage = sub {
224 my $scrip = $self->ScripObj->id;
225 ok(1, $self->ScripObj->ConditionObj->Name . " ".$self->ScripObj->ActionObj->Name);
226 main::_fired_scrip($self->ScripObj);
227 $MIME->make_singlepart;
228 main::is( ref($MIME) , 'MIME::Entity',
229 "hey, look. it's a mime entity" );
230 main::is( ref( $MIME->head ) , 'MIME::Head',
231 "its mime header is a mime header. yay" );
232 main::like( $MIME->head->get('Content-Type') , qr/iso-8859-1/,
233 "Its content type is iso-8859-1 - " . $MIME->head->get("Content-Type") );
234 my $message_as_string = $MIME->bodyhandle->as_string();
236 $message_as_string = Encode::decode("iso-8859-1",$message_as_string);
238 $message_as_string , qr/H\x{e5}vard/, "The message's content contains havard's name. this will fail if it's not utf8 out");
243 my $alt_umlaut_email = RT::Test::get_relocatable_file(
244 'multipart-alternative-with-umlaut', (File::Spec->updir(), 'data', 'emails'));
245 $content = RT::Test->file_content($alt_umlaut_email);
247 $parser->ParseMIMEEntityFromScalar($content);
250 # be as much like the mail gateway as possible.
252 no warnings qw/redefine/;
253 local *RT::Action::SendEmail::SendMessage = sub { return 1};
255 %args = (message => $content, queue => 1, action => 'correspond');
256 RT::Interface::Email::Gateway(\%args);
257 # TODO: following 5 lines should replaced by get_latest_ticket_ok()
258 $tickets = RT::Tickets->new(RT->SystemUser);
259 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
260 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
261 $tick = $tickets->First();
263 ok ($tick->Id, "found ticket ".$tick->Id);
265 like (first_txn($tick)->Content , qr/causes Error/, "We recorded the content right as text-plain");
266 is (count_attachs($tick) , 3 , "Has three attachments, presumably a text-plain, a text-html and a multipart alternative");
271 my $text_html_email = RT::Test::get_relocatable_file('text-html-with-umlaut',
272 (File::Spec->updir(), 'data', 'emails'));
273 $content = RT::Test->file_content($text_html_email);
275 $parser->ParseMIMEEntityFromScalar($content);
278 # be as much like the mail gateway as possible.
279 &text_html_redef_sendmessage;
281 %args = (message => $content, queue => 1, action => 'correspond');
282 RT::Interface::Email::Gateway(\%args);
283 $tickets = RT::Tickets->new(RT->SystemUser);
284 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
285 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
286 $tick = $tickets->First();
287 ok ($tick->Id, "found ticket ".$tick->Id);
289 like (first_attach($tick)->Content , qr/causes Error/, "We recorded the content as containing 'causes error'") or diag( first_attach($tick)->Content );
290 like (first_attach($tick)->ContentType , qr/text\/html/, "We recorded the content as text/html");
291 is (count_attachs($tick), 1 , "Has one attachment, presumably a text-html and a multipart alternative");
293 sub text_html_redef_sendmessage {
294 no warnings qw/redefine/;
295 *RT::Action::SendEmail::SendMessage = sub {
298 return (1) unless ($self->ScripObj->ScripActionObj->Name eq "Notify AdminCcs" );
299 is ($MIME->parts, 0, "generated correspondence mime entity
300 does not have parts");
301 is ($MIME->head->mime_type , "text/plain", "The mime type is a plain");
306 my $russian_email = RT::Test::get_relocatable_file('text-html-in-russian',
307 (File::Spec->updir(), 'data', 'emails'));
308 $content = RT::Test->file_content($russian_email);
310 $parser->ParseMIMEEntityFromScalar($content);
312 # be as much like the mail gateway as possible.
313 &text_html_redef_sendmessage;
315 %args = (message => $content, queue => 1, action => 'correspond');
320 local $SIG{__WARN__} = sub {
321 push @warnings, "@_";
324 RT::Interface::Email::Gateway(\%args);
328 'need a better approach of encoding converter, should be fixed in 4.2';
329 ok( @warnings == 1 || @warnings == 2, "1 or 2 warnings are ok" );
330 ok( @warnings == 1 || ( @warnings == 2 && $warnings[1] eq $warnings[0] ),
331 'if there are 2 warnings, they should be same' );
335 qr/\QEncoding error: "\x{041f}" does not map to iso-8859-1/,
336 "The badly formed Russian spam we have isn't actually well-formed UTF8, which makes Encode (correctly) warn",
342 $tickets = RT::Tickets->new(RT->SystemUser);
343 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
344 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
345 $tick = $tickets->First();
346 ok ($tick->Id, "found ticket ".$tick->Id);
348 like (first_attach($tick)->ContentType , qr/text\/html/, "We recorded the content right as text-html");
350 is (count_attachs($tick) ,1 , "Has one attachment, presumably a text-html and a multipart alternative");
354 RT->Config->Set( EmailInputEncodings => 'koi8-r', RT->Config->Get('EmailInputEncodings') );
355 RT->Config->Set( EmailOutputEncoding => 'koi8-r' );
356 my $russian_subject_email = RT::Test::get_relocatable_file(
357 'russian-subject-no-content-type', (File::Spec->updir(), 'data', 'emails'));
358 $content = RT::Test->file_content($russian_subject_email);
360 $parser->ParseMIMEEntityFromScalar($content);
363 # be as much like the mail gateway as possible.
364 &text_plain_russian_redef_sendmessage;
365 %args = (message => $content, queue => 1, action => 'correspond');
366 RT::Interface::Email::Gateway(\%args);
367 $tickets = RT::Tickets->new(RT->SystemUser);
368 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
369 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
370 $tick= $tickets->First();
371 ok ($tick->Id, "found ticket ".$tick->Id);
373 like (first_attach($tick)->ContentType , qr/text\/plain/, "We recorded the content type right");
374 is (count_attachs($tick) ,1 , "Has one attachment, presumably a text-plain");
375 is ($tick->Subject, "\x{442}\x{435}\x{441}\x{442} \x{442}\x{435}\x{441}\x{442}", "Recorded the subject right");
376 sub text_plain_russian_redef_sendmessage {
377 no warnings qw/redefine/;
378 *RT::Action::SendEmail::SendMessage = sub {
381 return (1) unless ($self->ScripObj->ScripActionObj->Name eq "Notify AdminCcs" );
382 is ($MIME->head->mime_type , "text/plain", "The only part is text/plain ");
383 my $subject = $MIME->head->get("subject");
385 #is( $subject , /^=\?KOI8-R\?B\?W2V4YW1wbGUuY39tICM3XSDUxdPUINTF09Q=\?=/ , "The $subject is encoded correctly");
389 my @input_encodings = RT->Config->Get( 'EmailInputEncodings' );
390 shift @input_encodings;
391 RT->Config->Set(EmailInputEncodings => @input_encodings );
392 RT->Config->Set(EmailOutputEncoding => 'utf-8');
396 my $nested_rfc822_email = RT::Test::get_relocatable_file('nested-rfc-822',
397 (File::Spec->updir(), 'data', 'emails'));
398 $content = RT::Test->file_content($nested_rfc822_email);
399 ok ($content, "Loaded nested-rfc-822 to test");
401 $parser->ParseMIMEEntityFromScalar($content);
404 # be as much like the mail gateway as possible.
405 &text_plain_nested_redef_sendmessage;
406 %args = (message => $content, queue => 1, action => 'correspond');
407 RT::Interface::Email::Gateway(\%args);
408 $tickets = RT::Tickets->new(RT->SystemUser);
409 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
410 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
411 $tick= $tickets->First();
412 ok ($tick->Id, "found ticket ".$tick->Id);
413 is ($tick->Subject, "[Jonas Liljegren] Re: [Para] Niv\x{e5}er?");
414 like (first_attach($tick)->ContentType , qr/multipart\/mixed/, "We recorded the content type right");
415 is (count_attachs($tick) , 5 , "Has one attachment, presumably a text-plain and a message RFC 822 and another plain");
416 sub text_plain_nested_redef_sendmessage {
417 no warnings qw/redefine/;
418 *RT::Action::SendEmail::SendMessage = sub {
422 return (1) unless ($self->ScripObj->ScripActionObj->Name eq "Notify AdminCcs" );
424 is ($MIME->head->mime_type , "multipart/mixed", "It is a mixed multipart");
426 use MIME::Words qw(:all);
427 my $encoded_subject = $MIME->head->get("subject");
428 my $subject = decode_mimewords($encoded_subject);
430 # MIME::Words isn't actually UTF8-safe. There go 4 hours I'll never get back.
431 utf8::decode($subject);
432 like($subject, qr/Niv\x{e5}er/, "The subject matches the word - $subject");
441 my $uuencoded_email = RT::Test::get_relocatable_file('notes-uuencoded',
442 (File::Spec->updir(), 'data', 'emails'));
443 $content = RT::Test->file_content($uuencoded_email);
445 $parser->ParseMIMEEntityFromScalar($content);
448 # be as much like the mail gateway as possible.
450 no warnings qw/redefine/;
451 local *RT::Action::SendEmail::SendMessage = sub { return 1};
452 %args = (message => $content, queue => 1, action => 'correspond');
453 RT::Interface::Email::Gateway(\%args);
454 $tickets = RT::Tickets->new(RT->SystemUser);
455 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
456 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
457 $tick= $tickets->First();
458 ok ($tick->Id, "found ticket ".$tick->Id);
460 like (first_txn($tick)->Content , qr/from Lotus Notes/, "We recorded the content right");
461 is (count_attachs($tick) , 3 , "Has three attachments");
466 my $crashes_file_based_parser_email = RT::Test::get_relocatable_file(
467 'crashes-file-based-parser', (File::Spec->updir(), 'data', 'emails'));
468 $content = RT::Test->file_content($crashes_file_based_parser_email);
470 $parser->ParseMIMEEntityFromScalar($content);
473 # be as much like the mail gateway as possible.
475 no warnings qw/redefine/;
476 local *RT::Action::SendEmail::SendMessage = sub { return 1};
477 %args = (message => $content, queue => 1, action => 'correspond');
478 RT::Interface::Email::Gateway(\%args);
479 $tickets = RT::Tickets->new(RT->SystemUser);
480 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
481 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
482 $tick= $tickets->First();
483 ok ($tick->Id, "found ticket ".$tick->Id);
485 like (first_txn($tick)->Content , qr/FYI/, "We recorded the content right");
486 is (count_attachs($tick) , 5 , "Has three attachments");
493 my $rt_send_cc_email = RT::Test::get_relocatable_file('rt-send-cc',
494 (File::Spec->updir(), 'data', 'emails'));
495 $content = RT::Test->file_content($rt_send_cc_email);
497 $parser->ParseMIMEEntityFromScalar($content);
501 %args = (message => $content, queue => 1, action => 'correspond');
502 RT::Interface::Email::Gateway(\%args);
503 $tickets = RT::Tickets->new(RT->SystemUser);
504 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
505 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
506 $tick= $tickets->First();
507 ok ($tick->Id, "found ticket ".$tick->Id);
509 my $cc = first_attach($tick)->GetHeader('RT-Send-Cc');
510 like ($cc , qr/test1/, "Found test 1");
511 like ($cc , qr/test2/, "Found test 2");
512 like ($cc , qr/test3/, "Found test 3");
513 like ($cc , qr/test4/, "Found test 4");
514 like ($cc , qr/test5/, "Found test 5");
517 diag q{regression test for #5248 from rt3.fsck.com};
519 my $subject_folding_email = RT::Test::get_relocatable_file(
520 'subject-with-folding-ws', (File::Spec->updir(), 'data', 'emails'));
521 my $content = RT::Test->file_content($subject_folding_email);
522 my ($status, $msg, $ticket) = RT::Interface::Email::Gateway(
523 { message => $content, queue => 1, action => 'correspond' }
525 ok ($status, 'created ticket') or diag "error: $msg";
526 ok ($ticket->id, "found ticket ". $ticket->id);
527 is ($ticket->Subject, 'test', 'correct subject');
530 diag q{regression test for #5248 from rt3.fsck.com};
532 my $long_subject_email = RT::Test::get_relocatable_file('very-long-subject',
533 (File::Spec->updir(), 'data', 'emails'));
534 my $content = RT::Test->file_content($long_subject_email);
535 my ($status, $msg, $ticket) = RT::Interface::Email::Gateway(
536 { message => $content, queue => 1, action => 'correspond' }
538 ok ($status, 'created ticket') or diag "error: $msg";
539 ok ($ticket->id, "found ticket ". $ticket->id);
540 is ($ticket->Subject, '0123456789'x20, 'correct subject');
545 # Don't taint the environment
546 $everyone->PrincipalObj->RevokeRight(Right =>'SuperUser');