aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/helpers/send_email.t
blob: f60f7fa5a585eef1ac449c3c58312e7aa4633fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use strict;
use warnings;
use utf8;

BEGIN {
    use FixMyStreet;
    FixMyStreet->test_mode(1);
}

use Email::MIME;
use Test::More;
use Test::LongString;

use Catalyst::Test 'FixMyStreet::App';

use Email::Send::Test;
use Path::Tiny;

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;

# send the test email
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;
is scalar(@emails), 1, "caught one email";

# Get the email, check it has a date and then strip it out
my $email_as_string = $mech->get_first_email(@emails);
my $email = Email::MIME->new($email_as_string);

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};
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;

    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 $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";
        };
};

done_testing;