Skip to content

Commit

Permalink
Merge branch 'handle-uploaded-photos-in-atak-integration' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
neprune committed Sep 19, 2024
2 parents b4a36c3 + ab1d980 commit be3fdec
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 77 deletions.
19 changes: 16 additions & 3 deletions perllib/Open311/Endpoint/Integration/ATAK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,30 @@ sub post_service_request {
die "Args must be a hashref" unless ref $args eq 'HASH';

$self->logger->info("[ATAK] Creating issue for service " . $service->service_name);

# Uploads break the json encoding for the debug logs, so popping beforehand.
my $uploads = delete $args->{uploads};
$self->logger->debug("[ATAK] POST service request args: " . encode_json($args));

my @attachments;

if ($args->{media_url}) {
my $i = 1;
my $image_counter = 1;
if (@{$uploads}) {

@attachments = map {
my $content_type = $_->content_type ? $_->content_type : 'image/jpeg';
{
filename => $_->filename,
description => "Image " . $image_counter++,
data => "data:" . $content_type . ";base64," . encode_base64(path($_)->slurp)
}
} @{$uploads};
} elsif (@{$args->{media_url}}) {
@attachments = map {
my $content_type = $_->content_type ? $_->content_type : 'image/jpeg';
{
filename => $_->filename,
description => "Image " . $i++,
description => "Image " . $image_counter++,
data => "data:" . $content_type . ";base64," . encode_base64($_->content)
}
} $self->_get_attachments($args->{media_url});
Expand Down
2 changes: 0 additions & 2 deletions perllib/Open311/Endpoint/Integration/Boomi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ sub _add_attachments {

my @attachments;

my $ua = LWP::UserAgent->new(agent => "FixMyStreet/open311-adapter");

for my $photo (@{ $args->{media_url} }) {
my $filename = (URI->new($photo)->path_segments)[-1];
push @attachments, {
Expand Down
165 changes: 93 additions & 72 deletions t/open311/endpoint/brent.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use YAML::XS qw(LoadFile);
use Path::Tiny;
use File::Temp qw(tempfile);
use Encode qw(encode);
use HTTP::Request::Common;
use Test::More;
use Test::MockModule;
use Test::LongString;
Expand Down Expand Up @@ -634,81 +635,101 @@ subtest "POST sack waste Echo service request OK" => sub {
} ], 'correct json returned';
};

subtest "POST Parks littering ATAK service request OK" => sub {
set_fixed_time('2023-07-27T12:00:00Z');
my $mock_ua = Test::MockModule->new('LWP::UserAgent');
$mock_ua->mock('get', sub {
my ($self, $url) = @_;
if ($url eq 'http://example.org/photo/1.jpeg') {
my $image_data = path(__FILE__)->sibling('files')->child('test_image.jpg')->slurp;
my $response = HTTP::Response->new(200, 'OK', []);
$response->header('Content-Disposition' => 'attachment; filename="1.jpeg"');
$response->header('Content-Type' => 'image/jpeg');
$response->content($image_data);
return $response;
} else {
return HTTP::Response->new(404, 'Not Found', [], '');
}
});
for my $test (
{ upload => 0 },
{ upload => 1 },
) {
subtest "POST Parks littering ATAK service request OK (photo upload $test->{upload})" => sub {
set_fixed_time('2023-07-27T12:00:00Z');
my $mock_ua = Test::MockModule->new('LWP::UserAgent');
$mock_ua->mock('get', sub {
my ($self, $url) = @_;
if ($url eq 'http://example.org/photo/1.jpeg') {
my $image_data = path(__FILE__)->sibling('files')->child('test_image.jpg')->slurp;
my $response = HTTP::Response->new(200, 'OK', []);
$response->header('Content-Disposition' => 'attachment; filename="1.jpeg"');
$response->header('Content-Type' => 'image/jpeg');
$response->content($image_data);
return $response;
} else {
return HTTP::Response->new(404, 'Not Found', [], '');
}
});

$mock_ua->mock('post', sub {
my ($self, $url, %headers) = @_;
if ($url eq 'https://example.com/ords/hws/atak/v1/enq') {
is $headers{Authorization}, 'AUTH-123';

my $data = decode_json($headers{Content})->{tasks}->[0];
is $data->{issue}, "Category: Parks littering\nGroup: Parks and open spaces\nLocation: Location name\n\n" .
"location of problem: title\n\ndetail: detail\n\nurl: url\n\n" .
"Submitted via FixMyStreet\n";
is $data->{client_ref}, '42';
is $data->{project_name}, 'LB BRENT';
is $data->{project_code}, 'C123';
is $data->{taken_on}, '2023-07-27T12:00:00Z';
is $data->{location_name}, 'Location name';
is $data->{caller}, '';
is $data->{resolve_by}, '';
is $data->{location}->{type}, 'Point';
is_deeply $data->{location}->{coordinates}, [ -1, 51 ];
my $photo = $data->{attachments}->[0];
is $photo->{filename}, '1.jpeg';
is $photo->{description}, 'Image 1';
like $photo->{data}, qr{^data:image/jpeg;base64,/9j/4};

return HTTP::Response->new(200, 'OK', [], '{"Processed task 1": "123ABC"}');
} elsif ($url eq 'https://example.com/ords/hws/atak/v1/login') {
return HTTP::Response->new(200, 'OK', [], '{"token": "AUTH-123"}');
} else {
return HTTP::Response->new(404, 'Not Found', [], '');
}
});
$mock_ua->mock('post', sub {
my ($self, $url, %headers) = @_;
if ($url eq 'https://example.com/ords/hws/atak/v1/enq') {
is $headers{Authorization}, 'AUTH-123';

my $data = decode_json($headers{Content})->{tasks}->[0];
is $data->{issue}, "Category: Parks littering\nGroup: Parks and open spaces\nLocation: Location name\n\n" .
"location of problem: title\n\ndetail: detail\n\nurl: url\n\n" .
"Submitted via FixMyStreet\n";
is $data->{client_ref}, '42';
is $data->{project_name}, 'LB BRENT';
is $data->{project_code}, 'C123';
is $data->{taken_on}, '2023-07-27T12:00:00Z';
is $data->{location_name}, 'Location name';
is $data->{caller}, '';
is $data->{resolve_by}, '';
is $data->{location}->{type}, 'Point';
is_deeply $data->{location}->{coordinates}, [ -1, 51 ];
my $photo = $data->{attachments}->[0];
if ($test->{upload}) {
is $photo->{filename}, 'test_image.jpg';
} else {
is $photo->{filename}, '1.jpeg';
}
is $photo->{description}, 'Image 1';
like $photo->{data}, qr{^data:image/jpeg;base64,/9j/4};

my $res = $endpoint->run_test_request(
POST => '/requests.json',
api_key => 'test',
service_code => 'ATAK-PARK_LITTERING',
first_name => 'Bob',
last_name => 'Mould',
description => "Lots of litter in the park",
lat => 51,
long => -1,
media_url => 'http://example.org/photo/1.jpeg',
'attribute[location_name]' => 'Location name',
'attribute[easting]' => EASTING,
'attribute[northing]' => NORTHING,
'attribute[fixmystreet_id]' => 42,
'attribute[report_url]' => 'url',
'attribute[detail]' => 'detail',
'attribute[title]' => 'title',
'attribute[group]' => 'Parks and open spaces',
);
ok $res->is_success, 'valid request';
return HTTP::Response->new(200, 'OK', [], '{"Processed task 1": "123ABC"}');
} elsif ($url eq 'https://example.com/ords/hws/atak/v1/login') {
return HTTP::Response->new(200, 'OK', [], '{"token": "AUTH-123"}');
} else {
return HTTP::Response->new(404, 'Not Found', [], '');
}
});

is_deeply decode_json($res->content), [
{
"service_request_id" => "ATAK-123ABC"
}
], 'correct json returned' or diag $res->content;
};
my $photo_upload = Web::Dispatch::Upload->new(
tempname => path(__FILE__)->dirname . '/files/test_image.jpg',
filename => 'image.jpg',
);
my $req = POST '/requests.json',
Content_Type => 'form-data',
Content => [
api_key => 'test',
service_code => 'ATAK-PARK_LITTERING',
first_name => 'Bob',
last_name => 'Mould',
description => "Lots of litter in the park",
lat => 51,
long => -1,
(
$test->{upload}
? ('uploads' => [ $photo_upload ])
: ('media_url' => 'http://example.org/photo/1.jpeg')
),
'attribute[location_name]' => 'Location name',
'attribute[easting]' => EASTING,
'attribute[northing]' => NORTHING,
'attribute[fixmystreet_id]' => 42,
'attribute[report_url]' => 'url',
'attribute[detail]' => 'detail',
'attribute[title]' => 'title',
'attribute[group]' => 'Parks and open spaces',
];

my $res = $endpoint->run_test_request($req);
ok $res->is_success, 'valid request';

is_deeply decode_json($res->content), [
{
"service_request_id" => "ATAK-123ABC"
}
], 'correct json returned' or diag $res->content;
};
}

subtest "POST Echo update OK" => sub {
my $res = $endpoint->run_test_request(
Expand Down

0 comments on commit be3fdec

Please sign in to comment.