aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/Cobrand/UK.pm4
-rw-r--r--perllib/Open311.pm26
-rw-r--r--t/Mock/Bing.pm37
-rw-r--r--t/Mock/MapIt.pm3
-rw-r--r--t/cobrand/closest.t31
-rw-r--r--t/open311.t6
6 files changed, 82 insertions, 25 deletions
diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm
index 46891f906..1a5682dad 100644
--- a/perllib/FixMyStreet/Cobrand/UK.pm
+++ b/perllib/FixMyStreet/Cobrand/UK.pm
@@ -5,6 +5,7 @@ use strict;
use JSON::MaybeXS;
use mySociety::MaPit;
use mySociety::VotingArea;
+use Utils;
sub country { return 'GB'; }
sub area_types { [ 'DIS', 'LBO', 'MTD', 'UTA', 'CTY', 'COI', 'LGD' ] }
@@ -122,7 +123,8 @@ sub find_closest {
my $data = $self->SUPER::find_closest($problem, $as_data);
my $mapit_url = FixMyStreet->config('MAPIT_URL');
- my $url = $mapit_url . "nearest/4326/" . $problem->longitude . ',' . $problem->latitude;
+ my ($lat, $lon) = map { Utils::truncate_coordinate($_) } $problem->latitude, $problem->longitude;
+ my $url = $mapit_url . "nearest/4326/$lon,$lat";
my $j = LWP::Simple::get($url);
if ($j) {
$j = JSON->new->utf8->allow_nonref->decode($j);
diff --git a/perllib/Open311.pm b/perllib/Open311.pm
index c1323acfa..b52cee0ea 100644
--- a/perllib/Open311.pm
+++ b/perllib/Open311.pm
@@ -59,6 +59,24 @@ sub get_service_meta_info {
return $self->_get_xml_object( $service_meta_xml );
}
+sub to_bristol {
+ my $problem = shift;
+ return unless $problem->cobrand =~ /fixmystreet|bristol/;
+ my $bodies = $problem->bodies;
+ return unless %$bodies;
+ my $body = (values %$bodies)[0];
+ return unless $body->areas->{2561};
+ return 1;
+}
+
+sub warn_failure {
+ my ($obj, $problem) = @_;
+ # Special case a poorly behaving Open311 server
+ my $threshold = 1;
+ $threshold = 5 if to_bristol($problem || $obj);
+ return $obj->send_fail_count && $obj->send_fail_count == $threshold;
+}
+
sub send_service_request {
my $self = shift;
my $problem = shift;
@@ -83,10 +101,10 @@ sub send_service_request {
}
warn sprintf( "Failed to submit problem %s over Open311, response\n: %s\n%s", $problem->id, $response, $self->debug_details )
- if $problem->send_fail_count && $problem->send_fail_count == 2;
+ if warn_failure($problem);
} else {
warn sprintf( "Failed to submit problem %s over Open311, details:\n%s", $problem->id, $self->error)
- if $problem->send_fail_count && $problem->send_fail_count == 2;
+ if warn_failure($problem);
}
return 0;
}
@@ -259,10 +277,10 @@ sub post_service_request_update {
}
warn sprintf( "Failed to submit comment %s over Open311, response - %s\n%s\n", $comment->id, $response, $self->debug_details )
- if $comment->send_fail_count && $comment->send_fail_count == 2;
+ if warn_failure($comment, $comment->problem);
} else {
warn sprintf( "Failed to submit comment %s over Open311, details\n%s\n", $comment->id, $self->error)
- if $comment->send_fail_count && $comment->send_fail_count == 2;
+ if warn_failure($comment, $comment->problem);
}
return 0;
}
diff --git a/t/Mock/Bing.pm b/t/Mock/Bing.pm
new file mode 100644
index 000000000..3dfb8fbe0
--- /dev/null
+++ b/t/Mock/Bing.pm
@@ -0,0 +1,37 @@
+package t::Mock::Bing;
+
+use JSON::MaybeXS;
+use Web::Simple;
+use LWP::Protocol::PSGI;
+
+has json => (
+ is => 'lazy',
+ default => sub {
+ JSON->new->pretty->allow_blessed->convert_blessed;
+ },
+);
+
+sub dispatch_request {
+ my $self = shift;
+
+ sub (GET + /REST/v1/Locations/* + ?*) {
+ my ($self, $location, $query) = @_;
+ my $data = {
+ resourceSets => [ {
+ resources => [ {
+ name => 'Constitution Hill, London, SW1A',
+ address => {
+ addressLine => 'Constitution Hill',
+ locality => 'London',
+ }
+ } ],
+ } ],
+ };
+ my $json = $self->json->encode($data);
+ return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ];
+ },
+}
+
+LWP::Protocol::PSGI->register(t::Mock::Bing->to_psgi_app, host => 'dev.virtualearth.net');
+
+__PACKAGE__->run_if_script;
diff --git a/t/Mock/MapIt.pm b/t/Mock/MapIt.pm
index 0b355983b..24e7a8084 100644
--- a/t/Mock/MapIt.pm
+++ b/t/Mock/MapIt.pm
@@ -32,6 +32,7 @@ my @PLACES = (
[ 'GU51 4AE', 51.279456, -0.846216, 2333, 'Hart District Council', 'DIS', 2227, 'Hampshire County Council', 'CTY' ],
[ 'WS1 4NH', 52.563074, -1.991032, 2535, 'Sandwell Borough Council', 'MTD' ],
[ 'OX28 4DS', 51.784721, -1.494453 ],
+ [ 'E14 2DN', 51.508536, '0.000001' ],
);
sub dispatch_request {
@@ -112,7 +113,7 @@ sub dispatch_request {
foreach (@PLACES) {
if ($point eq "4326/$_->[2],$_->[1]") {
return $self->output({
- postcode => { wgs84_lat => $_->[1], wgs84_lon => $_->[2], postcode => $_->[0] },
+ postcode => { wgs84_lat => $_->[1], wgs84_lon => $_->[2], postcode => $_->[0], distance => 93 },
});
}
}
diff --git a/t/cobrand/closest.t b/t/cobrand/closest.t
index 0d6a772ba..8bb2d649b 100644
--- a/t/cobrand/closest.t
+++ b/t/cobrand/closest.t
@@ -2,6 +2,7 @@ use strict;
use warnings;
use Test::More;
+use t::Mock::Bing;
use mySociety::Locale;
use FixMyStreet::DB;
@@ -31,7 +32,7 @@ my $dt = DateTime->new(
my $report = FixMyStreet::DB->resultset('Problem')->find_or_create(
{
- postcode => 'SW1A 1AA',
+ postcode => 'E142DN',
bodies_str => '2504',
areas => ',105255,11806,11828,2247,2504,',
category => 'Other',
@@ -47,8 +48,8 @@ my $report = FixMyStreet::DB->resultset('Problem')->find_or_create(
cobrand => 'default',
cobrand_data => '',
send_questionnaire => 't',
- latitude => '51.5016605453401',
- longitude => '-0.142497580865087',
+ latitude => 51.508536,
+ longitude => 0.000001,
user_id => $user->id,
}
);
@@ -56,21 +57,18 @@ my $report_id = $report->id;
ok $report, "created test report - $report_id";
$report->geocode( undef );
-
ok !$report->geocode, 'no geocode entry for report';
-my $near = $c->find_closest($report);
-
-SKIP: {
- if (!FixMyStreet->config('BING_MAPS_API_KEY')) {
- skip 'No Bing Maps key', 0;
- }
-
+FixMyStreet::override_config {
+ MAPIT_URL => 'http://mapit.uk/',
+ BING_MAPS_API_KEY => 'test',
+}, sub {
+ my $near = $c->find_closest($report);
ok $report->geocode, 'geocode entry added to report';
ok $report->geocode->{resourceSets}, 'geocode entry looks like right sort of thing';
like $near, qr/Constitution Hill/i, 'nearest street looks right';
- like $near, qr/Nearest postcode .*: SW1A 1AA/i, 'nearest postcode looks right';
+ like $near, qr/Nearest postcode .*: E14 2DN/i, 'nearest postcode looks right';
$near = $c->find_closest_address_for_rss($report);
@@ -81,8 +79,9 @@ SKIP: {
$near = $c->find_closest_address_for_rss($report);
ok !$near, 'no closest address for RSS if not cached';
-}
+};
-# all done
-$mech->delete_user( $user );
-done_testing();
+END {
+ $mech->delete_user( $user );
+ done_testing();
+}
diff --git a/t/open311.t b/t/open311.t
index e6ea9b6fc..06dec6a6f 100644
--- a/t/open311.t
+++ b/t/open311.t
@@ -44,7 +44,7 @@ for my $sfc (0..2) {
} );
my $expected_error = qr{Failed to submit problem 1 over Open311}ism;
- if ($sfc == 2) {
+ if ($sfc == 1) {
warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} $expected_error, 'warning generated on failed call';
} else {
warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} undef, 'no warning generated on failed call';
@@ -578,8 +578,8 @@ for my $test (
};
}
-$problem->send_fail_count(2);
-$comment->send_fail_count(2);
+$problem->send_fail_count(1);
+$comment->send_fail_count(1);
subtest 'No request id in reponse' => sub {
my $results;