Skip to content

Commit 898c299

Browse files
committed
[Open311][BANES] Makes Banes a multi and adds Passthrough
The Passthtrough will receive all reports sent from FMS. Confirm will only be sent reports for Confirm services after by running a script which will use the correct jurisdiction id. The script is still to implemented. mysociety/societyworks#4746
1 parent e1a9cc1 commit 898c299

File tree

6 files changed

+221
-7
lines changed

6 files changed

+221
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
endpoint: https://localhost:4000/
2+
api_key: 123
3+
bearer_details:
4+
username: FMS
5+
password: FMSPassword
6+
url: url/address
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
=head1 NAME
2+
3+
Open311::Endpoint::Integration::UK::BANES - Bath and North East Somerset integration set-up
4+
5+
=head1 SYNOPSIS
6+
7+
BANES manage their own Open311 server to receive all reports made on FMS, whether in
8+
email categories or in those created by their Confirm integration. The Confirm
9+
integration only receives the reports in categories in its services.
10+
11+
=cut
12+
113
package Open311::Endpoint::Integration::UK::BANES;
214

315
use Moo;
4-
extends 'Open311::Endpoint::Integration::Confirm';
16+
extends 'Open311::Endpoint::Integration::Multi';
17+
18+
use Module::Pluggable
19+
search_path => ['Open311::Endpoint::Integration::UK::BANES'],
20+
instantiate => 'new';
21+
22+
has jurisdiction_id => (
23+
is => 'ro',
24+
default => 'BANES',
25+
);
526

6-
around BUILDARGS => sub {
7-
my ($orig, $class, %args) = @_;
8-
$args{jurisdiction_id} = 'banes_confirm';
9-
return $class->$orig(%args);
10-
};
27+
has integration_without_prefix => (
28+
is => 'ro',
29+
default => 'Passthrough',
30+
);
1131

12-
1;
32+
__PACKAGE__->run_if_script;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Open311::Endpoint::Integration::UK::BANES::Confirm;
2+
3+
use Moo;
4+
extends 'Open311::Endpoint::Integration::Confirm';
5+
6+
around BUILDARGS => sub {
7+
my ($orig, $class, %args) = @_;
8+
$args{jurisdiction_id} = 'BANES_confirm';
9+
return $class->$orig(%args);
10+
};
11+
12+
1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=head1 NAME
2+
3+
Open311::Endpoint::Integration::UK::BANES::Passthrough - Bath and North East Somerset Passthrough backend
4+
5+
=head1 SUMMARY
6+
7+
This is the BANES-specific Passthrough integration. It follows Open311
8+
standards except requires a bearer token to be passed rather than an
9+
api token
10+
11+
=cut
12+
13+
package Open311::Endpoint::Integration::UK::BANES::Passthrough;
14+
15+
use Moo;
16+
extends 'Open311::Endpoint::Integration::Passthrough';
17+
18+
around BUILDARGS => sub {
19+
my ($orig, $class, %args) = @_;
20+
$args{jurisdiction_id} = 'www.banes.gov.uk';
21+
return $class->$orig(%args);
22+
};
23+
24+
has bearer_details => (is => 'ro');
25+
26+
=head2
27+
28+
BANES are not providing services or service calls for their own open311 backend.
29+
All service requests should be sent, so we generate an artificial service
30+
that permits the service request to be sent, but maintains the open311 flow.
31+
32+
=cut
33+
34+
sub service {
35+
my ($self, $service_id, $args) = @_;
36+
37+
my $service = Open311::Endpoint::Service->new();
38+
39+
my $attribute = Open311::Endpoint::Service::Attribute->new(
40+
code => $service_id,
41+
datatype => 'string',
42+
);
43+
push @{ $service->attributes }, $attribute;
44+
45+
return $service;
46+
};
47+
48+
=head2
49+
50+
Rather than using an api key, we need to get a bearer token for authorisation
51+
52+
=cut
53+
54+
around _request => sub {
55+
my ($orig, $self, $method, $url, $args) = @_;
56+
57+
delete $args->{uploads};
58+
59+
if ($method eq 'POST' && $url !~ /api\/token/ ) {
60+
$args = { 'Content' => $args, Authorization => 'Bearer ' . $self->_get_bearer_token()->content };
61+
};
62+
63+
return $self->$orig($method, $url, $args);
64+
};
65+
66+
sub _get_bearer_token {
67+
my $self = shift;
68+
69+
my $bearer_details = $self->bearer_details;
70+
71+
my $params = {
72+
username => $bearer_details->{username},
73+
password => $bearer_details->{password},
74+
};
75+
76+
my $url = $bearer_details->{url};
77+
78+
return $self->ua->post($url, $params);
79+
};
80+
81+
1;
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package Open311::Endpoint::Integration::UK::BANES::Passthrough::Dummy;
2+
use Path::Tiny;
3+
use Moo;
4+
extends 'Open311::Endpoint::Integration::UK::BANES::Passthrough';
5+
6+
around BUILDARGS => sub {
7+
my ($orig, $class, %args) = @_;
8+
$args{jurisdiction_id} = 'dummy';
9+
$args{config_file} = path(__FILE__)->sibling("www.banes.gov.uk.yml")->stringify;
10+
$args{endpoint} = '';
11+
return $class->$orig(%args);
12+
};
13+
14+
package main;
15+
16+
use strict;
17+
use warnings;
18+
19+
BEGIN { $ENV{TEST_MODE} = 1; }
20+
21+
use Test::More;
22+
use Test::MockModule;
23+
use JSON::MaybeXS;
24+
use Path::Tiny;
25+
26+
my $test_request = {
27+
jurisdiction_id => 'dummy',
28+
api_key => 'api-key',
29+
service_code => 'Trees_and_woodland',
30+
address_string => '22 Acacia Avenue',
31+
first_name => 'Bob',
32+
last_name => 'Mould',
33+
email => 'test@example.com',
34+
description => 'title: Tree blocking light detail: Tree overhanging garden and blocking light',
35+
media_url => ['https://localhost/one.jpeg?123', 'https://localhost/two.jpeg?123'],
36+
lat => '50',
37+
long => '0.1',
38+
'attribute[description]' => 'Tree overhanging garden and blocking light',
39+
'attribute[title]' => 'Tree blocking light',
40+
'attribute[report_url]' => 'http://localhost/1',
41+
'attribute[easting]' => 1,
42+
'attribute[northing]' => 2,
43+
'attribute[category]' => 'Trees and woodland',
44+
'attribute[fixmystreet_id]' => 1,
45+
};
46+
47+
my $expected_confirm_service_request_post = <<XML;
48+
<?xml version="1.0" encoding="utf-8"?>
49+
<service_requests>
50+
<request>
51+
<service_request_id>293944</service_request_id>
52+
<service_notice>
53+
The City will inspect and require the responsible party to correct within 24 hours and/or issue a Correction Notice or Notice of Violation of the Public Works Code
54+
</service_notice>
55+
<account_id/>
56+
</request>
57+
</service_requests>
58+
XML
59+
60+
my $ua = Test::MockModule->new('LWP::UserAgent');
61+
$ua->mock(post => sub {
62+
my ($self, $url, $args) = @_;
63+
64+
if ($url =~ /token\/api/) {
65+
is $args->{username}, 'FMS', "Username picked up from config";
66+
is $args->{password}, 'FMSPassword', "Password picked up from config";
67+
return HTTP::Response->new(200, 'OK', [], '12345678910');
68+
} else {
69+
is $args->{Authorization}, 'Bearer 12345678910', 'Authorisation header set';
70+
is_deeply $args->{Content}, $test_request;
71+
return HTTP::Response->new(200, 'OK', [], $expected_confirm_service_request_post);
72+
}
73+
});
74+
75+
use_ok 'Open311::Endpoint::Integration::UK::BANES::Passthrough';
76+
77+
my $endpoint = Open311::Endpoint::Integration::UK::BANES::Passthrough::Dummy->new;
78+
79+
subtest 'POST service request' => sub {
80+
my $res = $endpoint->run_test_request(
81+
POST => '/requests.json',
82+
%{ $test_request }
83+
);
84+
85+
ok $res->is_success, 'valid request' or diag $res->content;
86+
is_deeply decode_json($res->content), [ { service_request_id => '293944' } ], 'correct return';
87+
};
88+
89+
done_testing;
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
endpoint: 'test'
2+
api_key: 'test'
3+
bearer_details:
4+
username: 'FMS'
5+
password: 'FMSPassword'
6+
url: 'local/token/api'

0 commit comments

Comments
 (0)