diff options
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 14 | ||||
-rw-r--r-- | t/Mock/Facebook.pm (renamed from t/Facebook.pm) | 2 | ||||
-rw-r--r-- | t/Mock/MapIt.pm (renamed from t/MapIt.pm) | 2 | ||||
-rw-r--r-- | t/Mock/Nominatim.pm (renamed from t/Nominatim.pm) | 2 | ||||
-rw-r--r-- | t/Mock/Static.pm | 18 | ||||
-rw-r--r-- | t/app/controller/alert.t | 4 | ||||
-rw-r--r-- | t/app/controller/around.t | 4 | ||||
-rw-r--r-- | t/app/controller/auth_social.t | 8 | ||||
-rw-r--r-- | t/app/controller/report_import.t | 4 | ||||
-rw-r--r-- | t/cobrand/fixamingata.t | 4 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 27 |
11 files changed, 74 insertions, 15 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 6d846de42..4b6d56146 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -3,6 +3,7 @@ package Open311::GetServiceRequestUpdates; use Moo; use Open311; use FixMyStreet::DB; +use FixMyStreet::App::Model::PhotoSet; use DateTime::Format::W3CDTF; has system_user => ( is => 'rw' ); @@ -116,6 +117,19 @@ sub update_comments { } ); + # ref test as XML::Simple will have returned an empty hashref for empty element + if ($request->{media_url} && !ref $request->{media_url}) { + my $ua = LWP::UserAgent->new; + my $res = $ua->get($request->{media_url}); + if ( $res->is_success && $res->content_type eq 'image/jpeg' ) { + my $photoset = FixMyStreet::App::Model::PhotoSet->new({ + data_items => [ $res->decoded_content ], + }); + my $data = $photoset->get_raw_image_data(0); + $comment->photo($data->[0]); + } + } + # if the comment is older than the last update # do not change the status of the problem as it's # tricky to determine the right thing to do. diff --git a/t/Facebook.pm b/t/Mock/Facebook.pm index 8c258699b..68c63f2d5 100644 --- a/t/Facebook.pm +++ b/t/Mock/Facebook.pm @@ -1,4 +1,4 @@ -package t::Facebook; +package t::Mock::Facebook; use JSON; use Web::Simple; diff --git a/t/MapIt.pm b/t/Mock/MapIt.pm index a10118013..a3bfb10d4 100644 --- a/t/MapIt.pm +++ b/t/Mock/MapIt.pm @@ -1,4 +1,4 @@ -package t::MapIt; +package t::Mock::MapIt; use JSON; use Web::Simple; diff --git a/t/Nominatim.pm b/t/Mock/Nominatim.pm index 2041b4677..3f3cef69a 100644 --- a/t/Nominatim.pm +++ b/t/Mock/Nominatim.pm @@ -1,4 +1,4 @@ -package t::Nominatim; +package t::Mock::Nominatim; use JSON; use Web::Simple; diff --git a/t/Mock/Static.pm b/t/Mock/Static.pm new file mode 100644 index 000000000..260c181cd --- /dev/null +++ b/t/Mock/Static.pm @@ -0,0 +1,18 @@ +package t::Mock::Static; + +use Path::Tiny; +use Web::Simple; + +my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg"); +my $sample_photo = $sample_file->slurp_raw; + +sub dispatch_request { + my $self = shift; + + sub (GET + /image.jpeg) { + my ($self) = @_; + return [ 200, [ 'Content-Type' => 'image/jpeg' ], [ $sample_photo ] ]; + }, +} + +__PACKAGE__->run_if_script; diff --git a/t/app/controller/alert.t b/t/app/controller/alert.t index 5bf2af428..c7dc9ed09 100644 --- a/t/app/controller/alert.t +++ b/t/app/controller/alert.t @@ -6,7 +6,7 @@ use LWP::Protocol::PSGI; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; -use t::Nominatim; +use t::Mock::Nominatim; # check that we can get the page $mech->get_ok('/alert'); @@ -42,7 +42,7 @@ FixMyStreet::override_config { $mech->content_contains('ward:2651:20728:City_of_Edinburgh:City_Centre'); subtest "Test Nominatim lookup" => sub { - LWP::Protocol::PSGI->register(t::Nominatim->run_if_script, host => 'nominatim.openstreetmap.org'); + LWP::Protocol::PSGI->register(t::Mock::Nominatim->run_if_script, host => 'nominatim.openstreetmap.org'); $mech->get_ok('/alert/list?pc=High Street'); $mech->content_contains('We found more than one match for that location'); }; diff --git a/t/app/controller/around.t b/t/app/controller/around.t index a70116525..89ca5246e 100644 --- a/t/app/controller/around.t +++ b/t/app/controller/around.t @@ -3,7 +3,7 @@ use warnings; use Test::More; use LWP::Protocol::PSGI; -use t::MapIt; +use t::Mock::MapIt; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -84,7 +84,7 @@ foreach my $test ( ) { subtest "check lat/lng for '$test->{pc}'" => sub { - LWP::Protocol::PSGI->register(t::MapIt->run_if_script, host => 'mapit.uk'); + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk'); $mech->get_ok('/'); FixMyStreet::override_config { diff --git a/t/app/controller/auth_social.t b/t/app/controller/auth_social.t index 173572b02..84fdd4dfe 100644 --- a/t/app/controller/auth_social.t +++ b/t/app/controller/auth_social.t @@ -5,8 +5,8 @@ use LWP::Protocol::PSGI; use LWP::Simple; use JSON::MaybeXS; -use t::Facebook; -use t::MapIt; +use t::Mock::Facebook; +use t::Mock::MapIt; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -17,7 +17,7 @@ END { FixMyStreet::App->log->enable('info'); } my ($report) = $mech->create_problems_for_body(1, '2345', 'Test'); -LWP::Protocol::PSGI->register(t::MapIt->to_psgi_app, host => 'mapit.uk'); +LWP::Protocol::PSGI->register(t::Mock::MapIt->to_psgi_app, host => 'mapit.uk'); FixMyStreet::override_config { FACEBOOK_APP_ID => 'facebook-app-id', @@ -40,7 +40,7 @@ for my $fb_state ( 'refused', 'no email', 'existing UID', 'okay' ) { } # Set up a mock to catch (most, see below) requests to Facebook - my $fb = t::Facebook->new; + my $fb = t::Mock::Facebook->new; $fb->returns_email(0) if $fb_state eq 'no email' || $fb_state eq 'existing UID'; LWP::Protocol::PSGI->register($fb->to_psgi_app, host => 'www.facebook.com'); LWP::Protocol::PSGI->register($fb->to_psgi_app, host => 'graph.facebook.com'); diff --git a/t/app/controller/report_import.t b/t/app/controller/report_import.t index 1d6695996..220ebaac5 100644 --- a/t/app/controller/report_import.t +++ b/t/app/controller/report_import.t @@ -3,7 +3,7 @@ use warnings; use Test::More; use LWP::Protocol::PSGI; -use t::MapIt; +use t::Mock::MapIt; use FixMyStreet::TestMech; use FixMyStreet::App; use Web::Scraper; @@ -92,7 +92,7 @@ subtest "Test creating bad partial entries" => sub { }; subtest "Submit a correct entry" => sub { - LWP::Protocol::PSGI->register(t::MapIt->run_if_script, host => 'mapit.uk'); + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk'); $mech->get_ok('/import'); diff --git a/t/cobrand/fixamingata.t b/t/cobrand/fixamingata.t index ea3c2da92..50b7713d1 100644 --- a/t/cobrand/fixamingata.t +++ b/t/cobrand/fixamingata.t @@ -8,7 +8,7 @@ BEGIN { FixMyStreet->test_mode(1); } -use t::MapIt; +use t::Mock::MapIt; use mySociety::Locale; use FixMyStreet::TestMech; @@ -102,7 +102,7 @@ subtest "Test ajax decimal points" => sub { # A note to the future - the run_if_script line must be within a subtest # otherwise it fails to work - LWP::Protocol::PSGI->register(t::MapIt->run_if_script, host => 'mapit.sweden'); + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.sweden'); FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixamingata' ], diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 46c3feda4..18a5802bb 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -4,6 +4,8 @@ use strict; use warnings; use Test::More; use CGI::Simple; +use LWP::Protocol::PSGI; +use t::Mock::Static; use_ok( 'Open311' ); @@ -363,6 +365,31 @@ for my $test ( }; } +subtest 'Update with media_url includes image in update' => sub { + my $guard = LWP::Protocol::PSGI->register(t::Mock::Static->to_psgi_app, host => 'example.com'); + + my $local_requests_xml = $requests_xml; + my $updated_datetime = sprintf( '<updated_datetime>%s</updated_datetime>', $dt ); + $local_requests_xml =~ s/UPDATED_DATETIME/$updated_datetime/; + $local_requests_xml =~ s#<service_request_id>\d+</service_request_id># + <service_request_id>@{[$problem->external_id]}</service_request_id> + <media_url>http://example.com/image.jpeg</media_url>#; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); + + $problem->comments->delete; + $problem->lastupdate( DateTime->now()->subtract( days => 1 ) ); + $problem->state('confirmed'); + $problem->update; + + my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); + $update->update_comments( $o, $bodies{2482} ); + + is $problem->comments->count, 1, 'comment count'; + my $c = $problem->comments->first; + is $c->external_id, 638344; + is $c->photo, '1cdd4329ceee2234bd4e89cb33b42061a0724687', 'photo exists'; +}; foreach my $test ( { |