aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/TestMech.pm52
-rw-r--r--t/app/controller/auth.t26
2 files changed, 65 insertions, 13 deletions
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index b9ae90772..6d0d3aeda 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -13,6 +13,7 @@ use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App';
use Test::More;
use Web::Scraper;
use Carp;
+use Email::Send::Test;
=head1 NAME
@@ -65,6 +66,57 @@ sub log_out_ok {
$mech->not_logged_in_ok;
}
+=head2 clear_emails_ok
+
+ $bool = $mech->clear_emails_ok();
+
+Clear the email queue.
+
+=cut
+
+sub clear_emails_ok {
+ my $mech = shift;
+ Email::Send::Test->clear;
+ $mech->builder->ok( 1, 'cleared email queue' );
+ return 1;
+}
+
+=head2 email_count_is
+
+ $bool = $mech->email_count_is( $number );
+
+Check that the number of emails in queue is correct.
+
+=cut
+
+sub email_count_is {
+ my $mech = shift;
+ my $number = shift || 0;
+
+ $mech->builder->is_num( scalar( Email::Send::Test->emails ),
+ $number, "checking for $number email(s) in the queue" );
+}
+
+=head2 get_email
+
+ $email = $mech->get_email;
+
+In scalar context returns first email in queue and fails a test if there are not exactly one emails in the queue.
+
+In list context returns all the emails (or none).
+
+=cut
+
+sub get_email {
+ my $mech = shift;
+ my @emails = Email::Send::Test->emails;
+
+ return @emails if wantarray;
+
+ $mech->email_count_is(1) || return undef;
+ return $emails[0];
+}
+
=head2 form_errors
my $arrayref = $mech->form_errors;
diff --git a/t/app/controller/auth.t b/t/app/controller/auth.t
index 651fd0285..6e1e8d58d 100644
--- a/t/app/controller/auth.t
+++ b/t/app/controller/auth.t
@@ -1,8 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 90;
-use Email::Send::Test;
+use Test::More tests => 97;
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
@@ -48,7 +47,7 @@ for my $test (
}
# create a new account
-Email::Send::Test->clear;
+$mech->clear_emails_ok;
$mech->get_ok('/auth');
$mech->submit_form_ok(
{
@@ -65,16 +64,15 @@ $mech->not_logged_in_ok;
# check that we got one email
{
- my @emails = Email::Send::Test->emails;
- Email::Send::Test->clear;
-
- is scalar(@emails), 1, "got one email";
- is $emails[0]->header('Subject'), "Your FixMyStreet.com account details",
+ $mech->email_count_is(1);
+ my $email = $mech->get_email;
+ $mech->clear_emails_ok;
+ is $email->header('Subject'), "Your FixMyStreet.com account details",
"subject is correct";
- is $emails[0]->header('To'), $test_email, "to is correct";
+ is $email->header('To'), $test_email, "to is correct";
# extract the link
- my ($link) = $emails[0]->body =~ m{(http://\S+)};
+ my ($link) = $email->body =~ m{(http://\S+)};
ok $link, "Found a link in email '$link'";
# check that the user does not exist
@@ -105,7 +103,7 @@ $mech->not_logged_in_ok;
# get a login email and change password
{
- Email::Send::Test->clear;
+ $mech->clear_emails_ok;
$mech->get_ok('/auth');
$mech->submit_form_ok(
{
@@ -122,8 +120,10 @@ $mech->not_logged_in_ok;
# follow link and change password - check not prompted for old password
$mech->not_logged_in_ok;
- my @emails = Email::Send::Test->emails;
- my ($link) = $emails[0]->body =~ m{(http://\S+)};
+ $mech->email_count_is(1);
+ my $email = $mech->get_email;
+ $mech->clear_emails_ok;
+ my ($link) = $email->body =~ m{(http://\S+)};
$mech->get_ok($link);
$mech->follow_link_ok( { url => '/auth/change_password' } );