diff options
Diffstat (limited to 't/app/sendreport')
-rw-r--r-- | t/app/sendreport/angus.t | 61 | ||||
-rw-r--r-- | t/app/sendreport/email.t | 37 | ||||
-rw-r--r-- | t/app/sendreport/inspection_required.t | 111 | ||||
-rw-r--r-- | t/app/sendreport/open311.t | 267 |
4 files changed, 459 insertions, 17 deletions
diff --git a/t/app/sendreport/angus.t b/t/app/sendreport/angus.t new file mode 100644 index 000000000..a19ee483a --- /dev/null +++ b/t/app/sendreport/angus.t @@ -0,0 +1,61 @@ +use strict; +use warnings; + +use FixMyStreet::DB; + +use Test::More; + +use_ok("FixMyStreet::SendReport::Angus"); + +my $u = FixMyStreet::DB->resultset('User')->new( { email => 'test@example.org', name => 'A User' } ); + +my $p = FixMyStreet::DB->resultset('Problem')->new( { + latitude => 1, + longitude => 1, + title => 'title', + detail => 'detail', + user => $u, + id => 1, + name => 'A User', + cobrand => 'fixmystreet', +} ); + +my $angus = FixMyStreet::SendReport::Angus->new(); + +subtest 'parses authentication token correctly' => sub { + my $authxml = <<EOT; + <AuthenticateResponse> + + <AuthenticateResult> + TVRreUxqRTJPQzR5TlRVdU1qSjhNakF4Tmpvd01Ub3lNam94TlRvME16b3pPUT09VGhvdVNoYWx0Tm90UGFzcw== + </AuthenticateResult> + <success> + True + </success> + <message></message> + + </AuthenticateResponse> +EOT +; + is $angus->get_auth_token($authxml), 'TVRreUxqRTJPQzR5TlRVdU1qSjhNakF4Tmpvd01Ub3lNam94TlRvME16b3pPUT09VGhvdVNoYWx0Tm90UGFzcw==', 'token correct'; +}; + +subtest 'parses report external id correctly' => sub { + my $respxml = <<EOT; +<CreateRequestResponse> + + <CreateRequestResult> + <RequestId>7245</RequestId> + </CreateRequestResult> + + <success>True</success> + <message></message> + +</CreateRequestResponse> +EOT +; + is $angus->get_external_id($respxml), '7245', 'external id correct'; +}; + + +done_testing(); diff --git a/t/app/sendreport/email.t b/t/app/sendreport/email.t index 04b3854cc..471145dcb 100644 --- a/t/app/sendreport/email.t +++ b/t/app/sendreport/email.t @@ -1,33 +1,34 @@ -#!/usr/bin/perl - use strict; use warnings; use Test::More; use FixMyStreet; -use FixMyStreet::App; -use FixMyStreet::DB::Result::Contact; +use FixMyStreet::DB; use FixMyStreet::SendReport::Email; use FixMyStreet::TestMech; use mySociety::Locale; +ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' ); + my $e = FixMyStreet::SendReport::Email->new(); -my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( +# area id 1000 +my $params = { id => 1000, name => 'Council of the Thousand' }; +my $body = FixMyStreet::DB->resultset('Body')->find_or_create($params); +ok $body, "found/created body"; + +my $contact = $mech->create_contact_ok( email => 'council@example.com', body_id => 1000, category => 'category', - confirmed => 1, - deleted => 0, - editor => 'test suite', - whenedited => DateTime->now, note => '', ); -my $row = FixMyStreet::App->model('DB::Problem')->new( { - council => '1000', +my $row = FixMyStreet::DB->resultset('Problem')->new( { + bodies_str => '1000', category => 'category', + cobrand => '', } ); ok $e; @@ -44,14 +45,14 @@ foreach my $test ( { }, { desc => 'unconfirmed contact results in no receipients', - count => undef, + count => 0, add_council => 1, unconfirmed => 1, - expected_note => 'Council 1000 deleted', + expected_note => 'Body 1000 deleted', }, { desc => 'unconfirmed contact note uses note from contact table', - count => undef, + count => 0, add_council => 1, unconfirmed => 1, note => 'received bounced so unconfirmed', @@ -62,7 +63,7 @@ foreach my $test ( { my $e = FixMyStreet::SendReport::Email->new; $contact->update( { confirmed => 0 } ) if $test->{unconfirmed}; $contact->update( { note => $test->{note} } ) if $test->{note}; - $e->add_council( 1000, { name => 'test council' } ) if $test->{add_council}; + $e->add_body( $body ) if $test->{add_council}; is $e->build_recipient_list( $row, {} ), $test->{count}, 'correct recipient list count'; if ( $test->{unconfirmed} ) { @@ -72,6 +73,8 @@ foreach my $test ( { }; } -$contact->delete; - done_testing(); + +END { + $mech->delete_body($body); +} diff --git a/t/app/sendreport/inspection_required.t b/t/app/sendreport/inspection_required.t new file mode 100644 index 000000000..f9d40d39f --- /dev/null +++ b/t/app/sendreport/inspection_required.t @@ -0,0 +1,111 @@ +use strict; +use warnings; + +use Test::More; + +use FixMyStreet; +use FixMyStreet::DB; +use FixMyStreet::TestMech; +use FixMyStreet::SendReport::Email; +use mySociety::Locale; + +ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' ); + +my $user = $mech->create_user_ok( 'user@example.com' ); + +my $body = $mech->create_body_ok( 2237, 'Oxfordshire County Council', id => 2237 ); +# $body->update({ send_method => 'Email' }); + +my $contact = $mech->create_contact_ok( + body_id => $body->id, + category => 'Pothole', + email => 'test@example.org', +); +$contact->set_extra_metadata(inspection_required => 1); +$contact->update; + +my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', { + cobrand => 'oxfordshire', + category => $contact->category, + user => $user, +}); +my $report = $reports[0]; + +subtest 'Report isn’t sent if uninspected' => sub { + $mech->clear_emails_ok; + + FixMyStreet::DB->resultset('Problem')->send_reports(); + + $mech->email_count_is( 0 ); + is $report->whensent, undef, 'Report hasn’t been sent'; +}; + +subtest 'Report is sent when inspected' => sub { + $mech->clear_emails_ok; + $report->set_extra_metadata(inspected => 1); + $report->update; + + FixMyStreet::DB->resultset('Problem')->send_reports(); + + $report->discard_changes; + $mech->email_count_is( 1 ); + ok $report->whensent, 'Report marked as sent'; +}; + +subtest 'Uninspected report is sent when made by trusted user' => sub { + $mech->clear_emails_ok; + $report->unset_extra_metadata('inspected'); + $report->whensent( undef ); + $report->update; + + $user->user_body_permissions->find_or_create({ + body => $body, + permission_type => 'trusted', + }); + ok $user->has_permission_to('trusted', $report->bodies_str_ids), 'User can make trusted reports'; + + FixMyStreet::DB->resultset('Problem')->send_reports(); + + $report->discard_changes; + $mech->email_count_is( 1 ); + ok $report->whensent, 'Report marked as sent'; + is $report->get_extra_metadata('inspected'), undef, 'Report not marked as inspected'; +}; + +subtest 'Uninspected report isn’t sent when user rep is too low' => sub { + $mech->clear_emails_ok; + $report->whensent( undef ); + $report->update; + + $user->user_body_permissions->delete; + $user->set_extra_metadata(reputation => 15); + $user->update; + + $contact->set_extra_metadata(reputation_threshold => 20); + $contact->update; + + FixMyStreet::DB->resultset('Problem')->send_reports(); + + $report->discard_changes; + $mech->email_count_is( 0 ); + is $report->whensent, undef, 'Report hasn’t been sent'; +}; + +subtest 'Uninspected report is sent when user rep is high enough' => sub { + $user->set_extra_metadata(reputation => 21); + $user->update; + + FixMyStreet::DB->resultset('Problem')->send_reports(); + + $report->discard_changes; + $mech->email_count_is( 1 ); + ok $report->whensent, 'Report marked as sent'; + is $report->get_extra_metadata('inspected'), undef, 'Report not marked as inspected'; +}; + +done_testing(); + +END { + $mech->delete_user($user); + $mech->delete_body($body); +} diff --git a/t/app/sendreport/open311.t b/t/app/sendreport/open311.t new file mode 100644 index 000000000..c4c17577c --- /dev/null +++ b/t/app/sendreport/open311.t @@ -0,0 +1,267 @@ +use strict; +use warnings; + +use Test::More; +use Test::Deep; + +use Open311; +use FixMyStreet::SendReport::Open311; +use FixMyStreet::DB; + +use Data::Dumper; + +package main; +sub test_overrides; # defined below + +use constant TEST_USER_EMAIL => 'fred@example.com'; + +my %standard_open311_parameters = ( + 'use_extended_updates' => 0, + 'send_notpinpointed' => 0, + 'extended_description' => 1, + 'use_service_as_deviceid' => 0, + 'extended_statuses' => 0, + 'always_send_latlong' => 1, + 'debug' => 0, + 'error' => '', + 'endpoints' => { + 'requests' => 'requests.xml', + 'service_request_updates' => 'servicerequestupdates.xml', + 'services' => 'services.xml', + 'update' => 'servicerequestupdates.xml', + }, +); + +test_overrides oxfordshire => + { + body_name => 'Oxfordshire', + body_id => 2237, + row_data => { + postcode => 'OX1 1AA', + }, + extra => { + northing => 100, + easting => 100, + closest_address => '49 St Giles', + }, + }, + superhashof({ + handler => isa('FixMyStreet::Cobrand::Oxfordshire'), + discard_changes => 1, + 'open311' => noclass(superhashof({ + %standard_open311_parameters, + 'extended_description' => 'oxfordshire', + 'endpoints' => { + 'requests' => 'open311_service_request.cgi' + }, + })), + problem_extra => bag( + { name => 'northing', value => 100 }, + { name => 'easting', value => 100 }, + { name => 'closest_address' => value => '49 St Giles' }, + { name => 'external_id', value => re('[0-9]+') }, + ), + }); + +my $bromley_check = + superhashof({ + handler => isa('FixMyStreet::Cobrand::Bromley'), + discard_changes => 1, + 'open311' => noclass(superhashof({ + %standard_open311_parameters, + 'send_notpinpointed' => 1, + 'extended_description' => 0, + 'use_service_as_deviceid' => 0, + 'always_send_latlong' => 0, + })), + problem_extra => bag( + { name => 'report_url' => value => 'http://example.com/1234' }, + { name => 'report_title', value => 'Problem' }, + { name => 'public_anonymity_required', value => 'TRUE' }, + { name => 'email_alerts_requested', value => 'FALSE' }, + { name => 'requested_datetime', value => re(qr/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) }, + { name => 'email', value => TEST_USER_EMAIL }, + { name => 'last_name', value => 'Bloggs' }, + ), + }); + +test_overrides bromley => + { + body_name => 'Bromley', + body_id => 2482, + row_data => { + postcode => 'BR1 1AA', + extra => [ { name => 'last_name', value => 'Bloggs' } ], + }, + extra => { + northing => 100, + easting => 100, + url => 'http://example.com/1234', + }, + }, + $bromley_check; + +test_overrides fixmystreet => + { + body_name => 'Bromley', + body_id => 2482, + row_data => { + postcode => 'BR1 1AA', + # NB: we don't pass last_name here, as main cobrand doesn't know to do this! + }, + extra => { + northing => 100, + easting => 100, + url => 'http://example.com/1234', + }, + }, + $bromley_check; + +test_overrides greenwich => + { + body_name => 'Greenwich', + body_id => 2493, + }, + superhashof({ + handler => isa('FixMyStreet::Cobrand::Greenwich'), + 'open311' => noclass(superhashof({ + %standard_open311_parameters, + })), + problem_extra => bag( + { name => 'external_id', value => re('[0-9]+') }, + ), + }); + +test_overrides fixmystreet => + { + body_name => 'West Berkshire', + body_id => 2619, + row_data => { + postcode => 'RG1 1AA', + }, + }, + superhashof({ + handler => isa('FixMyStreet::Cobrand::WestBerkshire'), + 'open311' => noclass(superhashof({ + %standard_open311_parameters, + 'endpoints' => { + 'requests' => 'Requests', + 'services' => 'Services', + }, + })), + }); + +sub test_overrides { + # NB: Open311 and ::SendReport::Open311 are mocked below in BEGIN { ... } + my ($cobrand, $input, $expected_data) = @_; + subtest "$cobrand ($input->{body_name}) overrides" => sub { + + FixMyStreet::override_config { + ALLOWED_COBRANDS => ['fixmystreet', 'oxfordshire', 'bromley', 'westberkshire', 'greenwich'], + }, sub { + my $db = FixMyStreet::DB->storage->schema; + $db->txn_begin; + + my $params = { id => $input->{body_id}, name => $input->{body_name} }; + my $body = $db->resultset('Body')->find_or_create($params); + $body->body_areas->create({ area_id => $input->{body_id} }); + ok $body, "found/created body " . $input->{body_name}; + $body->update({ can_be_devolved => 1 }); + + my $contact = $body->contacts->find_or_create( + confirmed => 1, + email => 'ZZ', + category => 'ZZ', + deleted => 0, + editor => 'test suite', + note => '', + whenedited => DateTime->now, + jurisdiction => '1234', + api_key => 'SEEKRIT', + body_id => $input->{body_id}, + ); + $contact->update({ send_method => 'Open311', endpoint => 'http://example.com/open311' }); + + my $user = $db->resultset('User')->create( { + name => 'Fred Bloggs', + email => TEST_USER_EMAIL, + password => 'dummy', + }); + + my $row = $db->resultset('Problem')->create( { + title => 'Problem', + detail => 'A big problem', + used_map => 1, + name => 'Fred Bloggs', + anonymous => 1, + state => 'unconfirmed', + bodies_str => $input->{body_id}, + areas => (sprintf ',%d,', $input->{body_id}), + category => 'ZZ', + cobrand => $cobrand, + user => $user, + postcode => 'ZZ1 1AA', + latitude => 100, + longitude => 100, + confirmed => DateTime->now(), + %{ $input->{row_data} || {} }, + } ); + + my $sr = FixMyStreet::SendReport::Open311->new; + $sr->add_body($body, $contact); + $sr->send( $row, $input->{extra} || {} ); + + cmp_deeply (Open311->_get_test_data, $expected_data, 'Data as expected') + or diag Dumper( Open311->_get_test_data ); + + Open311->_reset_test_data(); + $db->txn_rollback; + }; + } +} + +BEGIN { + # Prepare the %data variable to write data from Open311 calls to... + my %data; + package Open311; + use Class::Method::Modifiers; + around new => sub { + my $orig = shift; + my ($class, %params) = @_; + my $self = $class->$orig(%params); + $data{open311} = $self; + $self; + }; + around send_service_request => sub { + my $orig = shift; + my ($self, $problem, $extra, $service_code) = @_; + $data{problem} = { $problem->get_columns }; + $data{extra} = $extra; + $data{problem_extra} = $problem->get_extra_fields; + $data{problem_user} = { $problem->user->get_columns }; + $data{service_code} = $service_code; + # don't actually send the service request! + }; + + sub _get_test_data { return +{ %data } } + sub _reset_test_data { %data = () } + + package FixMyStreet::DB::Result::Problem; + use Class::Method::Modifiers; # is marked as immutable by Moose + sub discard_changes { + $data{discard_changes}++; + # no need to actually discard, as we're in transaction anyway + }; + + package FixMyStreet::DB::Result::Body; + use Class::Method::Modifiers; # is marked as immutable by Moose + around get_cobrand_handler => sub { + my $orig = shift; + my ($self) = @_; + my $handler = $self->$orig(); + $data{handler} = $handler; + $handler; + }; +} + +done_testing(); |