aboutsummaryrefslogtreecommitdiffstats
path: root/t/open311.t
diff options
context:
space:
mode:
Diffstat (limited to 't/open311.t')
-rw-r--r--t/open311.t277
1 files changed, 217 insertions, 60 deletions
diff --git a/t/open311.t b/t/open311.t
index 2371c53bc..e6ea9b6fc 100644
--- a/t/open311.t
+++ b/t/open311.t
@@ -1,19 +1,18 @@
#!/usr/bin/env perl
+use utf8;
use strict;
use warnings;
+use File::Temp 'tempdir';
+use Path::Tiny;
use Test::More;
use Test::Warn;
-use FixMyStreet::App;
+use FixMyStreet::DB;
use CGI::Simple;
use HTTP::Response;
use DateTime;
use DateTime::Format::W3CDTF;
-use FindBin;
-use lib "$FindBin::Bin/../perllib";
-use lib "$FindBin::Bin/../commonlib/perllib";
-
use_ok( 'Open311' );
my $o = Open311->new();
@@ -27,31 +26,39 @@ EOT
is $o->_process_error( $err_text ), "400: Service Code cannot be null -- can't proceed with the request.\n", 'error text parsing';
is $o->_process_error( '503 - service unavailable' ), 'unknown error', 'error text parsing of bad error';
-my $o2 = Open311->new( endpoint => 'http://192.168.50.1/open311/', jurisdiction => 'example.org' );
-
-my $u = FixMyStreet::App->model('DB::User')->new( { email => 'test@example.org', name => 'A User' } );
-
-my $p = FixMyStreet::App->model('DB::Problem')->new( {
- latitude => 1,
- longitude => 1,
- title => 'title',
- detail => 'detail',
- user => $u,
- id => 1,
-} );
-
-my $expected_error = qr{Failed to submit problem 1 over Open311}ism;
-
-warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} $expected_error, 'warning generated on failed call';
+my $o2 = Open311->new( endpoint => 'http://127.0.0.1/open311/', jurisdiction => 'example.org' );
+
+my $u = FixMyStreet::DB->resultset('User')->new( { email => 'test@example.org', name => 'A User' } );
+
+for my $sfc (0..2) {
+ my $p = FixMyStreet::DB->resultset('Problem')->new( {
+ latitude => 1,
+ longitude => 1,
+ title => 'title',
+ detail => 'detail',
+ user => $u,
+ id => 1,
+ name => 'A User',
+ cobrand => 'fixmystreet',
+ send_fail_count => $sfc,
+ } );
+ my $expected_error = qr{Failed to submit problem 1 over Open311}ism;
+
+ if ($sfc == 2) {
+ warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} $expected_error, 'warning generated on failed call';
+ } else {
+ warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} undef, 'no warning generated on failed call';
+ }
+}
my $dt = DateTime->now();
-my $user = FixMyStreet::App->model('DB::User')->new( {
+my $user = FixMyStreet::DB->resultset('User')->new( {
name => 'Test User',
email => 'test@example.com',
} );
-my $problem = FixMyStreet::App->model('DB::Problem')->new( {
+my $problem = FixMyStreet::DB->resultset('Problem')->new( {
id => 80,
external_id => 81,
state => 'confirmed',
@@ -61,19 +68,20 @@ my $problem = FixMyStreet::App->model('DB::Problem')->new( {
latitude => 1,
longitude => 2,
user => $user,
+ name => 'Test User',
+ cobrand => 'fixmystreet',
} );
subtest 'posting service request' => sub {
my $extra = {
url => 'http://example.com/report/1',
+ easting => 'SET',
};
my $results = make_service_req( $problem, $extra, $problem->category, '<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>' );
is $results->{ res }, 248, 'got request id';
- my $req = $o->test_req_used;
-
my $description = <<EOT;
title: a problem
@@ -86,13 +94,14 @@ EOT
;
my $c = CGI::Simple->new( $results->{ req }->content );
+ (my $c_description = $c->param('description')) =~ s/\r\n/\n/g;
is $c->param('email'), $user->email, 'correct email';
is $c->param('first_name'), 'Test', 'correct first name';
is $c->param('last_name'), 'User', 'correct last name';
is $c->param('lat'), 1, 'latitide correct';
is $c->param('long'), 2, 'longitude correct';
- is $c->param('description'), $description, 'description correct';
+ is $c_description, $description, 'description correct';
is $c->param('service_code'), 'pothole', 'service code correct';
};
@@ -106,13 +115,11 @@ subtest 'posting service request with basic_description' => sub {
$extra,
$problem->category,
'<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>',
- { basic_description => 1 },
+ { extended_description => 0 },
);
is $results->{ res }, 248, 'got request id';
- my $req = $o->test_req_used;
-
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('description'), $problem->detail, 'description correct';
@@ -151,7 +158,7 @@ for my $test (
],
},
{
- title => 'magic fms_extra parameters handled correctly',
+ desc => 'magic fms_extra parameters handled correctly',
extra => [
{
name => 'fms_extra_title',
@@ -176,7 +183,6 @@ for my $test (
my $results = make_service_req( $problem, $extra, $problem->category,
'<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>'
);
- my $req = $o->test_req_used;
my $c = CGI::Simple->new( $results->{req}->content );
for my $param ( @{ $test->{params} } ) {
@@ -185,13 +191,38 @@ for my $test (
};
}
-my $comment = FixMyStreet::App->model('DB::Comment')->new( {
+for my $test (
+ {
+ desc => 'Check uses report name over user name',
+ name => 'Nom de Report',
+ first_name => 'Nom',
+ last_name => 'de Report',
+ },
+) {
+ subtest $test->{desc} => sub {
+ $problem->extra( undef );
+ $problem->name( $test->{name} );
+ my $extra = { url => 'http://example.com/report/1', };
+
+ my $results = make_service_req( $problem, $extra, $problem->category,
+'<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>'
+ );
+ my $c = CGI::Simple->new( $results->{req}->content );
+
+ is $c->param( 'first_name' ), $test->{first_name}, 'correct first name';
+ is $c->param( 'last_name' ), $test->{last_name}, 'correct last name';
+ };
+}
+
+
+my $comment = FixMyStreet::DB->resultset('Comment')->new( {
id => 38362,
user => $user,
problem => $problem,
anonymous => 0,
text => 'this is a comment',
confirmed => $dt,
+ problem_state => 'confirmed',
extra => { title => 'Mr', email_alerts_requested => 0 },
} );
@@ -200,8 +231,6 @@ subtest 'basic request update post parameters' => sub {
is $results->{ res }, 248, 'got update id';
- my $req = $o->test_req_used;
-
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('description'), 'this is a comment', 'email correct';
@@ -216,12 +245,10 @@ subtest 'basic request update post parameters' => sub {
};
subtest 'extended request update post parameters' => sub {
- my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', 1 );
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', { use_extended_updates => 1 } );
is $results->{ res }, 248, 'got update id';
- my $req = $o->test_req_used;
-
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('description'), 'this is a comment', 'email correct';
@@ -239,18 +266,26 @@ subtest 'extended request update post parameters' => sub {
};
subtest 'check media url set' => sub {
- $comment->photo(1);
- $comment->cobrand('fixmystreet');
+ my $UPLOAD_DIR = tempdir( CLEANUP => 1 );
- my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
+ my $image_path = path('t/app/controller/sample.jpg');
+ $image_path->copy( path( $UPLOAD_DIR, '0123456789012345678901234567890123456789.jpeg' ) );
- is $results->{ res }, 248, 'got update id';
+ $comment->photo("0123456789012345678901234567890123456789");
+ $comment->cobrand('fixmystreet');
- my $req = $o->test_req_used;
+ FixMyStreet::override_config {
+ UPLOAD_DIR => $UPLOAD_DIR,
+ }, sub {
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
- my $c = CGI::Simple->new( $results->{ req }->content );
- my $expected_path = '/c/' . $comment->id . '.full.jpeg';
- like $c->param('media_url'), qr/$expected_path/, 'image url included';
+ is $results->{ res }, 248, 'got update id';
+
+ my $c = CGI::Simple->new( $results->{ req }->content );
+ my $expected_path = '/c/' . $comment->id . '.0.full.jpeg';
+ like $c->param('media_url'), qr/$expected_path/, 'image url included';
+ };
+ $comment->photo(undef);
};
foreach my $test (
@@ -258,16 +293,47 @@ foreach my $test (
desc => 'comment with fixed state sends status of CLOSED',
state => 'fixed',
status => 'CLOSED',
+ extended => 'FIXED',
},
{
desc => 'comment with fixed - user state sends status of CLOSED',
state => 'fixed - user',
status => 'CLOSED',
+ extended => 'FIXED',
},
{
desc => 'comment with fixed - council state sends status of CLOSED',
state => 'fixed - council',
status => 'CLOSED',
+ extended => 'FIXED',
+ },
+ {
+ desc => 'comment with duplicate state sends status of CLOSED',
+ state => 'duplicate',
+ anon => 0,
+ status => 'CLOSED',
+ extended => 'DUPLICATE',
+ },
+ {
+ desc => 'comment with not reponsible state sends status of CLOSED',
+ state => 'not responsible',
+ anon => 0,
+ status => 'CLOSED',
+ extended => 'NOT_COUNCILS_RESPONSIBILITY',
+ },
+ {
+ desc => 'comment with unable to fix state sends status of CLOSED',
+ state => 'unable to fix',
+ anon => 0,
+ status => 'CLOSED',
+ extended => 'NO_FURTHER_ACTION',
+ },
+ {
+ desc => 'comment with internal referral state sends status of CLOSED',
+ state => 'internal referral',
+ anon => 0,
+ status => 'CLOSED',
+ extended => 'INTERNAL_REFERRAL',
},
{
desc => 'comment with closed state sends status of CLOSED',
@@ -278,29 +344,42 @@ foreach my $test (
desc => 'comment with investigating state sends status of OPEN',
state => 'investigating',
status => 'OPEN',
+ extended => 'INVESTIGATING',
},
{
desc => 'comment with planned state sends status of OPEN',
state => 'planned',
status => 'OPEN',
+ extended => 'ACTION_SCHEDULED',
},
{
- desc => 'comment with in progress state sends status of OPEN',
- state => 'in progress',
+ desc => 'comment with action scheduled state sends status of OPEN',
+ state => 'action scheduled',
+ anon => 0,
status => 'OPEN',
+ extended => 'ACTION_SCHEDULED',
},
{
- state => 'confirmed',
+ desc => 'comment with in progress state sends status of OPEN',
+ state => 'in progress',
status => 'OPEN',
+ extended => 'IN_PROGRESS',
},
) {
subtest $test->{desc} => sub {
+ $comment->problem_state( $test->{state} );
$comment->problem->state( $test->{state} );
my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('status'), $test->{status}, 'correct status';
+
+ if ( $test->{extended} ) {
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', { extended_statuses => 1 } );
+ my $c = CGI::Simple->new( $results->{ req }->content );
+ is $c->param('status'), $test->{extended}, 'correct extended status';
+ }
};
}
@@ -319,16 +398,70 @@ for my $test (
},
) {
subtest $test->{desc} => sub {
+ $comment->problem_state( $test->{state} );
$comment->problem->state( $test->{state} );
$comment->anonymous( $test->{anon} );
- my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', 1 );
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', { use_extended_updates => 1 } );
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('public_anonymity_required'), $test->{anon} ? 'TRUE' : 'FALSE', 'correct anonymity';
};
}
+my $dt2 = $dt->clone;
+$dt2->add( 'minutes' => 1 );
+
+my $comment2 = FixMyStreet::DB->resultset('Comment')->new( {
+ id => 38363,
+ user => $user,
+ problem => $problem,
+ anonymous => 0,
+ text => 'this is a comment',
+ confirmed => $dt,
+ problem_state => 'confirmed',
+ extra => { title => 'Mr', email_alerts_requested => 0 },
+} );
+
+for my $test (
+ {
+ desc => 'comment with fixed - council state sends status of CLOSED even if problem is open',
+ state => 'fixed - council',
+ problem_state => 'confirmed',
+ status => 'CLOSED',
+ extended => 'FIXED',
+ },
+ {
+ desc => 'comment marked open sends status of OPEN even if problem is closed',
+ state => 'confirmed',
+ problem_state => 'fixed - council',
+ status => 'OPEN',
+ extended => 'OPEN',
+ },
+ {
+ desc => 'comment with no problem state falls back to report state',
+ state => '',
+ problem_state => 'fixed - council',
+ status => 'CLOSED',
+ extended => 'FIXED',
+ },
+) {
+ subtest $test->{desc} => sub {
+ $comment->problem_state( $test->{state} );
+ $comment->problem->state( $test->{problem_state} );
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
+
+ my $c = CGI::Simple->new( $results->{ req }->content );
+ is $c->param('status'), $test->{status}, 'correct status';
+
+ if ( $test->{extended} ) {
+ my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', { extended_statuses => 1 } );
+ my $c = CGI::Simple->new( $results->{ req }->content );
+ is $c->param('status'), $test->{extended}, 'correct extended status';
+ }
+ };
+}
+
for my $test (
{
@@ -445,6 +578,9 @@ for my $test (
};
}
+$problem->send_fail_count(2);
+$comment->send_fail_count(2);
+
subtest 'No request id in reponse' => sub {
my $results;
warning_like {
@@ -534,24 +670,45 @@ for my $test (
};
}
+subtest 'check FixaMinGata' => sub {
+ $problem->cobrand('fixamingata');
+ $problem->detail("MØØse");
+ my $extra = {
+ url => 'http://example.com/report/1',
+ };
+ my $results = make_service_req( $problem, $extra, $problem->category, '<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>' );
+ is $results->{ res }, 248, 'got request id';
+ my $description = <<EOT;
+Beskrivning: MØØse
+
+Länk till ärendet: http://example.com/report/1
+
+Skickad via FixaMinGata
+EOT
+;
+ my $c = CGI::Simple->new( $results->{ req }->content );
+ (my $c_description = $c->param('description')) =~ s/\r\n/\n/g;
+ utf8::decode($c_description);
+ is $c_description, $description, 'description correct';
+};
+
done_testing();
+
sub make_update_req {
my $comment = shift;
my $xml = shift;
- my $extended = shift;
+ my $open311_args = shift || {};
my $params = {
- object => $comment,
- xml => $xml,
- method => 'post_service_request_update',
- path => 'servicerequestupdates.xml',
+ object => $comment,
+ xml => $xml,
+ method => 'post_service_request_update',
+ path => 'servicerequestupdates.xml',
+ open311_conf => $open311_args,
};
- if ( $extended ) {
- $params->{ open311_conf } = { use_extended_updates => 1 };
- }
- return make_req( $params );
+ return _make_req( $params );
}
sub make_service_req {
@@ -561,7 +718,7 @@ sub make_service_req {
my $xml = shift;
my $open311_args = shift || {};
- return make_req(
+ return _make_req(
{
object => $problem,
xml => $xml,
@@ -573,7 +730,7 @@ sub make_service_req {
);
}
-sub make_req {
+sub _make_req {
my $args = shift;
my $object = $args->{object};