diff options
Diffstat (limited to 't/app')
29 files changed, 575 insertions, 194 deletions
diff --git a/t/app/01app.t b/t/app/01app.t index 02ffcd217..eb98b6319 100644 --- a/t/app/01app.t +++ b/t/app/01app.t @@ -1,10 +1,27 @@ #!/usr/bin/env perl + use strict; use warnings; -use Test::More; +package FixMyStreet::Cobrand::Tester; +use parent 'FixMyStreet::Cobrand::FiksGataMi'; +sub front_stats_data { { new => 0, fixed => 0, updates => 12345 } } + +package main; + +use Test::More; use Catalyst::Test 'FixMyStreet::App'; +use charnames ':full'; +use Encode qw(encode); ok( request('/')->is_success, 'Request should succeed' ); +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'tester' ], +}, sub { + my $page = get('/'); + my $num = encode('UTF-8', "12\N{NO-BREAK SPACE}345"); + like $page, qr/$num/; +}; + done_testing(); diff --git a/t/app/controller/about.t b/t/app/controller/about.t index fb0744d6a..6a082a2ff 100644 --- a/t/app/controller/about.t +++ b/t/app/controller/about.t @@ -7,10 +7,19 @@ use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App'; ok( my $mech = Test::WWW::Mechanize::Catalyst->new, 'Created mech object' ); # check that we can get the page -$mech->get_ok('/about'); -$mech->content_like(qr{About us ::\s+FixMyStreet}); +$mech->get_ok('/faq'); +$mech->content_like(qr{Frequently Asked Questions ::\s+FixMyStreet}); $mech->content_contains('html class="no-js" lang="en-gb"'); +$mech->get_ok('/privacy'); +is $mech->res->code, 200, "got 200 for final destination"; +is $mech->res->previous->code, 302, "got 302 for redirect"; +is $mech->uri->path, '/about/privacy'; + +$mech->get('/about/page-that-does-not-exist'); +ok !$mech->res->is_success(), "want a bad response"; +is $mech->res->code, 404, "got 404"; + FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'emptyhomes' ], }, sub { diff --git a/t/app/controller/admin.t b/t/app/controller/admin.t index e2dd5df19..92cbbc00f 100644 --- a/t/app/controller/admin.t +++ b/t/app/controller/admin.t @@ -6,18 +6,11 @@ use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; -my $secret = FixMyStreet::App->model('DB::Secret')->search(); - -# don't explode if there's nothing in the secret table -if ( $secret == 0 ) { - diag "You need to put an entry in the secret table for the admin tests to run"; - plan skip_all => 'No entry in secret table'; -} - my $user = FixMyStreet::App->model('DB::User') - ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); + ->find_or_create( { email => 'test@example.com' } ); ok $user, "created test user"; +$user->update({ name => 'Test User' }); my $user2 = FixMyStreet::App->model('DB::User') @@ -70,8 +63,8 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( { - alert_type => 'new_updates', - parameter => $report->id, + alert_type => 'area_problems', + parameter => 2482, confirmed => 1, user => $user, }, @@ -1240,9 +1233,7 @@ subtest "Check admin_base_url" => sub { my $rs = FixMyStreet::App->model('DB::Problem'); my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($report->cobrand)->new(); - is (FixMyStreet::App->model('DB::Problem')->get_admin_url( - $cobrand, - $report), + is ($report->admin_url($cobrand), (sprintf 'https://secure.mysociety.org/admin/bci/report_edit/%d', $report_id), 'get_admin_url OK'); }; diff --git a/t/app/controller/alert.t b/t/app/controller/alert.t index 5bf2af428..c7dc9ed09 100644 --- a/t/app/controller/alert.t +++ b/t/app/controller/alert.t @@ -6,7 +6,7 @@ use LWP::Protocol::PSGI; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; -use t::Nominatim; +use t::Mock::Nominatim; # check that we can get the page $mech->get_ok('/alert'); @@ -42,7 +42,7 @@ FixMyStreet::override_config { $mech->content_contains('ward:2651:20728:City_of_Edinburgh:City_Centre'); subtest "Test Nominatim lookup" => sub { - LWP::Protocol::PSGI->register(t::Nominatim->run_if_script, host => 'nominatim.openstreetmap.org'); + LWP::Protocol::PSGI->register(t::Mock::Nominatim->run_if_script, host => 'nominatim.openstreetmap.org'); $mech->get_ok('/alert/list?pc=High Street'); $mech->content_contains('We found more than one match for that location'); }; diff --git a/t/app/controller/around.t b/t/app/controller/around.t index a70116525..89ca5246e 100644 --- a/t/app/controller/around.t +++ b/t/app/controller/around.t @@ -3,7 +3,7 @@ use warnings; use Test::More; use LWP::Protocol::PSGI; -use t::MapIt; +use t::Mock::MapIt; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -84,7 +84,7 @@ foreach my $test ( ) { subtest "check lat/lng for '$test->{pc}'" => sub { - LWP::Protocol::PSGI->register(t::MapIt->run_if_script, host => 'mapit.uk'); + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk'); $mech->get_ok('/'); FixMyStreet::override_config { diff --git a/t/app/controller/auth_social.t b/t/app/controller/auth_social.t new file mode 100644 index 000000000..84fdd4dfe --- /dev/null +++ b/t/app/controller/auth_social.t @@ -0,0 +1,143 @@ +use strict; +use warnings; +use Test::More; +use LWP::Protocol::PSGI; +use LWP::Simple; +use JSON::MaybeXS; + +use t::Mock::Facebook; +use t::Mock::MapIt; + +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + +my ($report) = $mech->create_problems_for_body(1, '2345', 'Test'); + +LWP::Protocol::PSGI->register(t::Mock::MapIt->to_psgi_app, host => 'mapit.uk'); + +FixMyStreet::override_config { + FACEBOOK_APP_ID => 'facebook-app-id', + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.uk/', +}, sub { + +my $fb_email = 'facebook@example.org'; +my $fb_uid = 123456789; + +for my $fb_state ( 'refused', 'no email', 'existing UID', 'okay' ) { + for my $page ( 'my', 'report', 'update' ) { + subtest "test FB '$fb_state' login for page '$page'" => sub { + $mech->log_out_ok; + if ($fb_state eq 'existing UID') { + my $user = $mech->create_user_ok($fb_email); + $user->update({ facebook_id => $fb_uid }); + } else { + $mech->delete_user($fb_email); + } + + # Set up a mock to catch (most, see below) requests to Facebook + my $fb = t::Mock::Facebook->new; + $fb->returns_email(0) if $fb_state eq 'no email' || $fb_state eq 'existing UID'; + LWP::Protocol::PSGI->register($fb->to_psgi_app, host => 'www.facebook.com'); + LWP::Protocol::PSGI->register($fb->to_psgi_app, host => 'graph.facebook.com'); + + # Due to https://metacpan.org/pod/Test::WWW::Mechanize::Catalyst#External-Redirects-and-allow_external + # the redirect to Facebook's OAuth page can mess up the session + # cookie. So let's pretend we always on www.facebook.com, which + # sorts that out. + $mech->host('www.facebook.com'); + + # Fetch the page with the form via which we wish to log in + my $fields; + if ($page eq 'my') { + $mech->get_ok('/my'); + } elsif ($page eq 'report') { + $mech->get_ok('/'); + $mech->submit_form_ok( { with_fields => { pc => 'SW1A1AA' } }, "submit location" ); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $fields = { + title => 'Test title', + detail => 'Test detail', + }; + } else { + $mech->get_ok('/report/' . $report->id); + $fields = { + update => 'Test update', + }; + } + $mech->submit_form(with_fields => $fields, button => 'facebook_sign_in'); + + # As well as the cookie issue above, caused by this external + # redirect rewriting the host, the redirect gets handled directly + # by Catalyst, not our mocked handler, so will be a 404. Check + # the redirect happened instead. + is $mech->res->previous->code, 302, 'FB button redirected'; + like $mech->res->previous->header('Location'), qr{facebook\.com.*dialog/oauth.*facebook-app-id}, 'FB redirect to oauth URL'; + + # Okay, now call the callback Facebook would send us to + if ($fb_state eq 'refused') { + $mech->get_ok('/auth/Facebook?error_code=ERROR'); + } else { + $mech->get_ok('/auth/Facebook?code=response-code'); + } + + # Check we're showing the right form, regardless of what came back + if ($page eq 'report') { + $mech->content_contains('/report/new'); + } elsif ($page eq 'update') { + $mech->content_contains('/report/update'); + } + + if ($fb_state eq 'refused') { + $mech->content_contains('Sorry, we could not log you in. Please fill in the form below.'); + $mech->not_logged_in_ok; + } elsif ($fb_state eq 'no email') { + $mech->content_contains('We need your email address, please give it below.'); + # We don't have an email, so check that we can still submit it, + # and the ID carries through the confirmation + if ($page eq 'update') { + $fields->{rznvy} = $fb_email; + } else { + $fields->{email} = $fb_email; + } + $fields->{name} = 'Ffion Tester'; + $mech->submit_form(with_fields => $fields); + $mech->content_contains('Nearly done! Now check your email'); + + my $email = $mech->get_email; + ok $email, "got an email"; + $mech->clear_emails_ok; + my ( $url, $url_token ) = $email->body =~ m{(https?://\S+/[CMP]/)(\S+)}; + ok $url, "extracted confirm url '$url'"; + + my $user = FixMyStreet::App->model( 'DB::User' )->find( { email => $fb_email } ); + if ($page eq 'my') { + is $user, undef, 'No user yet exists'; + } else { + is $user->facebook_id, undef, 'User has no facebook ID'; + } + $mech->get_ok( $url . $url_token ); + $user = FixMyStreet::App->model( 'DB::User' )->find( { email => $fb_email } ); + is $user->facebook_id, $fb_uid, 'User now has correct facebook ID'; + + } elsif ($page ne 'my') { + # /my auth login goes directly there, no message like this + $mech->content_contains('You have successfully signed in; please check and confirm your details are accurate'); + $mech->logged_in_ok; + } else { + is $mech->uri->path, '/my', 'Successfully on /my page'; + } + } + } +} + +}; + +END { + $mech->delete_problems_for_body('2345'); + done_testing(); +} diff --git a/t/app/controller/my.t b/t/app/controller/my.t index b723e537e..d24a66c8e 100644 --- a/t/app/controller/my.t +++ b/t/app/controller/my.t @@ -9,12 +9,17 @@ my $mech = FixMyStreet::TestMech->new; $mech->get_ok('/my'); is $mech->uri->path, '/auth', "got sent to the sign in page"; +$mech->create_problems_for_body(1, 1234, 'Test Title'); + # sign in my $user = $mech->log_in_ok( 'test@example.com' ); $mech->get_ok('/my'); is $mech->uri->path, '/my', "stayed on '/my/' page"; +# Report listed +$mech->content_contains('Test Title'); + # cleanup $mech->delete_user( $user ); - +$mech->delete_problems_for_body(1234); done_testing(); diff --git a/t/app/controller/page_not_found.t b/t/app/controller/page_not_found.t index 05e983109..3c2bc3c3d 100644 --- a/t/app/controller/page_not_found.t +++ b/t/app/controller/page_not_found.t @@ -1,5 +1,3 @@ -#!/usr/bin/perl - use strict; use warnings; diff --git a/t/app/controller/photo.t b/t/app/controller/photo.t index 6e61ebb32..39380e769 100644 --- a/t/app/controller/photo.t +++ b/t/app/controller/photo.t @@ -64,6 +64,9 @@ subtest "Check multiple upload worked" => sub { ); ok $mech->success, 'Made request with multiple photo upload'; $mech->base_is('http://localhost/report/new'); + $mech->content_like( + qr[(<img align="right" src="/photo/1cdd4329ceee2234bd4e89cb33b42061a0724687.temp.jpeg" alt="">\s*){3}], + 'Three uploaded pictures are all shown, safe'); $mech->content_contains( 'name="upload_fileid" value="1cdd4329ceee2234bd4e89cb33b42061a0724687,1cdd4329ceee2234bd4e89cb33b42061a0724687,1cdd4329ceee2234bd4e89cb33b42061a0724687"', 'Returned upload_fileid contains expected hash, 3 times'); diff --git a/t/app/controller/questionnaire.t b/t/app/controller/questionnaire.t index 2a89454d5..7718d5034 100644 --- a/t/app/controller/questionnaire.t +++ b/t/app/controller/questionnaire.t @@ -87,7 +87,7 @@ foreach my $test ( { desc => 'User goes to questionnaire URL with a bad token', token_extra => 'BAD', - content => "we couldn't validate that token", + content => "Sorry, that wasn’t a valid link", }, { desc => 'User goes to questionnaire URL for a now-hidden problem', diff --git a/t/app/controller/report_import.t b/t/app/controller/report_import.t index 4d0f6e5d1..26acf7790 100644 --- a/t/app/controller/report_import.t +++ b/t/app/controller/report_import.t @@ -3,7 +3,7 @@ use warnings; use Test::More; use LWP::Protocol::PSGI; -use t::MapIt; +use t::Mock::MapIt; use FixMyStreet::TestMech; use FixMyStreet::App; use Web::Scraper; @@ -92,7 +92,7 @@ subtest "Test creating bad partial entries" => sub { }; subtest "Submit a correct entry" => sub { - LWP::Protocol::PSGI->register(t::MapIt->run_if_script, host => 'mapit.uk'); + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk'); $mech->get_ok('/import'); @@ -153,10 +153,13 @@ subtest "Submit a correct entry" => sub { name => 'Test User', title => 'Test report', detail => 'This is a test report', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', phone => '', may_show_name => '1', category => '-- Pick a category --', + gender => undef, }, "check imported fields are shown"; @@ -187,10 +190,13 @@ subtest "Submit a correct entry" => sub { name => 'Test User', title => 'Test report', detail => 'This is a test report', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', phone => '', may_show_name => '1', category => '-- Pick a category --', + gender => undef, }, "check imported fields are shown"; @@ -275,10 +281,13 @@ subtest "Submit a correct entry (with location)" => sub { name => 'Test User ll', title => 'Test report ll', detail => 'This is a test report ll', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', phone => '', may_show_name => '1', category => '-- Pick a category --', + gender => undef, }, "check imported fields are shown"; diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t index 3c05adfbd..cf72221b4 100644 --- a/t/app/controller/report_new.t +++ b/t/app/controller/report_new.t @@ -103,7 +103,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -127,7 +129,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -154,7 +158,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -178,7 +184,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => undef, email => '', @@ -202,7 +210,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => undef, email => '', @@ -225,7 +235,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'Bob Jones', may_show_name => '1', email => '', @@ -248,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 => '', @@ -271,7 +285,9 @@ foreach my $test ( fields => { title => 'Test title', detail => 'Test detail', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'DUDE', may_show_name => '1', email => '', @@ -293,7 +309,9 @@ foreach my $test ( fields => { title => 'Test title', detail => 'Test detail', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => 'anonymous', may_show_name => '1', email => '', @@ -315,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', @@ -334,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 => '', @@ -359,7 +381,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => ' Bob Jones ', may_show_name => '1', email => ' BOB @ExAmplE.COM ', @@ -381,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', @@ -392,7 +418,7 @@ foreach my $test ( remember_me => undef, }, changes => { - photo => '', + photo1 => '', }, errors => [ "Please upload a JPEG image only" ], }, @@ -402,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', @@ -413,7 +441,7 @@ 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." ], }, @@ -423,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', @@ -434,7 +464,7 @@ foreach my $test ( remember_me => undef, }, changes => { - photo => '', + photo1 => '', }, errors => [ "Please enter a subject" ], }, @@ -468,6 +498,7 @@ foreach my $test ( my $new_values = { %{ $test->{fields} }, # values added to form %{ $test->{changes} }, # changes we expect + gender => undef, }; is_deeply $mech->visible_form_values, $new_values, "values correctly changed"; @@ -534,7 +565,7 @@ foreach my $test ( with_fields => { title => 'Test Report', detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', may_show_name => '1', email => 'test-1@example.com', @@ -652,7 +683,7 @@ subtest "test password errors for a user who is signing in as they report" => su with_fields => { title => 'Test Report', detail => 'Test report details.', - photo => '', + photo1 => '', email => 'test-2@example.com', password_sign_in => 'secret1', category => 'Street lighting', @@ -704,7 +735,7 @@ subtest "test report creation for a user who is signing in as they report" => su with_fields => { title => 'Test Report', detail => 'Test report details.', - photo => '', + photo1 => '', email => 'test-2@example.com', password_sign_in => 'secret2', category => 'Street lighting', @@ -803,8 +834,11 @@ foreach my $test ( may_show_name => '1', name => 'Test User', phone => '01234 567 890', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', category => '-- Pick a category --', + gender => undef, }, "user's details prefilled" ); @@ -814,7 +848,7 @@ foreach my $test ( with_fields => { title => "Test Report at café", detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', may_show_name => '1', phone => '07903 123 456', @@ -898,7 +932,7 @@ subtest "test report creation for a category that is non public" => sub { with_fields => { title => 'Test Report', detail => 'Test report details.', - photo => '', + photo1 => '', email => 'test-2@example.com', name => 'Joe Bloggs', category => 'Street lighting', @@ -1087,7 +1121,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', @@ -1156,7 +1190,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', @@ -1250,7 +1284,7 @@ subtest "test Hart" => sub { with_fields => { title => 'Test Report', detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', may_show_name => '1', category => $test->{category}, @@ -1484,7 +1518,7 @@ subtest "unresponsive body handling works" => sub { with_fields => { title => "Test Report at café", detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', email => $test_email, may_show_name => '1', @@ -1523,7 +1557,7 @@ subtest "unresponsive body handling works" => sub { with_fields => { title => "Test Report at café", detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', email => $test_email, may_show_name => '1', @@ -1604,7 +1638,7 @@ subtest "extra google analytics code displayed on logged in problem creation" => with_fields => { title => "Test Report at café", detail => 'Test report details.', - photo => '', + photo1 => '', name => 'Joe Bloggs', may_show_name => '1', phone => '07903 123 456', @@ -1646,7 +1680,7 @@ subtest "extra google analytics code displayed on email confirmation problem cre my $submission_fields = { title => "Test Report", detail => 'Test report details.', - photo => '', + photo1 => '', email => 'firstlast@example.com', name => 'Test User', may_show_name => '1', diff --git a/t/app/controller/report_new_mobile.t b/t/app/controller/report_new_mobile.t new file mode 100644 index 000000000..61cb14a1b --- /dev/null +++ b/t/app/controller/report_new_mobile.t @@ -0,0 +1,38 @@ +use Test::More; +use FixMyStreet::TestMech; + +my $mech = FixMyStreet::TestMech->new; + +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + +subtest "Check signed up for alert when logged in" => sub { + FixMyStreet::override_config { + MAPIT_URL => 'http://global.mapit.mysociety.org', + MAPIT_TYPES => [ 'O06' ], + }, sub { + $mech->log_in_ok('user@example.org'); + $mech->post_ok( '/report/new/mobile', { + service => 'iPhone', + title => 'Title', + detail => 'Problem detail', + lat => 47.381817, + lon => 8.529156, + email => 'user@example.org', + pc => '', + name => 'Name', + }); + my $res = $mech->response; + ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json'; + + my $user = FixMyStreet::DB->resultset('User')->search({ email => 'user@example.org' })->first; + my $a = FixMyStreet::DB->resultset('Alert')->search({ user_id => $user->id })->first; + isnt $a, undef, 'User is signed up for alert'; + }; +}; + +END { + $mech->delete_user('user@example.org'); + done_testing(); +} diff --git a/t/app/controller/report_new_open311.t b/t/app/controller/report_new_open311.t index d3ca93f0e..22b37fd55 100644 --- a/t/app/controller/report_new_open311.t +++ b/t/app/controller/report_new_open311.t @@ -45,7 +45,9 @@ foreach my $test ( fields => { title => '', detail => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', name => '', may_show_name => '1', email => '', @@ -129,6 +131,7 @@ foreach my $test ( my $new_values = { %{ $test->{fields} }, # values added to form %{ $test->{changes} }, # changes we expect + gender => undef, }; is_deeply $mech->visible_form_values, $new_values, "values correctly changed"; diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t index 6c6b4ca19..f9f5189e5 100644 --- a/t/app/controller/report_updates.t +++ b/t/app/controller/report_updates.t @@ -199,7 +199,9 @@ for my $test ( rznvy => '', update => '', name => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', fixed => undef, add_alert => 1, may_show_name => undef, @@ -216,7 +218,9 @@ for my $test ( rznvy => 'test', update => '', name => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', fixed => undef, add_alert => 1, may_show_name => undef, @@ -233,7 +237,9 @@ for my $test ( rznvy => 'test @ example. com', update => '', name => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', fixed => undef, add_alert => 1, may_show_name => undef, @@ -252,7 +258,9 @@ for my $test ( rznvy => 'test@EXAMPLE.COM', update => '', name => '', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', fixed => undef, add_alert => 1, may_show_name => undef, @@ -292,7 +300,9 @@ for my $test ( rznvy => '', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, remember_me => undef, @@ -316,7 +326,9 @@ for my $test ( rznvy => '', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, remember_me => undef, @@ -417,7 +429,9 @@ for my $test ( rznvy => '', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, remember_me => undef, @@ -502,7 +516,9 @@ subtest 'check non authority user cannot change set state' => sub { name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'this is a forbidden update', state => 'fixed - council', }, @@ -530,7 +546,9 @@ for my $state ( qw/unconfirmed hidden partial/ ) { name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'this is a forbidden update', state => $state, }, @@ -553,7 +571,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to investigating', state => 'investigating', }, @@ -565,7 +585,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to in progress', state => 'in progress', }, @@ -577,7 +599,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to fixed', state => 'fixed', }, @@ -589,7 +613,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to action scheduled', state => 'action scheduled', }, @@ -601,7 +627,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to unable to fix', state => 'unable to fix', }, @@ -613,7 +641,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to internal referral', state => 'internal referral', }, @@ -626,7 +656,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to not responsible', state => 'not responsible', }, @@ -639,7 +671,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to duplicate', state => 'duplicate', }, @@ -652,7 +686,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to internal referral', state => 'internal referral', }, @@ -665,7 +701,9 @@ for my $test ( name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Set state to fixed', state => 'fixed', }, @@ -783,7 +821,9 @@ subtest "check comment with no status change has not status in meta" => sub { name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Comment that does not change state', }, }, @@ -813,7 +853,9 @@ subtest "check comment with no status change has not status in meta" => sub { name => $user->name, may_show_name => 1, add_alert => undef, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => 'Comment that sets state to investigating', state => 'investigating', }, @@ -1049,6 +1091,9 @@ subtest 'submit an update for a registered user, creating update by email' => su $mech->delete_user( $user ); }; +my $sample_file = file(__FILE__)->parent->file("sample.jpg")->stringify; +ok -e $sample_file, "sample file $sample_file exists"; + for my $test ( { desc => 'submit update for registered user', @@ -1056,7 +1101,9 @@ for my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1066,6 +1113,7 @@ for my $test ( update => 'update from a registered user', add_alert => undef, fixed => undef, + photo1 => [ [ $sample_file, undef, Content_Type => 'image/jpeg' ], 1 ], }, changed => { update => 'Update from a registered user' @@ -1081,7 +1129,9 @@ for my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1107,7 +1157,9 @@ for my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1132,7 +1184,9 @@ for my $test ( name => 'Commenter', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1157,7 +1211,9 @@ for my $test ( name => 'Commenter', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', }, email => 'commenter@example.com', @@ -1210,6 +1266,11 @@ for my $test ( $mech->content_contains("/report/" . $report_id); $mech->get_ok("/report/" . $report_id); + my $update = $report->comments->first; + ok $update, 'found update'; + + $mech->content_contains("/photo/c/" . $update->id . ".0.jpeg") if $test->{fields}->{photo1}; + if ( !defined( $test->{endstate_banner} ) ) { is $mech->extract_problem_banner->{text}, undef, 'endstate banner'; } else { @@ -1223,8 +1284,6 @@ for my $test ( %{ $test->{changed} }, }; - my $update = $report->comments->first; - ok $update, 'found update'; is $update->text, $results->{update}, 'update text'; is $update->user->email, $test->{email}, 'update user'; is $update->state, 'confirmed', 'update confirmed'; @@ -1245,7 +1304,9 @@ foreach my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1271,7 +1332,9 @@ foreach my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1298,7 +1361,9 @@ foreach my $test ( name => 'Test User', may_show_name => 1, add_alert => 1, - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', update => '', fixed => undef, }, @@ -1771,7 +1836,9 @@ for my $test ( my %standard_fields = ( name => $report->user->name, update => 'update text', - photo => '', + photo1 => '', + photo2 => '', + photo3 => '', may_show_name => 1, add_alert => 1, ); diff --git a/t/app/controller/rss.t b/t/app/controller/rss.t index db653c094..3e820cff3 100644 --- a/t/app/controller/rss.t +++ b/t/app/controller/rss.t @@ -181,7 +181,7 @@ my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_c subtest "check RSS feeds on cobrand have correct URLs for non-cobrand reports" => sub { $mech->host('hart.fixmystreet.com'); - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; my $expected2; FixMyStreet::override_config { diff --git a/t/app/helpers/send_email.t b/t/app/helpers/send_email.t index d1609cb2f..f60f7fa5a 100644 --- a/t/app/helpers/send_email.t +++ b/t/app/helpers/send_email.t @@ -1,5 +1,3 @@ -#!/usr/bin/perl - use strict; use warnings; use utf8; @@ -9,6 +7,7 @@ BEGIN { FixMyStreet->test_mode(1); } +use Email::MIME; use Test::More; use Test::LongString; @@ -38,14 +37,17 @@ is scalar(@emails), 1, "caught one email"; # Get the email, check it has a date and then strip it out my $email_as_string = $mech->get_first_email(@emails); +my $email = Email::MIME->new($email_as_string); my $expected_email_content = path(__FILE__)->parent->child('send_email_sample.txt')->slurp; my $name = FixMyStreet->config('CONTACT_NAME'); $name = "\"$name\"" if $name =~ / /; my $sender = $name . ' <' . FixMyStreet->config('DO_NOT_REPLY_EMAIL') . '>'; $expected_email_content =~ s{CONTACT_EMAIL}{$sender}; +my $expected_email = Email::MIME->new($expected_email_content); -is_string $email_as_string, $expected_email_content, "email is as expected"; +is_deeply { $email->header_pairs }, { $expected_email->header_pairs }, 'MIME email headers ok'; +is_string $email->body, $expected_email->body, 'email is as expected'; subtest 'MIME attachments' => sub { my $data = path(__FILE__)->parent->child('grey.gif')->slurp_raw; @@ -82,15 +84,25 @@ subtest 'MIME attachments' => sub { is scalar(@emails), 1, "caught one email"; my $email_as_string = $mech->get_first_email(@emails); - my ($boundary) = $email_as_string =~ /boundary="([A-Za-z0-9.]*)"/ms; - my $changes = $email_as_string =~ s{$boundary}{}g; - is $changes, 5, '5 boundaries'; # header + 4 around the 3x parts (text + 2 images) + my $email = Email::MIME->new($email_as_string); my $expected_email_content = path(__FILE__)->parent->child('send_email_sample_mime.txt')->slurp; $expected_email_content =~ s{CONTACT_EMAIL}{$sender}g; - - is_string $email_as_string, $expected_email_content, 'MIME email text ok' + $expected_email_content =~ s{BOUNDARY}{$boundary}g; + my $expected_email = Email::MIME->new($expected_email_content); + + my @email_parts; + $email->walk_parts(sub { + my ($part) = @_; + push @email_parts, [ { $part->header_pairs }, $part->body ]; + }); + my @expected_email_parts; + $expected_email->walk_parts(sub { + my ($part) = @_; + push @expected_email_parts, [ { $part->header_pairs }, $part->body ]; + }); + is_deeply \@email_parts, \@expected_email_parts, 'MIME email text ok' or do { (my $test_name = $0) =~ s{/}{_}g; my $path = path("test-output-$test_name.tmp"); diff --git a/t/app/helpers/send_email_sample_mime.txt b/t/app/helpers/send_email_sample_mime.txt index 4ce0f9520..c4ca97bcc 100644 --- a/t/app/helpers/send_email_sample_mime.txt +++ b/t/app/helpers/send_email_sample_mime.txt @@ -1,12 +1,12 @@ MIME-Version: 1.0 Subject: test email =?utf-8?Q?=E2=98=BA?= -Content-Type: multipart/mixed; boundary="" +Content-Type: multipart/mixed; boundary="BOUNDARY" To: test@recipient.com Content-Transfer-Encoding: 7bit From: CONTACT_EMAIL --- +--BOUNDARY MIME-Version: 1.0 Subject: test email =?utf-8?Q?=E2=98=BA?= Content-Type: text/plain; charset="utf-8" @@ -36,7 +36,7 @@ FixMyStreet.=20= --- +--BOUNDARY MIME-Version: 1.0 Content-Type: image/gif; name="foo.gif" Content-Disposition: inline; filename="foo.gif" @@ -45,7 +45,7 @@ Content-Transfer-Encoding: quoted-printable GIF89a=01=00=01=00=80=00=00=00=00=00=CC=CC=CC,=00=00=00=00=01=00=01=00=00= =02=01L=00;= --- +--BOUNDARY MIME-Version: 1.0 Content-Type: image/gif; name="bar.gif" Content-Disposition: inline; filename="bar.gif" @@ -54,4 +54,4 @@ Content-Transfer-Encoding: quoted-printable GIF89a=01=00=01=00=80=00=00=00=00=00=CC=CC=CC,=00=00=00=00=01=00=01=00=00= =02=01L=00;= ----- +--BOUNDARY-- diff --git a/t/app/load_general_config.t b/t/app/load_general_config.t index 3855c2565..1051aac3f 100644 --- a/t/app/load_general_config.t +++ b/t/app/load_general_config.t @@ -1,5 +1,3 @@ -#!/usr/bin/perl -w - use strict; use warnings; diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t index 2620dd68c..0130f404e 100644 --- a/t/app/model/alert_type.t +++ b/t/app/model/alert_type.t @@ -9,21 +9,21 @@ my $mech = FixMyStreet::TestMech->new(); # this is the easiest way to make sure we're not going # to get any emails sent by data kicking about in the database -FixMyStreet::App->model('DB::AlertType')->email_alerts(); +FixMyStreet::DB->resultset('AlertType')->email_alerts(); $mech->clear_emails_ok; my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); ok $user, "created test user"; my $user2 = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'commenter@example.com', name => 'Commenter' } ); ok $user2, "created comment user"; my $user3 = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'bystander@example.com', name => 'Bystander' } ); ok $user3, "created bystander"; @@ -36,7 +36,7 @@ my $dt = DateTime->new( second => 23 ); -my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'SW1A 1AA', bodies_str => '2504', @@ -62,7 +62,7 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( my $report_id = $report->id; ok $report, "created test report - $report_id"; -my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( +my $comment = FixMyStreet::DB->resultset('Comment')->find_or_create( { problem_id => $report_id, user_id => $user2->id, @@ -74,7 +74,7 @@ my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( anonymous => 'f', } ); -my $comment2 = FixMyStreet::App->model('DB::Comment')->find_or_create( +my $comment2 = FixMyStreet::DB->resultset('Comment')->find_or_create( { problem_id => $report_id, user_id => $user2->id, @@ -90,7 +90,7 @@ my $comment2 = FixMyStreet::App->model('DB::Comment')->find_or_create( $comment->confirmed( \"current_timestamp - '3 days'::interval" ); $comment->update; -my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => $report_id, @@ -101,7 +101,7 @@ my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); -my $alert3 = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $alert3 = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user3, parameter => $report_id, @@ -129,7 +129,7 @@ for my $test ( subtest "correct summary for state of $test->{state}" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => [ $alert->id, $alert3->id ], parameter => $comment->id, @@ -139,7 +139,7 @@ for my $test ( $report->state( $test->{state} ); $report->update; - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); $mech->email_count_is( 2 ); my @emails = $mech->get_email; @@ -167,7 +167,7 @@ my $now = DateTime->now(); $report->confirmed( $now->ymd . ' ' . $now->hms ); $report->update(); -my $council_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $council_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user2, parameter => 2504, @@ -181,7 +181,7 @@ my $council_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( subtest "correct text for title after URL" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $council_alert->id, parameter => $report->id, @@ -190,7 +190,7 @@ subtest "correct text for title after URL" => sub { FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; @@ -304,7 +304,7 @@ foreach my $test ( subtest "correct Nearest Road text with $test->{desc}" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $council_alert->id, parameter => $report->id, @@ -327,7 +327,7 @@ foreach my $test ( FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; @@ -341,7 +341,7 @@ foreach my $test ( }; } -my $ward_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $ward_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => 7117, @@ -352,7 +352,7 @@ my $ward_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); -my $report_to_council = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_to_council = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2434', @@ -376,7 +376,7 @@ my $report_to_council = FixMyStreet::App->model('DB::Problem')->find_or_create( } ); -my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_to_county_council = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2240', @@ -400,7 +400,7 @@ my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_c } ); -my $report_outside_district = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_outside_district = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2221', @@ -427,7 +427,7 @@ my $report_outside_district = FixMyStreet::App->model('DB::Problem')->find_or_cr subtest "check alerts from cobrand send main site url for alerts for different council" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $ward_alert->id, } @@ -436,14 +436,14 @@ subtest "check alerts from cobrand send main site url for alerts for different c FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; my $body = $email->body; - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; - my $expected3 = mySociety::Config::get('BASE_URL') . '/report/' . $report_outside_district->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected3 = FixMyStreet->config('BASE_URL') . '/report/' . $report_outside_district->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('hart')->new(); my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; @@ -453,7 +453,7 @@ subtest "check alerts from cobrand send main site url for alerts for different c }; -my $local_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $local_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => -1.731322, @@ -468,18 +468,18 @@ my $local_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( subtest "check local alerts from cobrand send main site url for alerts for different council" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $local_alert->id, } )->delete; - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); my $email = $mech->get_email; my $body = $email->body; - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('hart')->new(); my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; @@ -494,7 +494,7 @@ subtest "correct i18n-ed summary for state of closed" => sub { $report->update( { state => 'closed' } ); $alert->update( { lang => 'nb', cobrand => 'fiksgatami' } ); - FixMyStreet::App->model('DB::AlertSent')->search( { + FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $alert->id, parameter => $comment->id, } )->delete; @@ -502,7 +502,7 @@ subtest "correct i18n-ed summary for state of closed" => sub { FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fiksgatami' ], }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; $mech->email_count_is( 1 ); diff --git a/t/app/model/comment.t b/t/app/model/comment.t index 3141af828..e83d795fc 100644 --- a/t/app/model/comment.t +++ b/t/app/model/comment.t @@ -1,14 +1,12 @@ -#!/usr/bin/perl - use strict; use warnings; use Test::More tests => 2; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; -my $comment_rs = FixMyStreet::App->model('DB::Comment'); +my $comment_rs = FixMyStreet::DB->resultset('Comment'); my $comment = $comment_rs->new( { diff --git a/t/app/model/extra.t b/t/app/model/extra.t index 21c37336e..52e2d839c 100644 --- a/t/app/model/extra.t +++ b/t/app/model/extra.t @@ -3,13 +3,11 @@ use warnings; use Test::More; use utf8; -use FixMyStreet::App; +use FixMyStreet::DB; use Data::Dumper; use DateTime; -my $c = FixMyStreet::App->new; - -my $db = FixMyStreet::App->model('DB')->schema; +my $db = FixMyStreet::DB->connect; $db->txn_begin; my $body = $db->resultset('Body')->create({ name => 'ExtraTestingBody' }); diff --git a/t/app/model/moderation.t b/t/app/model/moderation.t index cdc9a91b0..8fa333db4 100644 --- a/t/app/model/moderation.t +++ b/t/app/model/moderation.t @@ -4,17 +4,17 @@ use Test::More; use Test::Exception; use utf8; -use FixMyStreet::App; +use FixMyStreet::DB; use Data::Dumper; use DateTime; my $dt = DateTime->now; -my $user = FixMyStreet::App->model('DB::User')->find_or_create({ +my $user = FixMyStreet::DB->resultset('User')->find_or_create({ name => 'Bob', email => 'bob@example.com', }); sub get_report_and_original_data { - my $report = FixMyStreet::App->model('DB::Problem')->create( + my $report = FixMyStreet::DB->resultset('Problem')->create( { postcode => 'BR1 3SB', bodies_str => '', diff --git a/t/app/model/photoset.t b/t/app/model/photoset.t index 9e566f873..cfb5236a8 100644 --- a/t/app/model/photoset.t +++ b/t/app/model/photoset.t @@ -4,27 +4,29 @@ use Test::More; use Test::Exception; use utf8; -use FixMyStreet::App; -use Data::Dumper; +use FixMyStreet::DB; use DateTime; use Path::Tiny 'path'; use File::Temp 'tempdir'; my $dt = DateTime->now; -my $c = FixMyStreet::App->new; my $UPLOAD_DIR = tempdir( CLEANUP => 1 ); -local $c->config->{UPLOAD_DIR} = $UPLOAD_DIR; -my $user = $c->model('DB::User')->find_or_create({ +my $db = FixMyStreet::DB->storage->schema; + +my $user = $db->resultset('User')->find_or_create({ name => 'Bob', email => 'bob@example.com', }); -my $image_path = path('t/app/controller/sample.jpg'); +FixMyStreet::override_config { + UPLOAD_DIR => $UPLOAD_DIR, +}, sub { -my $db = FixMyStreet::App->model('DB')->schema; $db->txn_begin; +my $image_path = path('t/app/controller/sample.jpg'); + sub make_report { my $photo_data = shift; return $db->resultset('Problem')->create({ @@ -54,23 +56,26 @@ sub make_report { subtest 'Photoset with photo inline in DB' => sub { my $report = make_report( $image_path->slurp ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 1, 'Found just 1 image'; + is $photoset->data, '1cdd4329ceee2234bd4e89cb33b42061a0724687'; }; $image_path->copy( path( $UPLOAD_DIR, '0123456789012345678901234567890123456789.jpeg' ) ); subtest 'Photoset with 1 referenced photo' => sub { my $report = make_report( '0123456789012345678901234567890123456789' ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 1, 'Found just 1 image'; }; -subtest 'Photoset with 1 referenced photo' => sub { +subtest 'Photoset with 3 referenced photo' => sub { my $report = make_report( '0123456789012345678901234567890123456789,0123456789012345678901234567890123456789,0123456789012345678901234567890123456789' ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 3, 'Found 3 images'; }; $db->txn_rollback; +}; + done_testing(); diff --git a/t/app/model/problem.t b/t/app/model/problem.t index 82569d72a..4e4a098d7 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -1,5 +1,3 @@ -#!/usr/bin/perl - use strict; use warnings; @@ -7,13 +5,13 @@ use Test::More; use FixMyStreet::TestMech; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; use mySociety::Locale; use Sub::Override; mySociety::Locale::gettext_domain('FixMyStreet'); -my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem_rs = FixMyStreet::DB->resultset('Problem'); my $problem = $problem_rs->new( { @@ -147,7 +145,7 @@ for my $test ( }; } -my $user = FixMyStreet::App->model('DB::User')->find_or_create( +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'system_user@example.com' } @@ -161,7 +159,7 @@ $problem->insert; my $tz_local = DateTime::TimeZone->new( name => 'local' ); -my $body = FixMyStreet::App->model('DB::Body')->new({ +my $body = FixMyStreet::DB->resultset('Body')->new({ name => 'Edinburgh City Council' }); @@ -521,7 +519,7 @@ foreach my $test ( { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -541,7 +539,7 @@ foreach my $test ( { } ); FixMyStreet::override_config $override, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); }; $mech->email_count_is( $test->{ email_count } ); @@ -596,7 +594,7 @@ subtest 'check can set mutiple emails as a single contact' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -615,7 +613,7 @@ subtest 'check can set mutiple emails as a single contact' => sub { } ); FixMyStreet::override_config $override, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); }; $mech->email_count_is(1); @@ -630,7 +628,7 @@ subtest 'check can turn on report sent email alerts' => sub { ); $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -648,7 +646,7 @@ subtest 'check can turn on report sent email alerts' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 2 ); my @emails = $mech->get_email; @@ -675,7 +673,7 @@ subtest 'check can turn on report sent email alerts' => sub { subtest 'check iOS app store test reports not sent' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -692,7 +690,7 @@ subtest 'check iOS app store test reports not sent' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 0 ); @@ -704,7 +702,7 @@ subtest 'check iOS app store test reports not sent' => sub { subtest 'check reports from abuser not sent' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -721,7 +719,7 @@ subtest 'check reports from abuser not sent' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 1 ); @@ -734,10 +732,10 @@ subtest 'check reports from abuser not sent' => sub { whensent => undef, } ); - my $abuse = FixMyStreet::App->model('DB::Abuse')->create( { email => $problem->user->email } ); + my $abuse = FixMyStreet::DB->resultset('Abuse')->create( { email => $problem->user->email } ); $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 0 ); diff --git a/t/app/model/questionnaire.t b/t/app/model/questionnaire.t index 240d6d050..945a64633 100644 --- a/t/app/model/questionnaire.t +++ b/t/app/model/questionnaire.t @@ -1,5 +1,3 @@ -#!/usr/bin/perl - use strict; use warnings; @@ -8,9 +6,9 @@ use Test::More; use FixMyStreet; use FixMyStreet::TestMech; -my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => 'test@example.com' } ); +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'test@example.com' } ); -my $problem = FixMyStreet::App->model('DB::Problem')->create( +my $problem = FixMyStreet::DB->resultset('Problem')->create( { postcode => 'EH99 1SP', latitude => 1, @@ -106,7 +104,7 @@ for my $test ( $mech->email_count_is(0); - FixMyStreet::App->model('DB::Questionnaire') + FixMyStreet::DB->resultset('Questionnaire') ->send_questionnaires( { site => 'fixmystreet' } ); $mech->email_count_is( $test->{send_email} ); diff --git a/t/app/model/token.t b/t/app/model/token.t index d72574bb1..e31901187 100644 --- a/t/app/model/token.t +++ b/t/app/model/token.t @@ -1,12 +1,10 @@ -#!/usr/bin/perl - use strict; use warnings; use Test::More; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; my %tests = ( nested_hash => { foo => 'bar', and => [ 'baz', 'bundy' ] }, @@ -14,7 +12,7 @@ my %tests = ( scalar => 123, ); -my $token_rs = FixMyStreet::App->model('DB::Token'); +my $token_rs = FixMyStreet::DB->resultset('Token'); foreach my $test_data_name ( sort keys %tests ) { my $test_data = $tests{$test_data_name}; diff --git a/t/app/model/user.t b/t/app/model/user.t new file mode 100644 index 000000000..24e5d2d98 --- /dev/null +++ b/t/app/model/user.t @@ -0,0 +1,61 @@ +use strict; +use warnings; + +use Test::More; + +use FixMyStreet::TestMech; +use FixMyStreet::DB; + +my $mech = FixMyStreet::TestMech->new(); +$mech->log_in_ok('test@example.com'); + +my ($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 'f' }); +is $problem->user->latest_anonymity, 0, "User's last report was not anonymous"; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.mysociety.org/', +}, sub { + $mech->get_ok('/around?pc=sw1a1aa'); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->content_like(qr/may_show_name[^>]*checked/); +}; + +($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 't' }); +is $problem->user->latest_anonymity, 1, "User's last report was anonymous"; + +create_update($problem, anonymous => 'f'); +is $problem->user->latest_anonymity, 0, "User's last update was not anonyous"; + +create_update($problem, anonymous => 't'); +is $problem->user->latest_anonymity, 1, "User's last update was anonymous"; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], + MAPIT_URL => 'http://mapit.mysociety.org/', +}, sub { + $mech->get_ok('/around?pc=sw1a1aa'); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->content_like(qr/may_show_name[^>c]*>/); +}; + +END { + $mech->delete_user( $problem->user ) if $problem; + done_testing(); +} + +sub create_update { + my ($problem, %params) = @_; + my $dt = DateTime->now()->add(hours => 1); + return FixMyStreet::App->model('DB::Comment')->find_or_create({ + problem_id => $problem->id, + user_id => $problem->user_id, + name => 'Other User', + mark_fixed => 'false', + text => 'This is some update text', + state => 'confirmed', + anonymous => 'f', + created => $dt->ymd . ' ' . $dt->hms, + %params, + }); +} diff --git a/t/app/sendreport/email.t b/t/app/sendreport/email.t index 65cd7bfa8..471145dcb 100644 --- a/t/app/sendreport/email.t +++ b/t/app/sendreport/email.t @@ -1,12 +1,10 @@ -#!/usr/bin/perl - use strict; use warnings; use Test::More; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; use FixMyStreet::SendReport::Email; use FixMyStreet::TestMech; use mySociety::Locale; @@ -17,7 +15,7 @@ my $e = FixMyStreet::SendReport::Email->new(); # area id 1000 my $params = { id => 1000, name => 'Council of the Thousand' }; -my $body = FixMyStreet::App->model('DB::Body')->find_or_create($params); +my $body = FixMyStreet::DB->resultset('Body')->find_or_create($params); ok $body, "found/created body"; my $contact = $mech->create_contact_ok( @@ -27,7 +25,7 @@ my $contact = $mech->create_contact_ok( note => '', ); -my $row = FixMyStreet::App->model('DB::Problem')->new( { +my $row = FixMyStreet::DB->resultset('Problem')->new( { bodies_str => '1000', category => 'category', cobrand => '', |