aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/helpers/send_email.t
diff options
context:
space:
mode:
Diffstat (limited to 't/app/helpers/send_email.t')
-rw-r--r--t/app/helpers/send_email.t72
1 files changed, 63 insertions, 9 deletions
diff --git a/t/app/helpers/send_email.t b/t/app/helpers/send_email.t
index 14c7d363b..d1609cb2f 100644
--- a/t/app/helpers/send_email.t
+++ b/t/app/helpers/send_email.t
@@ -9,12 +9,16 @@ BEGIN {
FixMyStreet->test_mode(1);
}
-use Test::More tests => 5;
+use Test::More;
+use Test::LongString;
use Catalyst::Test 'FixMyStreet::App';
use Email::Send::Test;
-use Path::Class;
+use Path::Tiny;
+
+use FixMyStreet::TestMech;
+my $mech = FixMyStreet::TestMech->new;
my $c = ctx_request("/");
@@ -33,16 +37,66 @@ my @emails = Email::Send::Test->emails;
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 $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('DO_NOT_REPLY_EMAIL') . '>';
$expected_email_content =~ s{CONTACT_EMAIL}{$sender};
-is $email_as_string,
-$expected_email_content,
- "email is as expected";
+is_string $email_as_string, $expected_email_content, "email is as expected";
+
+subtest 'MIME attachments' => sub {
+ my $data = path(__FILE__)->parent->child('grey.gif')->slurp_raw;
+
+ Email::Send::Test->clear;
+ my @emails = Email::Send::Test->emails;
+ is scalar(@emails), 0, "reset";
+
+ 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 $changes = $email_as_string =~ s{$boundary}{}g;
+ is $changes, 5, '5 boundaries'; # header + 4 around the 3x parts (text + 2 images)
+
+ my $expected_email_content = path(__FILE__)->parent->child('send_email_sample_mime.txt')->slurp;
+ $expected_email_content =~ s{CONTACT_EMAIL}{$sender}g;
+
+ is_string $email_as_string, $expected_email_content, '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";
+ };
+};
+
+done_testing;