aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/TestMech.pm47
-rw-r--r--t/app/controller/auth.t29
2 files changed, 55 insertions, 21 deletions
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index 94e87f50b..b9ae90772 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -1,11 +1,15 @@
package FixMyStreet::TestMech;
-use base qw(Test::WWW::Mechanize::Catalyst);
+use base qw(Test::WWW::Mechanize::Catalyst Test::Builder::Module);
use strict;
use warnings;
-use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App';
+BEGIN {
+ use FixMyStreet;
+ FixMyStreet->test_mode(1);
+}
+use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App';
use Test::More;
use Web::Scraper;
use Carp;
@@ -20,8 +24,47 @@ This module subclasses L<Test::WWW::Mechanize::Catalyst> and adds some
FixMyStreet specific smarts - such as the ability to scrape the resulting page
for form error messages.
+Note - using this module puts L<FixMyStreet::App> into test mode - so for
+example emails will not get sent.
+
=head1 METHODS
+=head2 check_not_logged_in, check_logged_in
+
+ $bool = $mech->check_not_logged_in();
+ $bool = $mech->check_logged_in();
+
+Check that the current mech is not logged or logged in as a user. Produces test output.
+Returns true test passed, false otherwise.
+
+=cut
+
+sub not_logged_in_ok {
+ my $mech = shift;
+ $mech->builder->ok( $mech->get('/auth/check_auth')->code == 401,
+ "not logged in" );
+}
+
+sub logged_in_ok {
+ my $mech = shift;
+ $mech->builder->ok( $mech->get('/auth/check_auth')->code == 200,
+ "logged in" );
+}
+
+=head2 log_out_ok
+
+ $bool = $mech->log_out_ok( );
+
+Log out the current user
+
+=cut
+
+sub log_out_ok {
+ my $mech = shift;
+ $mech->get_ok('/auth/logout');
+ $mech->not_logged_in_ok;
+}
+
=head2 form_errors
my $arrayref = $mech->form_errors;
diff --git a/t/app/controller/auth.t b/t/app/controller/auth.t
index 43f83db13..651fd0285 100644
--- a/t/app/controller/auth.t
+++ b/t/app/controller/auth.t
@@ -1,18 +1,11 @@
use strict;
use warnings;
-BEGIN {
- use FixMyStreet;
- FixMyStreet->test_mode(1);
-}
-
use Test::More tests => 90;
use Email::Send::Test;
-use FixMyStreet::App;
-
-use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App';
-my $mech = Test::WWW::Mechanize::Catalyst->new;
+use FixMyStreet::TestMech;
+my $mech = FixMyStreet::TestMech->new;
my $test_email = 'test@example.com';
my $test_password = 'foobar';
@@ -26,7 +19,7 @@ END {
$mech->get_ok('/auth');
# check that we can't reach a page that is only available to authenticated users
-is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+$mech->not_logged_in_ok;
# check that submitting form with no / bad email creates an error.
$mech->get_ok('/auth');
@@ -68,7 +61,7 @@ $mech->submit_form_ok(
is $mech->uri->path, '/auth/token', "redirected to welcome page";
# check that we are not logged in yet
-is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+$mech->not_logged_in_ok;
# check that we got one email
{
@@ -93,22 +86,21 @@ is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
# visit the confirm link (with bad token) and check user no confirmed
$mech->get_ok( $link . 'XXX' );
ok !get_user(), "no user exists";
- is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+ $mech->not_logged_in_ok;
# visit the confirm link and check user is confirmed
$mech->get_ok($link);
ok get_user(), "user created";
is $mech->uri->path, '/my', "redirected to the 'my' section of site";
- $mech->get_ok('/auth/check_auth');
+ $mech->logged_in_ok;
# logout and try to use the token again
- $mech->get_ok("/auth/logout");
- is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+ $mech->log_out_ok;
$mech->get_ok($link);
is $mech->uri, $link, "not logged in";
$mech->content_contains( 'Link too old or already used',
'token now invalid' );
- is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+ $mech->not_logged_in_ok;
}
# get a login email and change password
@@ -128,7 +120,7 @@ is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
# rest is as before so no need to test
# follow link and change password - check not prompted for old password
- is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+ $mech->not_logged_in_ok;
my @emails = Email::Send::Test->emails;
my ($link) = $emails[0]->body =~ m{(http://\S+)};
@@ -202,8 +194,7 @@ $mech->submit_form_ok(
is $mech->uri->path, '/my', "redirected to correct page";
# logout
-$mech->get_ok("/auth/logout");
-is $mech->get('/auth/check_auth')->code, 401, "got 401 at check_auth";
+$mech->log_out_ok;
# try to login with bad details
$mech->get_ok('/auth');