diff options
Diffstat (limited to 't/app')
-rw-r--r-- | t/app/model/problem.t | 75 | ||||
-rw-r--r-- | t/app/script/archive_old_enquiries.t | 163 |
2 files changed, 236 insertions, 2 deletions
diff --git a/t/app/model/problem.t b/t/app/model/problem.t index 1130078c0..52213ed51 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -5,6 +5,7 @@ use Test::More; use FixMyStreet::TestMech; use FixMyStreet; +use FixMyStreet::App; use FixMyStreet::DB; use mySociety::Locale; use Sub::Override; @@ -53,7 +54,7 @@ is $problem->whensent, undef, 'inflating null confirmed ok'; is $problem->lastupdate, undef, 'inflating null confirmed ok'; is $problem->created, undef, 'inflating null confirmed ok'; -for my $test ( +for my $test ( { desc => 'more or less empty problem', changed => {}, @@ -242,7 +243,7 @@ for my $test ( }; } -for my $test ( +for my $test ( { state => 'partial', is_visible => 0, @@ -774,6 +775,76 @@ subtest 'check duplicate reports' => sub { is $problem2->duplicates->[0]->title, $problem1->title, 'problem2 includes problem1 in duplicates'; }; +subtest 'generates a tokenised url for a user' => sub { + my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE'); + my $url = $problem->tokenised_url($user); + (my $token = $url) =~ s/\/M\///g; + + like $url, qr/\/M\//, 'problem generates tokenised url'; + + my $token_obj = FixMyStreet::App->model('DB::Token')->find( { + scope => 'email_sign_in', token => $token + } ); + is $token, $token_obj->token, 'token is generated in database with correct scope'; + is $token_obj->data->{r}, $problem->url, 'token has correct redirect data'; +}; + +subtest 'stores params in a token' => sub { + my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE'); + my $url = $problem->tokenised_url($user, { foo => 'bar', baz => 'boo'}); + (my $token = $url) =~ s/\/M\///g; + + my $token_obj = FixMyStreet::App->model('DB::Token')->find( { + scope => 'email_sign_in', token => $token + } ); + + is_deeply $token_obj->data->{p}, { foo => 'bar', baz => 'boo'}, 'token has correct params'; +}; + +subtest 'get report time ago in appropriate format' => sub { + my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE'); + + $problem->update( { + confirmed => DateTime->now->subtract( minutes => 2) + } ); + is $problem->time_ago, '2 minutes', 'problem returns time ago in minutes'; + + $problem->update( { + confirmed => DateTime->now->subtract( hours => 18) + } ); + is $problem->time_ago, '18 hours', 'problem returns time ago in hours'; + + $problem->update( { + confirmed => DateTime->now->subtract( days => 4) + } ); + is $problem->time_ago, '4 days', 'problem returns time ago in days'; + + $problem->update( { + confirmed => DateTime->now->subtract( weeks => 3 ) + } ); + is $problem->time_ago, '3 weeks', 'problem returns time ago in weeks'; + + $problem->update( { + confirmed => DateTime->now->subtract( months => 4 ) + } ); + is $problem->time_ago, '4 months', 'problem returns time ago in months'; + + $problem->update( { + confirmed => DateTime->now->subtract( years => 2 ) + } ); + is $problem->time_ago, '2 years', 'problem returns time ago in years'; + +}; + +subtest 'time ago works with other dates' => sub { + my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE'); + + $problem->update( { + lastupdate => DateTime->now->subtract( days => 4) + } ); + is $problem->time_ago('lastupdate'), '4 days', 'problem returns last updated time ago in days'; +}; + END { $problem->comments->delete if $problem; $problem->delete if $problem; diff --git a/t/app/script/archive_old_enquiries.t b/t/app/script/archive_old_enquiries.t new file mode 100644 index 000000000..e87d6a0f8 --- /dev/null +++ b/t/app/script/archive_old_enquiries.t @@ -0,0 +1,163 @@ +use strict; +use warnings; +use Test::More; +use FixMyStreet::TestMech; +use FixMyStreet::Script::ArchiveOldEnquiries; + +mySociety::Locale::gettext_domain( 'FixMyStreet' ); + +my $mech = FixMyStreet::TestMech->new(); + +$mech->clear_emails_ok; + +my $opts = { + commit => 1, +}; + +my $user = $mech->create_user_ok('test@example.com', name => 'Test User'); +my $oxfordshire = $mech->create_body_ok(2237, 'Oxfordshire County Council', id => 2237); +my $west_oxon = $mech->create_body_ok(2420, 'West Oxfordshire District Council', id => 2420); + +subtest 'sets reports to the correct status' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + my ($report) = $mech->create_problems_for_body(1, $oxfordshire->id, 'Test', { + areas => ',2237,', + user_id => $user->id, + }); + + my ($report1) = $mech->create_problems_for_body(1, $oxfordshire->id . "," .$west_oxon->id, 'Test', { + areas => ',2237,', + lastupdate => '2015-12-01 07:00:00', + user => $user, + }); + + my ($report2) = $mech->create_problems_for_body(1, $oxfordshire->id, 'Test 2', { + areas => ',2237,', + lastupdate => '2015-12-01 08:00:00', + user => $user, + state => 'investigating', + }); + + my ($report3, $report4) = $mech->create_problems_for_body(2, $oxfordshire->id, 'Test', { + areas => ',2237,', + lastupdate => '2014-12-01 07:00:00', + user => $user, + }); + + my ($report5) = $mech->create_problems_for_body(1, $oxfordshire->id . "," .$west_oxon->id, 'Test', { + areas => ',2237,', + lastupdate => '2014-12-01 07:00:00', + user => $user, + state => 'in progress' + }); + + FixMyStreet::Script::ArchiveOldEnquiries::archive($opts); + + $report->discard_changes; + $report1->discard_changes; + $report2->discard_changes; + $report3->discard_changes; + $report4->discard_changes; + $report5->discard_changes; + + is $report1->state, 'closed', 'Report 1 has been set to closed'; + is $report2->state, 'closed', 'Report 2 has been set to closed'; + is $report3->state, 'closed', 'Report 3 has been set to closed'; + is $report4->state, 'closed', 'Report 4 has been set to closed'; + is $report5->state, 'closed', 'Report 5 has been set to closed'; + + is $report->state, 'confirmed', 'Recent report has been left alone'; + }; +}; + +subtest 'sends emails to a user' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $mech->clear_emails_ok; + $mech->email_count_is(0); + + $mech->create_problems_for_body(1, $oxfordshire->id, 'Shiny new report', { + areas => ',2237,', + user => $user, + }); + + $mech->create_problems_for_body(1, $oxfordshire->id, 'Problem the first', { + areas => ',2237,', + lastupdate => '2015-12-01 07:00:00', + user => $user, + }); + + $mech->create_problems_for_body(1, $oxfordshire->id, 'Problem the second', { + areas => ',2237,', + lastupdate => '2015-12-01 07:00:00', + user => $user, + }); + + $mech->create_problems_for_body(1, $oxfordshire->id, 'Problem the third', { + areas => ',2237,', + lastupdate => '2015-12-01 07:00:00', + user => $user, + }); + + $mech->create_problems_for_body(1, $oxfordshire->id, 'Really old report', { + areas => ',2237,', + lastupdate => '2014-12-01 07:00:00', + user => $user, + }); + + FixMyStreet::Script::ArchiveOldEnquiries::archive($opts); + + my @emails = $mech->get_email; + $mech->email_count_is(1); + + my $email = $emails[0]; + my $body = $mech->get_text_body_from_email($email); + + like $body, qr/Problem the first/, 'Email body matches report name'; + like $body, qr/Problem the second/, 'Email body matches report name'; + like $body, qr/Problem the third/, 'Email body matches report name'; + + unlike $body, qr/Shiny new report/, 'Email body does not have new report'; + unlike $body, qr/Really old report/, 'Email body does not have old report'; + }; +}; + +subtest 'user with old reports does not get email' => sub { + $mech->clear_emails_ok; + $mech->email_count_is(0); + + $mech->create_problems_for_body(4, $oxfordshire->id, 'Really old report', { + areas => ',2237,', + lastupdate => '2014-12-01 07:00:00', + user => $user, + }); + + FixMyStreet::Script::ArchiveOldEnquiries::archive($opts); + + my @emails = $mech->get_email; + $mech->email_count_is(0); +}; + +subtest 'user with new reports does not get email' => sub { + $mech->clear_emails_ok; + $mech->email_count_is(0); + + $mech->create_problems_for_body(4, $oxfordshire->id, 'Shiny new report', { + areas => ',2237,', + user => $user, + }); + + FixMyStreet::Script::ArchiveOldEnquiries::archive($opts); + + $mech->email_count_is(0); +}; + +done_testing(); + +END { + $mech->delete_user($user); + $mech->delete_body($oxfordshire); +} |