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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!perl
use strict;
use warnings;
use Test::More tests => 45;
use Email::Sender::Transport::Test;
use Email::Sender::Transport::Failable;
my $sender = Email::Sender::Transport::Test->new;
ok($sender->does('Email::Sender::Transport'));
isa_ok($sender, 'Email::Sender::Transport::Test');
is($sender->delivery_count, 0, "no deliveries so far");
my $message = <<'END_MESSAGE';
From: sender@test.example.com
To: recipient@nowhere.example.net
Subject: this message is going nowhere fast
Dear Recipient,
You will never receive this.
--
sender
END_MESSAGE
ok($sender->is_simple, "we can use standard Test for Simple");
ok(! $sender->allow_partial_success, "std Test doesn't allow partial succ");
{
my $result = $sender->send(
$message,
{ to => [ qw(recipient@nowhere.example.net) ] }
);
ok($result, 'success');
}
is($sender->delivery_count, 1, "we've done one delivery so far");
{
my $result = $sender->send(
$message,
{ to => [ qw(secret-bcc@nowhere.example.net) ] }
);
ok($result, 'success');
}
is($sender->delivery_count, 2, "we've done two deliveries so far");
my @deliveries = $sender->deliveries;
is_deeply(
$deliveries[0]{successes},
[ qw(recipient@nowhere.example.net)],
"first message delivered to 'recipient'",
);
is_deeply(
$deliveries[1]{successes},
[ qw(secret-bcc@nowhere.example.net)],
"second message delivered to 'secret-bcc'",
);
ok($sender->shift_deliveries, "we have one delivery (shifted)...");
ok($sender->shift_deliveries, "...then two (shifted)...");
ok(! $sender->shift_deliveries, "...then no more");
####
{
package Email::Sender::Transport::TestFail;
use Moo;
extends 'Email::Sender::Transport::Test';
sub delivery_failure {
my ($self, $email, $env) = @_;
return Email::Sender::Failure->new('bad sender')
if $env->{from} =~ /^reject@/;
return;
}
sub recipient_failure {
my ($self, $rcpt) = @_;
if ($rcpt =~ /^fault@/) {
return Email::Sender::Failure->new({
message => 'fault',
recipients => [ $rcpt ],
});
}
if ($rcpt =~ /^tempfail@/) {
return Email::Sender::Failure::Temporary->new({
message => 'tempfail',
recipients => [ $rcpt ],
});
}
if ($rcpt =~ /^permfail@/) {
return Email::Sender::Failure::Permanent->new({
message => 'permfail',
recipients => [ $rcpt ],
});
}
return;
}
no Moo;
}
my $fail_test = Email::Sender::Transport::TestFail->new;
sub test_fail {
my ($env, $succ_cb, $fail_cb) = @_;
my $ok = eval { $fail_test->send($message, $env); };
my $error = $@;
$succ_cb ? $succ_cb->($ok) : ok(! $ok, 'we expected to fail');
$fail_cb ? $fail_cb->($error) : ok(! $error, 'we expected to succeed');
}
test_fail(
{
to => 'ok@example.com',
from => 'sender@example.com',
},
sub { ok(ref $_[0] eq 'Email::Sender::Success', 'correct success class'); },
undef,
);
test_fail(
{
to => 'ok@example.com',
from => 'reject@example.com',
},
undef,
sub {
my ($fail) = @_;
isa_ok($fail, 'Email::Sender::Failure');
is($fail->message, 'bad sender', 'got expected failure message');
is_deeply(
[ $fail->recipients ],
[ 'ok@example.com' ],
'correct recipients on failure notice',
);
},
);
test_fail(
{
to => 'tempfail@example.com',
from => 'sender@example.com',
},
undef,
sub { isa_ok($_[0], 'Email::Sender::Failure::Temporary'); },
);
test_fail(
{
to => 'permfail@example.com',
from => 'sender@example.com',
},
undef,
sub { isa_ok($_[0], 'Email::Sender::Failure::Permanent'); },
);
test_fail(
{
to => 'fault@example.com',
from => 'sender@example.com',
},
undef,
sub { is(ref $_[0], 'Email::Sender::Failure', 'exact class on fault'); },
);
test_fail(
{
to => [ 'permfail@example.com', 'ok@example.com' ],
from => 'sender@example.com',
},
undef,
sub {
my $fail = shift;
isa_ok($fail, 'Email::Sender::Failure', 'we got a failure');
isa_ok($fail, 'Email::Sender::Failure::Multi', "it's a multifailure");
my @failures = $fail->failures;
is(@failures, 1, "there is only 1 failure in our multi");
is_deeply(
[ $fail->recipients ],
[ 'permfail@example.com' ],
'failing addrs are correct',
);
ok(
$fail->isa('Email::Sender::Failure::Permanent'),
"even though it is a Multi, we report isa Permanent since it's uniform",
);
},
);
$fail_test = Email::Sender::Transport::TestFail->new({
allow_partial_success => 1,
});
ok(! $fail_test->is_simple, "partial success capable Test ! simple");
ok($fail_test->allow_partial_success, "...because it allows partial success");
test_fail(
{
to => [ 'permfail@example.com', 'ok@example.com' ],
from => 'sender@example.com',
},
sub {
my $succ = shift;
isa_ok($succ, 'Email::Sender::Success', 'we got a success');
isa_ok($succ, 'Email::Sender::Success::Partial', "it's partial");
my $failure = $succ->failure;
isa_ok($failure, 'Email::Sender::Failure::Multi', 'the failure is multi');
my @failures = $failure->failures;
is(@failures, 1, "there is only 1 failure in our partial");
is_deeply(
[ $succ->failure->recipients ],
[ 'permfail@example.com' ],
'failing addrs are correct',
);
ok(
! $succ->isa('Email::Sender::Failure::Permanent'),
"we do not crazily report the success ->isa permfail",
);
},
undef,
);
####
my $failer = Email::Sender::Transport::Failable->new({ transport => $sender });
$failer->transport->clear_deliveries;
my $i = 0;
$failer->fail_if(sub {
return "failing half of all mail to test" if $i++ % 2;
return;
});
{
my $result = eval { $failer->send($message, { to => [ qw(ok@ok.ok) ] }) };
ok($result, 'success');
}
is(
$failer->transport->delivery_count,
1,
"first post-fail_if delivery is OK"
);
{
eval { my $result = $failer->send($message, { to => [ qw(ok@ok.ok) ] }) };
isa_ok($@, 'Email::Sender::Failure', "we died");
}
is(
$failer->transport->delivery_count,
1,
"second post-fail_if delivery fails"
);
|