Skip to content

Commit 42abb3f

Browse files
committed
[Open311][BANES] WIP
1 parent e1a9cc1 commit 42abb3f

File tree

6 files changed

+203
-7
lines changed

6 files changed

+203
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
endpoint: https://localhost:4000/
2+
api_key: 123
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
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';
521

6-
around BUILDARGS => sub {
7-
my ($orig, $class, %args) = @_;
8-
$args{jurisdiction_id} = 'banes_confirm';
9-
return $class->$orig(%args);
10-
};
22+
has jurisdiction_id => (
23+
is => 'ro',
24+
default => 'BANES',
25+
);
1126

12-
1;
27+
__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,77 @@
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+
=head2
25+
26+
BANES are not providing services or service calls for their own open311 backend.
27+
All service requests should be sent, so we generate an artificial service
28+
that permits the service request to be sent, but maintains the open311 flow.
29+
30+
=cut
31+
32+
sub service {
33+
my ($self, $service_id, $args) = @_;
34+
35+
my $service = Open311::Endpoint::Service->new();
36+
37+
my $attribute = Open311::Endpoint::Service::Attribute->new(
38+
code => $service_id,
39+
datatype => 'string',
40+
);
41+
push @{ $service->attributes }, $attribute;
42+
43+
return $service;
44+
};
45+
46+
=head2
47+
48+
Rather than using an api key, we need to get a bearer token for authorisation
49+
50+
=cut
51+
52+
around _request => sub {
53+
my ($orig, $self, $method, $url, $args) = @_;
54+
55+
if ($method eq 'POST' && $url !~ /api\/token/ ) {
56+
$args = { 'Content' => $args, Authorization => 'Bearer ' . $self->_get_bearer_token()->content };
57+
};
58+
59+
return $self->$orig($method, $url, $args);
60+
};
61+
62+
sub _get_bearer_token {
63+
my $self = shift;
64+
65+
my $bearer_details = $self->config->{bearer_details};
66+
67+
my $params = {
68+
username => $bearer_details->{username},
69+
password => $bearer_details->{password},
70+
};
71+
72+
my $url = $bearer_details->{url};
73+
74+
return $self->ua->post($url, $params);
75+
};
76+
77+
1;
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
sub _build_config_file { path(__FILE__)->sibling("www.banes.gov.uk.yml")->stringify };
6+
7+
around BUILDARGS => sub {
8+
my ($orig, $class, %args) = @_;
9+
$args{jurisdiction_id} = 'www.banes.gov.uk.yml';
10+
$args{config_file} = path(__FILE__)->sibling("www.banes.gov.uk.yml")->stringify;
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 $pt = Test::MockModule->new('Open311::Endpoint::Integration::UK::BANES::Passthrough::Dummy');
27+
$pt->mock(_build_config_file => sub { path(__FILE__)->sibling("www.banes.gov.uk.yml")->stringify });
28+
$pt->mock(endpoint => sub { '' });
29+
30+
my $expected_confirm_service_request_post = <<XML;
31+
<?xml version="1.0" encoding="utf-8"?>
32+
<service_requests>
33+
<request>
34+
<service_request_id>293944</service_request_id>
35+
<service_notice>
36+
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
37+
</service_notice>
38+
<account_id/>
39+
</request>
40+
</service_requests>
41+
XML
42+
43+
my $ua = Test::MockModule->new('LWP::UserAgent');
44+
$ua->mock(post => sub {
45+
my ($self, $url, $args) = @_;
46+
47+
if ($url =~ /token\/api/) {
48+
return HTTP::Response->new(200, 'OK', [], '8888888888');
49+
} else {
50+
return HTTP::Response->new(200, 'OK', [], $expected_confirm_service_request_post);
51+
}
52+
});
53+
54+
use_ok 'Open311::Endpoint::Integration::UK::BANES::Passthrough';
55+
56+
my $endpoint = Open311::Endpoint::Integration::UK::BANES::Passthrough::Dummy->new;
57+
58+
subtest 'POST service request' => sub {
59+
my $res = $endpoint->run_test_request(
60+
POST => '/requests.json',
61+
jurisdiction_id => 'dummy',
62+
api_key => 'api-key',
63+
service_code => 'Trees_and_woodland',
64+
address_string => '22 Acacia Avenue',
65+
first_name => 'Bob',
66+
last_name => 'Mould',
67+
email => 'test@example.com',
68+
description => 'title: Tree blocking light detail: Tree overhanging garden and blocking light',
69+
media_url => ['https://localhost/one.jpeg?123', 'https://localhost/two.jpeg?123'],
70+
lat => '50',
71+
long => '0.1',
72+
'attribute[description]' => 'Tree overhanging garden and blocking light',
73+
'attribute[title]' => 'Tree blocking light',
74+
'attribute[report_url]' => 'http://localhost/1',
75+
'attribute[easting]' => 1,
76+
'attribute[northing]' => 2,
77+
'attribute[category]' => 'Trees and woodland',
78+
'attribute[fixmystreet_id]' => 1,
79+
);
80+
ok $res->is_success, 'valid request' or diag $res->content;
81+
is_deeply decode_json($res->content), [ { service_request_id => '293944' } ], 'correct return';
82+
};
83+
84+
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)