aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--perllib/FixMyStreet/App/Controller/Open311/Updates.pm88
-rw-r--r--t/app/controller/open311_updates.t99
3 files changed, 188 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 788e4bb0a..fd0fdcaea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@
POST service request.
- Allow description in email template with placeholder.
- Do not store display-only extra fields on new reports.
+ - Support receiving updates from external source.
- Improve JSON output of controller.
* v2.6 (3rd May 2019)
diff --git a/perllib/FixMyStreet/App/Controller/Open311/Updates.pm b/perllib/FixMyStreet/App/Controller/Open311/Updates.pm
new file mode 100644
index 000000000..105400a8a
--- /dev/null
+++ b/perllib/FixMyStreet/App/Controller/Open311/Updates.pm
@@ -0,0 +1,88 @@
+package FixMyStreet::App::Controller::Open311::Updates;
+
+use utf8;
+use Moose;
+use namespace::autoclean;
+use Open311;
+use Open311::GetServiceRequestUpdates;
+
+BEGIN { extends 'Catalyst::Controller'; }
+
+=head1 NAME
+
+FixMyStreet::App::Controller::Open311::Updates - Catalyst Controller
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=cut
+
+sub receive : Regex('^open311/v2/servicerequestupdates.(xml|json)$') : Args(0) {
+ my ( $self, $c ) = @_;
+ $c->stash->{format} = $c->req->captures->[0];
+
+ $c->detach('bad_request', [ 'POST' ]) unless $c->req->method eq 'POST';
+
+ my $body;
+ if ($c->cobrand->can('council_area_id')) {
+ $body = $c->cobrand->body;
+ } else {
+ $body = $c->model('DB::Body')->find({ id => $c->get_param('jurisdiction_id') });
+ }
+ $c->detach('bad_request', ['jurisdiction_id']) unless $body;
+ my $user = $body->comment_user;
+
+ my $key = $c->get_param('api_key') || '';
+ my $token = $c->cobrand->feature('open311_token') || '';
+ $c->detach('bad_request', [ 'api_key' ]) unless $key && $key eq $token;
+
+ my $request = {
+ media_url => $c->get_param('media_url'),
+ external_status_code => $c->get_param('external_status_code'),
+ };
+ foreach (qw(service_request_id update_id updated_datetime status description)) {
+ $request->{$_} = $c->get_param($_) || $c->detach('bad_request', [ $_ ]);
+ }
+
+ my %open311_conf = (
+ endpoint => $body->endpoint,
+ api_key => $body->api_key,
+ jurisdiction => $body->jurisdiction,
+ extended_statuses => $body->send_extended_statuses,
+ );
+
+ my $cobrand = $body->get_cobrand_handler;
+ $cobrand->call_hook(open311_config_updates => \%open311_conf)
+ if $cobrand;
+
+ my $open311 = Open311->new(%open311_conf);
+ my $updates = Open311::GetServiceRequestUpdates->new(
+ system_user => $user,
+ current_open311 => $open311,
+ current_body => $body,
+ );
+
+ my $p = $updates->find_problem($request);
+ $c->detach('bad_request', [ 'not found' ]) unless $p;
+
+ my $comment = $p->comments->search( { external_id => $request->{update_id} } )->first;
+ $c->detach('bad_request', [ 'already exists' ]) if $comment;
+
+ $comment = $updates->process_update($request, $p);
+
+ my $data = { service_request_updates => { update_id => $comment->id } };
+
+ $c->forward('/open311/format_output', [ $data ]);
+}
+
+sub bad_request : Private {
+ my ($self, $c, $comment) = @_;
+ $c->response->status(400);
+ $c->forward('/open311/format_output', [ { errors => { code => 400, description => "Bad request: $comment" } } ]);
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
+
diff --git a/t/app/controller/open311_updates.t b/t/app/controller/open311_updates.t
new file mode 100644
index 000000000..f2db0a02b
--- /dev/null
+++ b/t/app/controller/open311_updates.t
@@ -0,0 +1,99 @@
+use XML::Simple;
+use FixMyStreet::TestMech;
+my $mech = FixMyStreet::TestMech->new;
+
+my $user = $mech->create_user_ok('commentuser@example.com');
+my $body = $mech->create_body_ok(2237, 'Oxfordshire County Council', {
+ comment_user => $user,
+ api_key => 'sending-key',
+ jurisdiction => 'none',
+ endpoint => 'endpoint',
+});
+my ($problem) = $mech->create_problems_for_body(1, $body->id, 'Open311 updates', { external_id => 'p123' });
+
+subtest 'bad requests do not get through' => sub {
+ $mech->get('/open311/v2/servicerequestupdates.xml');
+ is $mech->response->code, 400, 'Is bad request';
+ $mech->content_contains('<description>Bad request: POST</description>');
+
+ $mech->post('/open311/v2/servicerequestupdates.xml');
+ is $mech->response->code, 400, 'Is bad request';
+ my $xml = _get_xml_object($mech->content);
+ is $xml->{error}[0]{description}, 'Bad request: jurisdiction_id';
+};
+
+subtest 'cobrand gets jurisdiction, but needs a token' => sub {
+ FixMyStreet::override_config {
+ ALLOWED_COBRANDS => 'oxfordshire',
+ }, sub {
+ $mech->post('/open311/v2/servicerequestupdates.xml');
+ is $mech->response->code, 400, 'Is bad request';
+ my $xml = _get_xml_object($mech->content);
+ is $xml->{error}[0]{description}, 'Bad request: api_key';
+ };
+
+ FixMyStreet::override_config {
+ ALLOWED_COBRANDS => 'oxfordshire',
+ COBRAND_FEATURES => { open311_token => { oxfordshire => 'wrong-token' } },
+ }, sub {
+ $mech->post('/open311/v2/servicerequestupdates.xml');
+ is $mech->response->code, 400, 'Is bad request';
+ my $xml = _get_xml_object($mech->content);
+ is $xml->{error}[0]{description}, 'Bad request: api_key';
+ };
+};
+
+subtest 'With all data, an update is added' => sub {
+ FixMyStreet::override_config {
+ ALLOWED_COBRANDS => 'oxfordshire',
+ COBRAND_FEATURES => { open311_token => { oxfordshire => 'receiving-token' } },
+ }, sub {
+ $mech->post_ok('/open311/v2/servicerequestupdates.xml', {
+ api_key => 'receiving-token',
+ service_request_id => $problem->external_id,
+ update_id => 'c123',
+ updated_datetime => $problem->confirmed->clone->add(hours => 2),
+ status => 'CLOSED',
+ description => 'This report has been fixed',
+ });
+ };
+
+ $problem->discard_changes;
+ is $problem->state, 'fixed - council', 'problem updated';
+ is $problem->comments->count, 1, 'One comment created';
+
+ my $comment = $problem->comments->first;
+ is $comment->text, 'This report has been fixed', 'correct text';
+ is $comment->user_id, $user->id, 'correct user';
+ is $comment->problem_state, 'fixed - council', 'correct state';
+ is $comment->external_id, 'c123', 'correct external id';
+
+ my $xml = _get_xml_object($mech->content);
+ my $response = $xml->{request_update};
+ is $response->[0]->{update_id}, $comment->id, 'correct id in response';
+};
+
+done_testing();
+
+sub _get_xml_object {
+ my ($xml) = @_;
+
+ # Of these, services/service_requests/service_request_updates are root
+ # elements, so GroupTags has no effect, but this is used in ForceArray too.
+ my $group_tags = {
+ services => 'service',
+ attributes => 'attribute',
+ values => 'value',
+ service_requests => 'request',
+ errors => 'error',
+ service_request_updates => 'request_update',
+ };
+ my $simple = XML::Simple->new(
+ ForceArray => [ values %$group_tags ],
+ KeyAttr => {},
+ GroupTags => $group_tags,
+ SuppressEmpty => undef,
+ );
+ my $obj = $simple->parse_string($xml);
+ return $obj;
+}