diff options
Diffstat (limited to 't/app/helpers')
-rw-r--r-- | t/app/helpers/emails/html_test.html | 15 | ||||
-rw-r--r-- | t/app/helpers/emails/html_test.txt | 14 | ||||
-rw-r--r-- | t/app/helpers/emails/test.txt | 14 | ||||
-rw-r--r-- | t/app/helpers/grey.gif | bin | 0 -> 34 bytes | |||
-rw-r--r-- | t/app/helpers/send_email.t | 137 | ||||
-rw-r--r-- | t/app/helpers/send_email_sample.txt | 33 | ||||
-rw-r--r-- | t/app/helpers/send_email_sample_mime.txt | 49 |
7 files changed, 214 insertions, 48 deletions
diff --git a/t/app/helpers/emails/html_test.html b/t/app/helpers/emails/html_test.html new file mode 100644 index 000000000..49f7b38d3 --- /dev/null +++ b/t/app/helpers/emails/html_test.html @@ -0,0 +1,15 @@ +Subject: test email ☺ + +Hello, + +This is a test email where foo: [% foo %]. + +utf8: 我们应该能够无缝处理UTF8编码 + + indented_text + +It additionally has an inline image! +<img src="[% inline_image('t/app/helpers/grey.gif') %]"> + +Yours, +FixMyStreet. diff --git a/t/app/helpers/emails/html_test.txt b/t/app/helpers/emails/html_test.txt new file mode 100644 index 000000000..692d25ccf --- /dev/null +++ b/t/app/helpers/emails/html_test.txt @@ -0,0 +1,14 @@ +Subject: test email ☺ + +Hello, + +This is a test email where foo: [% foo %]. + +utf8: 我们应该能够无缝处理UTF8编码 + + indented_text + +It additionally has an inline image! + +Yours, +FixMyStreet. diff --git a/t/app/helpers/emails/test.txt b/t/app/helpers/emails/test.txt new file mode 100644 index 000000000..1acd4b6ca --- /dev/null +++ b/t/app/helpers/emails/test.txt @@ -0,0 +1,14 @@ +Subject: test email ☺ + +Hello, + +This is a test email where foo: [% foo %]. + +utf8: 我们应该能够无缝处理UTF8编码 + + indented_text + +long line: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Yours, +FixMyStreet. diff --git a/t/app/helpers/grey.gif b/t/app/helpers/grey.gif Binary files differnew file mode 100644 index 000000000..98eee7d12 --- /dev/null +++ b/t/app/helpers/grey.gif diff --git a/t/app/helpers/send_email.t b/t/app/helpers/send_email.t index 8c043f701..66b771292 100644 --- a/t/app/helpers/send_email.t +++ b/t/app/helpers/send_email.t @@ -1,57 +1,134 @@ -#!/usr/bin/perl - use strict; use warnings; use utf8; +package FixMyStreet::Cobrand::Tester; +use parent 'FixMyStreet::Cobrand::Default'; +sub path_to_email_templates { [ FixMyStreet->path_to( 't', 'app', 'helpers', 'emails') ] } + +package main; + BEGIN { use FixMyStreet; FixMyStreet->test_mode(1); } -use Test::More tests => 6; +use Email::MIME; +use Test::More; +use Test::LongString; + +use Catalyst::Test 'FixMyStreet::App'; -use Email::Send::Test; -use Path::Class; +use Path::Tiny; -use_ok 'FixMyStreet::App'; -my $c = FixMyStreet::App->new( - { - request => Catalyst::Request->new( - { - base => URI->new('http://fixmystreet.com/'), - uri => URI->new('http://fixmystreet.com/') - } - ), - } -); -$c->setup_request(); +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +my $c = ctx_request("/"); # set some values in the stash $c->stash->{foo} = 'bar'; # clear the email queue -Email::Send::Test->clear; +$mech->clear_emails_ok; # send the test email -ok $c->send_email( 'test.txt', { to => 'test@recipient.com' } ), - "sent an email"; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'tester' ], +}, sub { + ok $c->send_email( 'test.txt', { to => 'test@recipient.com' } ), + "sent an email"; +}; # check it got templated and sent correctly -my @emails = Email::Send::Test->emails; +my @emails = $mech->get_email; is scalar(@emails), 1, "caught one email"; # Get the email, check it has a date and then strip it out -my $email_as_string = $emails[0]->as_string; -ok $email_as_string =~ s{\s+Date:\s+\S.*?$}{}xms, "Found and stripped out date"; -ok $email_as_string =~ s{\s+Message-ID:\s+\S.*?$}{}xms, "Found and stripped out message ID (contains epoch)"; +my $email_as_string = $mech->get_first_email(@emails); +my $email = Email::MIME->new($email_as_string); -my $expected_email_content = file(__FILE__)->dir->file('send_email_sample.txt')->slurp; +my $expected_email_content = path(__FILE__)->parent->child('send_email_sample.txt')->slurp; my $name = FixMyStreet->config('CONTACT_NAME'); -$name = "\"$name\"" if $name =~ / /; -my $sender = $name . ' <' . FixMyStreet->config('CONTACT_EMAIL') . '>'; +my $sender = '"' . $name . '" <' . FixMyStreet->config('DO_NOT_REPLY_EMAIL') . '>'; $expected_email_content =~ s{CONTACT_EMAIL}{$sender}; +my $expected_email = Email::MIME->new($expected_email_content); + +is_deeply { $email->header_pairs }, { $expected_email->header_pairs }, 'MIME email headers ok'; +is_string $email->body, $expected_email->body, 'email is as expected'; + +subtest 'MIME attachments' => sub { + my $data = path(__FILE__)->parent->child('grey.gif')->slurp_raw; + + $mech->clear_emails_ok; + + ok $c->send_email( 'test.txt', + { to => 'test@recipient.com', + attachments => [ + { + body => $data, + attributes => { + filename => 'foo.gif', + content_type => 'image/gif', + encoding => 'quoted-printable', + name => 'foo.gif', + }, + }, + { + body => $data, + attributes => { + filename => 'bar.gif', + content_type => 'image/gif', + encoding => 'quoted-printable', + name => 'bar.gif', + }, + }, + ] + } ), "sent an email with MIME attachments"; + + @emails = $mech->get_email; + is scalar(@emails), 1, "caught one email"; + + my $email_as_string = $mech->get_first_email(@emails); + my ($boundary) = $email_as_string =~ /boundary="([A-Za-z0-9.]*)"/ms; + my $email = Email::MIME->new($email_as_string); + + my $expected_email_content = path(__FILE__)->parent->child('send_email_sample_mime.txt')->slurp; + $expected_email_content =~ s{CONTACT_EMAIL}{$sender}g; + $expected_email_content =~ s{BOUNDARY}{$boundary}g; + my $expected_email = Email::MIME->new($expected_email_content); + + my @email_parts; + $email->walk_parts(sub { + my ($part) = @_; + push @email_parts, [ { $part->header_pairs }, $part->body ]; + }); + my @expected_email_parts; + $expected_email->walk_parts(sub { + my ($part) = @_; + push @expected_email_parts, [ { $part->header_pairs }, $part->body ]; + }); + is_deeply \@email_parts, \@expected_email_parts, 'MIME email text ok' + or do { + (my $test_name = $0) =~ s{/}{_}g; + my $path = path("test-output-$test_name.tmp"); + $path->spew($email_as_string); + diag "Saved output in $path"; + }; + $mech->clear_emails_ok; +}; + +subtest 'Inline emails!' => sub { + ok $c->send_email( 'html_test.txt', { to => 'test@recipient.com' } ), "sent an email with email attachments"; + + my $email = $mech->get_email; + like $email->debug_structure, qr[ + \+\ multipart/related.*\n + \ {5}\+\ multipart/alternative.*\n + \ {10}\+\ text/plain.*\n + \ {10}\+\ text/html.*\n + \ {5}\+\ image/gif]x; + $mech->clear_emails_ok; +}; -is $email_as_string, -$expected_email_content, - "email is as expected"; +done_testing; diff --git a/t/app/helpers/send_email_sample.txt b/t/app/helpers/send_email_sample.txt index 2fe5272cb..68fe61f0e 100644 --- a/t/app/helpers/send_email_sample.txt +++ b/t/app/helpers/send_email_sample.txt @@ -1,29 +1,26 @@ MIME-Version: 1.0 -Subject: test email =?utf-8?Q?=E2=98=BA?= +Subject: =?UTF-8?B?dGVzdCBlbWFpbCDimLo=?= Content-Type: text/plain; charset="utf-8" To: test@recipient.com Content-Transfer-Encoding: quoted-printable From: CONTACT_EMAIL - Hello, +Hello, - This is a test email where foo: bar. +This is a test email where foo: bar. - utf8: =E6=88=91=E4=BB=AC=E5=BA=94=E8=AF=A5=E8=83=BD=E5=A4=9F=E6=97=A0= -=E7=BC=9D=E5=A4=84=E7=90=86UTF8=E7=BC=96=E7=A0=81 +utf8: =E6=88=91=E4=BB=AC=E5=BA=94=E8=AF=A5=E8=83=BD=E5=A4=9F=E6=97=A0=E7=BC= +=9D=E5=A4=84=E7=90=86UTF8=E7=BC=96=E7=A0=81 - indented_text - - long line: Lorem ipsum dolor sit amet, consectetur adipisicing - elit, sed do eiusmod tempor incididunt ut labore et dolore - magna aliqua. Ut enim ad minim veniam, quis nostrud - exercitation ullamco laboris nisi ut aliquip ex ea commodo - consequat. Duis aute irure dolor in reprehenderit in voluptate - velit esse cillum dolore eu fugiat nulla pariatur. Excepteur - sint occaecat cupidatat non proident, sunt in culpa qui officia - deserunt mollit anim id est laborum. - - Yours,=20=20 - FixMyStreet.=20= + indented_text +long line: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do= + eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad min= +im veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea = +commodo consequat. Duis aute irure dolor in reprehenderit in voluptate veli= +t esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupi= +datat non proident, sunt in culpa qui officia deserunt mollit anim id est l= +aborum. +Yours, +FixMyStreet.= diff --git a/t/app/helpers/send_email_sample_mime.txt b/t/app/helpers/send_email_sample_mime.txt new file mode 100644 index 000000000..7b4ce91f6 --- /dev/null +++ b/t/app/helpers/send_email_sample_mime.txt @@ -0,0 +1,49 @@ +MIME-Version: 1.0
+Subject: =?UTF-8?B?dGVzdCBlbWFpbCDimLo=?=
+Content-Type: multipart/mixed; boundary="BOUNDARY"
+To: test@recipient.com
+Content-Transfer-Encoding: 7bit
+From: CONTACT_EMAIL
+
+
+--BOUNDARY
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: quoted-printable
+
+Hello,
+
+This is a test email where foo: bar.
+
+utf8: =E6=88=91=E4=BB=AC=E5=BA=94=E8=AF=A5=E8=83=BD=E5=A4=9F=E6=97=A0=E7=BC=
+=9D=E5=A4=84=E7=90=86UTF8=E7=BC=96=E7=A0=81
+
+ indented_text
+
+long line: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do=
+ eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad min=
+im veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea =
+commodo consequat. Duis aute irure dolor in reprehenderit in voluptate veli=
+t esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupi=
+datat non proident, sunt in culpa qui officia deserunt mollit anim id est l=
+aborum.
+
+Yours,
+FixMyStreet.=
+
+--BOUNDARY
+Content-Type: image/gif; name="foo.gif"
+Content-Disposition: inline; filename="foo.gif"
+Content-Transfer-Encoding: quoted-printable
+
+GIF89a=01=00=01=00=80=00=00=00=00=00=CC=CC=CC,=00=00=00=00=01=00=01=00=00=
+=02=01L=00;=
+
+--BOUNDARY
+Content-Type: image/gif; name="bar.gif"
+Content-Disposition: inline; filename="bar.gif"
+Content-Transfer-Encoding: quoted-printable
+
+GIF89a=01=00=01=00=80=00=00=00=00=00=CC=CC=CC,=00=00=00=00=01=00=01=00=00=
+=02=01L=00;=
+
+--BOUNDARY--
|