diff options
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 47 | ||||
-rw-r--r-- | t/app/controller/reports_new.t | 64 |
2 files changed, 105 insertions, 6 deletions
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 6d0d3aeda..750a78305 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -14,6 +14,7 @@ use Test::More; use Web::Scraper; use Carp; use Email::Send::Test; +use Digest::SHA1 'sha1_hex'; =head1 NAME @@ -52,6 +53,40 @@ sub logged_in_ok { "logged in" ); } +=head2 log_in_ok + + $user = $mech->log_in_ok( $email_address ); + +Log in with the email given. If email does not match an account then create one. + +=cut + +sub log_in_ok { + my $mech = shift; + my $email = shift; + + my $user = + FixMyStreet::App->model('DB::User') + ->find_or_create( { email => $email } ); + ok $user, "found/created user for $email"; + + # store the old password and then change it + my $old_password_sha1 = $user->password; + $user->update( { password => sha1_hex('secret') } ); + + # log in + $mech->get_ok('/auth'); + $mech->submit_form_ok( + { with_fields => { email => $email, password => 'secret' } }, + "login using form" ); + $mech->logged_in_ok; + + # restore the password (if there was one) + $user->update( { password => $old_password_sha1 } ) if $old_password_sha1; + + return $user; +} + =head2 log_out_ok $bool = $mech->log_out_ok( ); @@ -66,6 +101,18 @@ sub log_out_ok { $mech->not_logged_in_ok; } +sub delete_user { + my $mech = shift; + my $user = shift; + + $mech->log_out_ok; + ok( $_->delete, "delete problem " . $_->title ) # + for $user->problems; + ok $user->delete, "delete test user " . $user->email; + + return 1; +} + =head2 clear_emails_ok $bool = $mech->clear_emails_ok(); diff --git a/t/app/controller/reports_new.t b/t/app/controller/reports_new.t index aa4920cc5..7ee125592 100644 --- a/t/app/controller/reports_new.t +++ b/t/app/controller/reports_new.t @@ -376,9 +376,7 @@ subtest "test report creation for a user who does not have an account" => sub { $mech->logged_in_ok; # cleanup - $mech->log_out_ok; - ok $_->delete, "delete problem" for $user->problems; - ok $user->delete, "delete test user"; + $mech->delete_user($user); }; #### test report creation for a user who has account but is not logged in @@ -389,9 +387,63 @@ subtest "test report creation for a user who does not have an account" => sub { # report is confirmed #### test report creation for user with account and logged in -# come to site -# fill in report -# report is confirmed +subtest "test report creation for a user who is logged in" => sub { + + # check that the user does not exist + my $test_email = 'test-2@example.com'; + + $mech->clear_emails_ok; + my $user = $mech->log_in_ok($test_email); + + # submit initial pc form + $mech->get_ok('/reports/new'); + $mech->submit_form_ok( { with_fields => { pc => 'SW1A 1AA', } }, + "submit location" ); + + TODO: { + local $TODO = +"'/reports/<<id>>' not handled by catalyst yet - form creation redirects to there on success if logged in"; + eval { + $mech->submit_form_ok( + { + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo => '', + name => 'Joe Bloggs', + may_show_name => '1', + email => $test_email, + phone => '07903 123 456', + } + }, + "submit good details" + ); + }; + } + + # find the report + my $report = $user->problems->first; + ok $report, "Found the report"; + + # check that we got redirected to /reports/ + is $mech->uri->path, "/reports/" . $report->id, "redirected to report page"; + + # check that no emails have been sent + $mech->email_count_is(0); + + # check report is confirmed and available + is $report->state, 'confirmed', "report is now confirmed"; + TODO: { + local $TODO = "'/reports/<<id>>' not handled by catalyst yet"; + $mech->get_ok( '/reports/' . $report->id ); + } + + # user is still logged in + $mech->logged_in_ok; + + # cleanup + $mech->delete_user($user); +}; #### test uploading an image |