diff options
Diffstat (limited to 't/app/controller/report_new.t')
-rw-r--r-- | t/app/controller/report_new.t | 1143 |
1 files changed, 816 insertions, 327 deletions
diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t index 92943bde9..71090cd26 100644 --- a/t/app/controller/report_new.t +++ b/t/app/controller/report_new.t @@ -2,12 +2,16 @@ use strict; use utf8; # sign in error message has – in it use warnings; use Test::More; -use utf8; use FixMyStreet::TestMech; +use FixMyStreet::App; use Web::Scraper; use Path::Class; +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + my $mech = FixMyStreet::TestMech->new; $mech->get_ok('/report/new'); @@ -17,86 +21,146 @@ ok -e $sample_file, "sample file $sample_file exists"; subtest "test that bare requests to /report/new get redirected" => sub { $mech->get_ok('/report/new'); - is $mech->uri->path, '/around', "went to /around"; + is $mech->uri->path, '/', "went to /"; is_deeply { $mech->uri->query_form }, {}, "query empty"; - $mech->get_ok('/report/new?pc=SW1A%201AA'); + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/report/new?pc=SW1A%201AA'); + }; is $mech->uri->path, '/around', "went to /around"; is_deeply { $mech->uri->query_form }, { pc => 'SW1A 1AA' }, "pc correctly transferred"; }; -my %contact_params = ( - confirmed => 1, - deleted => 0, - editor => 'Test', - whenedited => \'current_timestamp', - note => 'Created for test', -); +my %body_ids; +my @bodies; +for my $body ( + { area_id => 2651, name => 'City of Edinburgh Council' }, + { area_id => 2226, name => 'Gloucestershire County Council' }, + { area_id => 2326, name => 'Cheltenham Borough Council' }, + { area_id => 2504, name => 'Westminster City Council' }, + # The next three have fixed IDs because bits of the code rely on + # the body ID === MapIt area ID. + { area_id => 2482, name => 'Bromley Council', id => 2482 }, + { area_id => 2227, name => 'Hampshire County Council', id => 2227 }, + { area_id => 2333, name => 'Hart Council', id => 2333 }, +) { + my $body_obj = $mech->create_body_ok($body->{area_id}, $body->{name}, id => $body->{id}); + push @bodies, $body_obj; + $body_ids{$body->{area_id}} = $body_obj->id; +} + # Let's make some contacts to send things to! -FixMyStreet::App->model('DB::Contact')->search( { - email => { 'like', '%example.com' }, -} )->delete; -my $contact1 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2651, # Edinburgh +my $contact1 = $mech->create_contact_ok( + body_id => $body_ids{2651}, # Edinburgh category => 'Street lighting', email => 'highways@example.com', -} ); -my $contact2 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2226, # Gloucestershire +); +my $contact2 = $mech->create_contact_ok( + body_id => $body_ids{2226}, # Gloucestershire category => 'Potholes', email => 'potholes@example.com', -} ); -my $contact3 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2326, # Cheltenham +); +my $contact3 = $mech->create_contact_ok( + body_id => $body_ids{2326}, # Cheltenham category => 'Trees', email => 'trees@example.com', -} ); -my $contact4 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2482, # Bromley +); +my $contact4 = $mech->create_contact_ok( + body_id => $body_ids{2482}, # Bromley category => 'Trees', email => 'trees@example.com', -} ); -my $contact5 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2651, # Edinburgh +); +my $contact5 = $mech->create_contact_ok( + body_id => $body_ids{2651}, # Edinburgh category => 'Trees', email => 'trees@example.com', -} ); -my $contact6 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2434, # Lichfield +); +my $contact6 = $mech->create_contact_ok( + body_id => $body_ids{2333}, # Hart category => 'Trees', email => 'trees@example.com', -} ); -my $contact7 = FixMyStreet::App->model('DB::Contact')->find_or_create( { - %contact_params, - body_id => 2240, # Lichfield - category => 'Street lighting', +); +my $contact7 = $mech->create_contact_ok( + body_id => $body_ids{2227}, # Hampshire + category => 'Street lighting', email => 'highways@example.com', -} ); -ok $contact1, "created test contact 1"; -ok $contact2, "created test contact 2"; -ok $contact3, "created test contact 3"; -ok $contact4, "created test contact 4"; -ok $contact5, "created test contact 5"; -ok $contact6, "created test contact 6"; -ok $contact7, "created test contact 7"; +); +my $contact8 = $mech->create_contact_ok( + body_id => $body_ids{2504}, + category => 'Street lighting', + email => 'highways@example.com' +); # test that the various bit of form get filled in and errors correctly # generated. foreach my $test ( { msg => 'all fields empty', + pc => 'OX1 3DH', + fields => { + title => '', + detail => '', + photo1 => '', + photo2 => '', + photo3 => '', + name => '', + may_show_name => '1', + email => '', + phone => '', + password_sign_in => '', + password_register => '', + remember_me => undef, + }, + changes => {}, + errors => [ + 'Please enter a subject', + 'Please enter some details', + # No category error, as no categories for Oxon at all, so is skipped + 'Please enter your email', + 'Please enter your name', + ], + }, + { + msg => 'all fields empty, bad category', + pc => 'GL50 2PR', + fields => { + title => '', + detail => '', + photo1 => '', + photo2 => '', + photo3 => '', + name => '', + may_show_name => '1', + email => '', + phone => '', + category => 'Something bad', + password_sign_in => '', + password_register => '', + remember_me => undef, + }, + changes => { + category => '-- Pick a category --', + }, + errors => [ + 'Please enter a subject', + 'Please enter some details', + 'Please choose a category', + 'Please enter your email', + 'Please enter your name', + ], + }, + { + msg => 'all fields empty except category', pc => 'SW1A 1AA', fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -120,7 +184,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => undef, email => '', @@ -144,7 +210,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => undef, email => '', @@ -167,7 +235,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => '', @@ -190,7 +260,9 @@ foreach my $test ( fields => { title => "DOG SHIT\r\nON WALLS", detail => "on this portakabin -\r\n\r\nmore of a portaloo HEH!!", - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => '', @@ -213,7 +285,9 @@ foreach my $test ( fields => { title => 'Test title', detail => 'Test detail', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'DUDE', may_show_name => '1', email => '', @@ -235,7 +309,9 @@ foreach my $test ( fields => { title => 'Test title', detail => 'Test detail', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'anonymous', may_show_name => '1', email => '', @@ -257,7 +333,9 @@ foreach my $test ( fields => { title => 'Test title', detail => 'Test detail', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Joe Smith', may_show_name => '1', email => 'not an email', @@ -276,7 +354,9 @@ foreach my $test ( fields => { title => " Test title ", detail => " first line \n\n second\nline\n\n ", - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -301,7 +381,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => ' Bob Jones ', may_show_name => '1', email => ' BOB @ExAmplE.COM ', @@ -323,7 +405,9 @@ foreach my $test ( fields => { title => 'Title', detail => 'Detail', - photo => [ [ undef, 'bad.txt', Content => 'This is not a JPEG', Content_Type => 'text/plain' ], 1 ], + photo1 => [ [ undef, 'bad.txt', Content => 'This is not a JPEG', Content_Type => 'text/plain' ], 1 ], + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => 'bob@example.com', @@ -334,9 +418,9 @@ foreach my $test ( remember_me => undef, }, changes => { - photo => '', + photo1 => '', }, - errors => [ "Please upload a JPEG image only" ], + errors => [ "Please upload an image only" ], }, { msg => 'bad photo upload gives error', @@ -344,7 +428,9 @@ foreach my $test ( fields => { title => 'Title', detail => 'Detail', - photo => [ [ undef, 'fake.jpeg', Content => 'This is not a JPEG', Content_Type => 'image/jpeg' ], 1 ], + photo1 => [ [ undef, 'fake.jpeg', Content => 'This is not a JPEG', Content_Type => 'image/jpeg' ], 1 ], + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => 'bob@example.com', @@ -355,9 +441,9 @@ foreach my $test ( remember_me => undef, }, changes => { - photo => '', + photo1 => '', }, - errors => [ "That image doesn't appear to have uploaded correctly (Please upload a JPEG image only ), please try again." ], + errors => [ "That image doesn't appear to have uploaded correctly (Please upload an image only ), please try again." ], }, { msg => 'photo with octet-stream gets through okay', @@ -365,7 +451,9 @@ foreach my $test ( fields => { title => '', detail => 'Detail', - photo => [ [ $sample_file, undef, Content_Type => 'application/octet-stream' ], 1 ], + photo1 => [ [ $sample_file, undef, Content_Type => 'application/octet-stream' ], 1 ], + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => 'bob@example.com', @@ -376,7 +464,7 @@ foreach my $test ( remember_me => undef, }, changes => { - photo => '', + photo1 => '', }, errors => [ "Please enter a subject" ], }, @@ -386,20 +474,25 @@ foreach my $test ( $mech->get_ok('/around'); # submit initial pc form - $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } }, - "submit location" ); - is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'"; - - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); - - # submit the main form - $mech->submit_form_ok( { with_fields => $test->{fields} }, - "submit form" ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } }, + "submit location" ); + is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'"; + + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); + + # submit the main form + $mech->submit_form_ok( { with_fields => $test->{fields} }, + "submit form" ); + }; # check that we got the errors expected - is_deeply $mech->page_errors, $test->{errors}, "check errors"; + is_deeply [ sort @{$mech->page_errors} ], [ sort @{$test->{errors}} ], "check errors"; # check that fields have changed as expected my $new_values = { @@ -454,30 +547,35 @@ foreach my $test ( # submit initial pc form $mech->get_ok('/around'); - $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, - "submit location" ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, + "submit location" ); - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); - $mech->submit_form_ok( - { - button => 'submit_register', - with_fields => { - title => 'Test Report', - detail => 'Test report details.', - photo => '', - name => 'Joe Bloggs', - may_show_name => '1', - email => 'test-1@example.com', - phone => '07903 123 456', - category => 'Street lighting', - password_register => $test->{password} ? 'secret' : '', - } - }, - "submit good details" - ); + $mech->submit_form_ok( + { + button => 'submit_register', + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + may_show_name => '1', + email => 'test-1@example.com', + phone => '07903 123 456', + category => 'Street lighting', + password_register => $test->{password} ? 'secret' : '', + } + }, + "submit good details" + ); + }; # check that we got the errors expected is_deeply $mech->page_errors, [], "check there were no errors"; @@ -503,15 +601,14 @@ foreach my $test ( is $mech->get( '/report/' . $report->id )->code, 404, "report not found"; # Check the report has been assigned appropriately - is $report->council, 2651; + is $report->bodies_str, $body_ids{2651}; # receive token my $email = $mech->get_email; ok $email, "got an email"; - like $email->body, qr/confirm the problem/i, "confirm the problem"; + like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem"; - my ($url) = $email->body =~ m{(http://\S+)}; - ok $url, "extracted confirm url '$url'"; + my $url = $mech->get_link_from_email($email); # confirm token $mech->get_ok($url); @@ -555,8 +652,7 @@ subtest "test password errors for a user who is signing in as they report" => su # check that the user does not exist my $test_email = 'test-2@example.com'; - my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => $test_email } ); - ok $user, "test user does exist"; + my $user = $mech->create_user_ok($test_email); # setup the user. ok $user->update( { @@ -567,27 +663,32 @@ subtest "test password errors for a user who is signing in as they report" => su # submit initial pc form $mech->get_ok('/around'); - $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, - "submit location" ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, + "submit location" ); - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); - $mech->submit_form_ok( - { - button => 'submit_sign_in', - with_fields => { - title => 'Test Report', - detail => 'Test report details.', - photo => '', - email => 'test-2@example.com', - password_sign_in => 'secret1', - category => 'Street lighting', - } - }, - "submit with wrong password" - ); + $mech->submit_form_ok( + { + button => 'submit_sign_in', + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo1 => '', + email => 'test-2@example.com', + password_sign_in => 'secret1', + category => 'Street lighting', + } + }, + "submit with wrong password" + ); + }; # check that we got the errors expected is_deeply $mech->page_errors, [ @@ -597,13 +698,13 @@ subtest "test password errors for a user who is signing in as they report" => su subtest "test report creation for a user who is signing in as they report" => sub { $mech->log_out_ok; + $mech->cookie_jar({}); $mech->clear_emails_ok; # check that the user does not exist my $test_email = 'test-2@example.com'; - my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => $test_email } ); - ok $user, "test user does exist"; + my $user = $mech->create_user_ok($test_email); # setup the user. ok $user->update( { @@ -614,52 +715,54 @@ subtest "test report creation for a user who is signing in as they report" => su # submit initial pc form $mech->get_ok('/around'); - $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, - "submit location" ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, + "submit location" ); - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); - $mech->submit_form_ok( - { - button => 'submit_sign_in', - with_fields => { - title => 'Test Report', - detail => 'Test report details.', - photo => '', - email => 'test-2@example.com', - password_sign_in => 'secret2', - category => 'Street lighting', - } - }, - "submit good details" - ); + $mech->submit_form_ok( + { + button => 'submit_sign_in', + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo1 => '', + email => 'test-2@example.com', + password_sign_in => 'secret2', + category => 'Street lighting', + } + }, + "submit good details" + ); - # check that we got the errors expected - is_deeply $mech->page_errors, [ - 'You have successfully signed in; please check and confirm your details are accurate:', - ], "check there were errors"; + # check that we got the message expected + $mech->content_contains( 'You have successfully signed in; please check and confirm your details are accurate:' ); - # Now submit with a name - $mech->submit_form_ok( - { - with_fields => { - name => 'Joe Bloggs', - } - }, - "submit good details" - ); + # Now submit with a name + $mech->submit_form_ok( + { + with_fields => { + name => 'Joe Bloggs', + } + }, + "submit good details" + ); + }; # find the report my $report = $user->problems->first; ok $report, "Found the report"; - # check that we got redirected to /report/ - is $mech->uri->path, "/report/" . $report->id, "redirected to report page"; + $mech->content_contains('Thank you for reporting this issue'); # Check the report has been assigned appropriately - is $report->council, 2651; + is $report->bodies_str, $body_ids{2651}; # check that no emails have been sent $mech->email_count_is(0); @@ -708,52 +811,58 @@ foreach my $test ( # submit initial pc form $mech->get_ok('/around'); - $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } }, - "submit location" ); - - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); - - # check that the fields are correctly prefilled - is_deeply( - $mech->visible_form_values, - { - title => '', - detail => '', - may_show_name => '1', - name => 'Test User', - phone => '01234 567 890', - photo => '', - category => '-- Pick a category --', - }, - "user's details prefilled" - ); - - $mech->submit_form_ok( - { - with_fields => { - title => "Test Report at café", - detail => 'Test report details.', - photo => '', - name => 'Joe Bloggs', + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } }, + "submit location" ); + + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); + + # check that the fields are correctly prefilled + is_deeply( + $mech->visible_form_values, + { + title => '', + detail => '', may_show_name => '1', - phone => '07903 123 456', - category => $test->{category}, - } - }, - "submit good details" - ); + name => 'Test User', + phone => '01234 567 890', + photo1 => '', + photo2 => '', + photo3 => '', + category => '-- Pick a category --', + }, + "user's details prefilled" + ); + + $mech->submit_form_ok( + { + with_fields => { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + may_show_name => '1', + phone => '07903 123 456', + category => $test->{category}, + } + }, + "submit good details" + ); + }; # find the report my $report = $user->problems->first; ok $report, "Found the report"; # Check the report has been assigned appropriately - is $report->council, $test->{council}; + is $report->bodies_str, $body_ids{$test->{council}}; - # check that we got redirected to /report/ - is $mech->uri->path, "/report/" . $report->id, "redirected to report page"; + $mech->content_contains('Thank you for reporting this issue'); # check that no emails have been sent $mech->email_count_is(0); @@ -795,34 +904,38 @@ subtest "test report creation for a category that is non public" => sub { # check that the user does not exist my $test_email = 'test-2@example.com'; - my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => $test_email } ); - ok $user, "test user does exist"; + my $user = $mech->create_user_ok($test_email); $contact1->update( { non_public => 1 } ); # submit initial pc form $mech->get_ok('/around'); - $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, - "submit location" ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, + "submit location" ); - # click through to the report page - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" ); + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); - $mech->submit_form_ok( - { - button => 'submit_register', - with_fields => { - title => 'Test Report', - detail => 'Test report details.', - photo => '', - email => 'test-2@example.com', - name => 'Joe Bloggs', - category => 'Street lighting', - } - }, - "submit good details" - ); + $mech->submit_form_ok( + { + button => 'submit_register', + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo1 => '', + email => 'test-2@example.com', + name => 'Joe Bloggs', + category => 'Street lighting', + } + }, + "submit good details" + ); + }; # find the report my $report = $user->problems->first; @@ -833,10 +946,9 @@ subtest "test report creation for a category that is non public" => sub { my $email = $mech->get_email; ok $email, "got an email"; - like $email->body, qr/confirm the problem/i, "confirm the problem"; + like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem"; - my ($url) = $email->body =~ m{(http://\S+)}; - ok $url, "extracted confirm url '$url'"; + my $url = $mech->get_link_from_email($email); # confirm token $mech->get_ok($url); @@ -858,8 +970,24 @@ subtest "test report creation for a category that is non public" => sub { $contact2->category( "Pothol\xc3\xa9s" ); $contact2->update; -$mech->get_ok( '/report/new/ajax?latitude=' . $saved_lat . '&longitude=' . $saved_lon ); + +my $extra_details; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', +}, sub { + $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=' . $saved_lat . '&longitude=' . $saved_lon ); +}; $mech->content_contains( "Pothol\xc3\xa9s" ); +ok !$extra_details->{titles_list}, 'Non Bromley does not send back list of titles'; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', +}, sub { + $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=51.4021&longitude=0.01578'); +}; +ok $extra_details->{titles_list}, 'Bromley sends back list of titles'; #### test uploading an image @@ -867,18 +995,22 @@ $mech->content_contains( "Pothol\xc3\xa9s" ); #### possibly manual testing # create report without using map -# create report by clicking on may with javascript off +# create report by clicking on map with javascript off # create report with images off subtest "check that a lat/lon off coast leads to /around" => sub { my $off_coast_latitude = 50.78301; my $off_coast_longitude = -0.646929; - $mech->get_ok( # - "/report/new" - . "?latitude=$off_coast_latitude" - . "&longitude=$off_coast_longitude" - ); + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok( # + "/report/new" + . "?latitude=$off_coast_latitude" + . "&longitude=$off_coast_longitude" + ); + }; is $mech->uri->path, '/around', "redirected to '/around'"; @@ -892,15 +1024,15 @@ subtest "check that a lat/lon off coast leads to /around" => sub { for my $test ( { desc => 'user title not set if not bromley problem', - host => 'http://www.fixmystreet.com', - postcode => 'EH99 1SP', + host => 'www.fixmystreet.com', + postcode => 'EH1 1BB', fms_extra_title => '', - extra => undef, + extra => [], user_title => undef, }, { desc => 'title shown for bromley problem on main site', - host => 'http://www.fixmystreet.com', + host => 'www.fixmystreet.com', postcode => 'BR1 3UH', fms_extra_title => 'MR', extra => [ @@ -915,7 +1047,7 @@ for my $test ( { desc => 'title, first and last name shown for bromley problem on cobrand', - host => 'http://bromley.fixmystreet.com', + host => 'bromley.fixmystreet.com', postcode => 'BR1 3UH', first_name => 'Test', last_name => 'User', @@ -942,9 +1074,10 @@ for my $test ( ) { subtest $test->{desc} => sub { - if ( $test->{host} =~ /bromley/ && !FixMyStreet::Cobrand->exists('bromley') ) { - plan skip_all => 'Skipping Bromley tests without Bromley cobrand'; - } + my $override = { + ALLOWED_COBRANDS => [ $test->{host} =~ /bromley/ ? 'bromley' : 'fixmystreet' ], + MAPIT_URL => 'http://mapit.uk/', + }; $mech->host( $test->{host} ); @@ -952,12 +1085,14 @@ for my $test ( $mech->clear_emails_ok; $mech->get_ok('/'); - $mech->submit_form_ok( { with_fields => { pc => $test->{postcode}, } }, - "submit location" ); - $mech->follow_link_ok( - { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" - ); + FixMyStreet::override_config $override, sub { + $mech->submit_form_ok( { with_fields => { pc => $test->{postcode}, } }, + "submit location" ); + $mech->follow_link_ok( + { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" + ); + }; my $fields = $mech->visible_form_values('mapSkippedForm'); if ( $test->{fms_extra_title} ) { @@ -980,7 +1115,7 @@ for my $test ( my $submission_fields = { title => "Test Report", detail => 'Test report details.', - photo => '', + photo1 => '', email => 'firstlast@example.com', may_show_name => '1', phone => '07903 123 456', @@ -999,15 +1134,16 @@ for my $test ( $submission_fields->{name} = 'Test User'; } - $mech->submit_form_ok( { with_fields => $submission_fields }, - "submit good details" ); + FixMyStreet::override_config $override, sub { + $mech->submit_form_ok( { with_fields => $submission_fields }, + "submit good details" ); + }; my $email = $mech->get_email; ok $email, "got an email"; - like $email->body, qr/confirm the problem/i, "confirm the problem"; + like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem"; - my ($url) = $email->body =~ m{(https?://\S+)}; - ok $url, "extracted confirm url '$url'"; + my $url = $mech->get_link_from_email($email); # confirm token in order to update the user details $mech->get_ok($url); @@ -1018,7 +1154,7 @@ for my $test ( my $report = $user->problems->first; ok $report, "Found the report"; - my $extras = $report->extra; + my $extras = $report->get_extra_fields; is $user->title, $test->{'user_title'}, 'user title correct'; is_deeply $extras, $test->{extra}, 'extra contains correct values'; @@ -1030,7 +1166,7 @@ for my $test ( subtest 'user title not reset if no user title in submission' => sub { $mech->log_out_ok; - $mech->host( 'http://fixmystreet.com' ); + $mech->host( 'www.fixmystreet.com' ); my $user = $mech->log_in_ok( 'userwithtitle@example.com' ); @@ -1047,7 +1183,7 @@ subtest 'user title not reset if no user title in submission' => sub { my $submission_fields = { title => "Test Report", detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Has Title', may_show_name => '1', phone => '07903 123 456', @@ -1055,18 +1191,23 @@ subtest 'user title not reset if no user title in submission' => sub { }; $mech->get_ok('/'); - $mech->submit_form_ok( { with_fields => { pc => 'EH99 1SP', } }, - "submit location" ); - $mech->follow_link_ok( - { text_regex => qr/skip this step/i, }, - "follow 'skip this step' link" - ); - - my $fields = $mech->visible_form_values('mapSkippedForm'); - ok !exists( $fields->{fms_extra_title} ), 'user title field not displayed'; + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, + "submit location" ); + $mech->follow_link_ok( + { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" + ); + + my $fields = $mech->visible_form_values('mapSkippedForm'); + ok !exists( $fields->{fms_extra_title} ), 'user title field not displayed'; - $mech->submit_form_ok( { with_fields => $submission_fields }, - "submit good details" ); + $mech->submit_form_ok( { with_fields => $submission_fields }, + "submit good details" ); + }; $user->discard_changes; my $report = $user->problems->first; @@ -1075,79 +1216,427 @@ subtest 'user title not reset if no user title in submission' => sub { is $user->title, 'MR', 'User title unchanged'; }; -SKIP: { - skip( "Need 'lichfielddc' in ALLOWED_COBRANDS config", 100 ) - unless FixMyStreet::Cobrand->exists('lichfielddc'); +subtest "test Hart" => sub { + for my $test ( + { + desc => 'confirm link for cobrand council in two tier cobrand links to cobrand site', + category => 'Trees', + council => 2333, + national => 0, + button => 'submit_register', + }, + { + desc => 'confirm link for non cobrand council in two tier cobrand links to national site', + category => 'Street Lighting', + council => 2227, + national => 1, + button => 'submit_register', + }, + { + desc => 'confirmation page for cobrand council in two tier cobrand links to cobrand site', + category => 'Trees', + council => 2333, + national => 0, + confirm => 1, + }, + { + desc => 'confirmation page for non cobrand council in two tier cobrand links to national site', + category => 'Street Lighting', + council => 2227, + national => 1, + confirm => 1, + }, + ) { + subtest $test->{ desc } => sub { + my $test_email = 'test-22@example.com'; + $mech->host( 'hart.fixmystreet.com' ); + $mech->clear_emails_ok; + $mech->log_out_ok; + + my $user = $mech->log_in_ok($test_email) if $test->{confirm}; + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ], + BASE_URL => 'http://www.fixmystreet.com', + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/around'); + $mech->content_contains( "Hart Council" ); + $mech->submit_form_ok( { with_fields => { pc => 'GU51 4AE' } }, "submit location" ); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + my %optional_fields = $test->{confirm} ? () : + ( email => $test_email, phone => '07903 123 456' ); + + # we do this as otherwise test::www::mechanize::catalyst + # goes to the value set in ->host above irregardless and + # that is a 404. It works but it is not pleasant. + $mech->clear_host if $test->{confirm} && $test->{national}; + $mech->submit_form_ok( + { + button => $test->{button}, + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + may_show_name => '1', + category => $test->{category}, + %optional_fields + } + }, + "submit good details" + ); + }; + is_deeply $mech->page_errors, [], "check there were no errors"; + + # check that the user has been created/ not changed + $user = + FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); + ok $user, "user found"; + + # find the report + my $report = $user->problems->first; + ok $report, "Found the report"; + + # Check the report has been assigned appropriately + is $report->bodies_str, $body_ids{$test->{council}}; + + if ( $test->{confirm} ) { + is $mech->uri->path, "/report/new"; + my $base = 'www.fixmystreet.com'; + $base = "hart.fixmystreet.com" unless $test->{national}; + $mech->content_contains("$base/report/" . $report->id, "links to correct site"); + } else { + # receive token + my $email = $mech->get_email; + ok $email, "got an email"; + my $body = $mech->get_text_body_from_email($email); + like $body, qr/to confirm that you want to send your/i, "confirm the problem"; + + # does it reference the fact that this report hasn't been sent to Hart? + if ( $test->{national} ) { + like $body, qr/Hart Council is not responsible for this type/i, "mentions report hasn't gone to Hart"; + } else { + unlike $body, qr/Hart Council is not responsible for this type/i, "doesn't mention report hasn't gone to Hart"; + } - my $test_email = 'test-22@example.com'; - $mech->host( 'http://lichfielddc.fixmystreet.com/' ); - $mech->clear_emails_ok; - $mech->log_out_ok; + my $url = $mech->get_link_from_email($email); + + # confirm token + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ], + BASE_URL => 'http://www.fixmystreet.com', + }, sub { + $mech->get_ok($url); + }; + + my $base = 'www.fixmystreet.com'; + $base = 'hart.fixmystreet.com' unless $test->{national}; + $mech->content_contains( $base . '/report/' . + $report->id, 'confirm page links to correct site' ); + + if ( $test->{national} ) { + # Shouldn't be found, as it was a county problem + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ], + }, sub { + is $mech->get( '/report/' . $report->id )->code, 404, "report not found"; + }; + + # But should be on the main site + $mech->host( 'www.fixmystreet.com' ); + } + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ], + }, sub { + $mech->get_ok( '/report/' . $report->id ); + }; + } - $mech->get_ok('/around'); - $mech->content_contains( "Lichfield District Council FixMyStreet" ); - $mech->submit_form_ok( { with_fields => { pc => 'WS13 7RD' } }, "submit location" ); - $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); - $mech->submit_form_ok( - { - button => 'submit_register', - with_fields => { - title => 'Test Report', - detail => 'Test report details.', - photo => '', - name => 'Joe Bloggs', - may_show_name => '1', - email => $test_email, - phone => '07903 123 456', - category => 'Street lighting', + $report->discard_changes; + is $report->state, 'confirmed', "Report is now confirmed"; + + is $report->name, 'Joe Bloggs', 'name updated correctly'; + + $mech->delete_user($user); + }; + } +}; + +subtest "categories from deleted bodies shouldn't be visible for new reports" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/report/new/ajax?latitude=51.896268&longitude=-2.093063'); # Cheltenham + ok $mech->content_contains( $contact3->category ); + + # Delete the body which the contact belongs to. + $contact3->body->update( { deleted => 1 } ); + + $mech->get_ok('/report/new/ajax?latitude=51.896268&longitude=-2.093063'); # Cheltenham + ok $mech->content_lacks( $contact3->category ); + + $contact3->body->update( { deleted => 0 } ); + }; +}; + +subtest "unresponsive body handling works" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + # Test body-level send method + my $old_send = $contact1->body->send_method; + $contact1->body->update( { send_method => 'Refused' } ); + $mech->get_ok('/report/new/ajax?latitude=55.952055&longitude=-3.189579'); # Edinburgh + my $body_id = $contact1->body->id; + ok $mech->content_like( qr{Edinburgh.*accept reports.*/unresponsive\?body=$body_id} ); + + my $test_email = 'test-2@example.com'; + $mech->log_out_ok; + $mech->get_ok('/around'); + $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, "submit location" ); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->submit_form_ok( + { + with_fields => { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + email => $test_email, + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + } + }, + "submit good details" + ); + + my $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); + ok $user, "test user does exist"; + + my $report = $user->problems->first; + ok $report, "Found the report"; + is $report->bodies_str, undef, "Report not going anywhere"; + + like $mech->get_text_body_from_email, qr/despite not being sent/i, "correct email sent"; + + $user->problems->delete; + $mech->clear_emails_ok; + + # Make sure the same behaviour occurs for reports from the mobile app + $mech->log_out_ok; + $mech->post_ok( '/report/new/mobile', { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + email => $test_email, + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + service => 'iOS', + lat => 55.952055, + lon => -3.189579, + pc => '', + used_map => '1', + submit_register => '1', + password_register => '', + }); + my $res = $mech->response; + ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json'; + + $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); + ok $user, "test user does exist"; + + $report = $user->problems->first; + ok $report, "Found the report"; + is $report->bodies_str, undef, "Report not going anywhere"; + + like $mech->get_text_body_from_email, qr/despite not being sent/i, "correct email sent"; + + $user->problems->delete; + $mech->clear_emails_ok; + + $contact1->body->update( { send_method => $old_send } ); + + # And test per-category refusing + my $old_email = $contact3->email; + $contact3->update( { email => 'REFUSED' } ); + $mech->get_ok('/report/new/category_extras?category=Trees&latitude=51.896268&longitude=-2.093063'); + ok $mech->content_like( qr/Cheltenham.*Trees.*unresponsive.*category=Trees/ ); + + $mech->get_ok('/around'); + $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } }, "submit location" ); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->submit_form_ok( + { + with_fields => { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + email => $test_email, + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + } + }, + "submit good details" + ); + + $report = $user->problems->first; + ok $report, "Found the report"; + is $report->bodies_str, undef, "Report not going anywhere"; + + $contact3->update( { email => $old_email } ); + $mech->delete_user($user); + }; +}; + +subtest "unresponsive body page works" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + }, sub { + my $old_send = $contact1->body->send_method; + my $body_id = $contact1->body->id; + my $url = "/unresponsive?body=$body_id"; + is $mech->get($url)->code, 404, "page not found"; + $contact1->body->update( { send_method => 'Refused' } ); + $mech->get_ok($url); + $mech->content_contains('Edinburgh'); + $contact1->body->update( { send_method => $old_send } ); + + my $old_email = $contact3->email; + $body_id = $contact3->body->id; + $url = "/unresponsive?body=$body_id;category=Trees"; + is $mech->get($url)->code, 404, "page not found"; + $contact3->update( { email => 'REFUSED' } ); + $mech->get_ok($url); + $mech->content_contains('Cheltenham'); + $mech->content_contains('Trees'); + $contact3->update( { email => $old_email } ); + }; +}; + +subtest "extra google analytics code displayed on logged in problem creation" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + BASE_URL => 'https://www.fixmystreet.com', + MAPIT_URL => 'http://mapit.uk/', + }, 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); + + # setup the user. + ok $user->update( + { + name => 'Test User', + phone => '01234 567 890', } - }, - "submit good details" - ); - is_deeply $mech->page_errors, [], "check there were no errors"; + ), + "set users details"; - # check that the user has been created/ not changed - my $user = - FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); - ok $user, "user found"; + # submit initial pc form + $mech->get_ok('/around'); + $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } }, + "submit location" ); - # find the report - my $report = $user->problems->first; - ok $report, "Found the report"; + # click through to the report page + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" ); - # Check the report has been assigned appropriately - is $report->council, 2240; + $mech->submit_form_ok( + { + with_fields => { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + } + }, + "submit good details" + ); - # receive token - my $email = $mech->get_email; - ok $email, "got an email"; - like $email->body, qr/confirm the problem/i, "confirm the problem"; + # find the report + my $report = $user->problems->first; + ok $report, "Found the report"; - my ($url) = $email->body =~ m{(http://\S+)}; - ok $url, "extracted confirm url '$url'"; + $mech->content_contains( "'id': 'report/" . $report->id . "'", 'extra google code present' ); - # confirm token - $mech->get_ok($url); - $report->discard_changes; - is $report->state, 'confirmed', "Report is now confirmed"; + # cleanup + $mech->delete_user($user); + }; +}; - # Shouldn't be found, as it was a county problem - is $mech->get( '/report/' . $report->id )->code, 404, "report not found"; +subtest "extra google analytics code displayed on email confirmation problem creation" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + BASE_URL => 'https://www.fixmystreet.com', + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->log_out_ok; + $mech->clear_emails_ok; - # But should be on the main site - $mech->host( 'www.fixmystreet.com' ); - $mech->get_ok( '/report/' . $report->id ); - is $report->name, 'Joe Bloggs', 'name updated correctly'; + $mech->get_ok('/'); + $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR' } }, + "submit location" ); + $mech->follow_link_ok( + { text_regex => qr/skip this step/i, }, + "follow 'skip this step' link" + ); - $mech->delete_user($user); -} + my $fields = $mech->visible_form_values('mapSkippedForm'); + my $submission_fields = { + title => "Test Report", + detail => 'Test report details.', + photo1 => '', + email => 'firstlast@example.com', + name => 'Test User', + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + password_register => '', + }; -$contact1->delete; -$contact2->delete; -$contact3->delete; -$contact4->delete; -$contact5->delete; -$contact6->delete; -$contact7->delete; + $mech->submit_form_ok( { with_fields => $submission_fields }, + "submit good details" ); + + my $email = $mech->get_email; + ok $email, "got an email"; + like $mech->get_text_body_from_email($email), qr/confirm that you want to/i, "confirm the problem"; + + my $url = $mech->get_link_from_email($email); + + # confirm token in order to update the user details + $mech->get_ok($url); + + # find the report + my $user = + FixMyStreet::App->model('DB::User') + ->find( { email => 'firstlast@example.com' } ); + + my $report = $user->problems->first; + ok $report, "Found the report"; + + $mech->content_contains( "'id': 'report/" . $report->id . "'", 'extra google code present' ); + + $user->problems->delete; + $user->alerts->delete; + $user->delete; + }; +}; done_testing(); + +END { + $mech->delete_body($_) foreach @bodies; +} |