diff options
Diffstat (limited to 't/Mock')
-rw-r--r-- | t/Mock/Facebook.pm | 52 | ||||
-rw-r--r-- | t/Mock/MapIt.pm | 51 | ||||
-rw-r--r-- | t/Mock/Nominatim.pm | 37 | ||||
-rw-r--r-- | t/Mock/Static.pm | 18 |
4 files changed, 158 insertions, 0 deletions
diff --git a/t/Mock/Facebook.pm b/t/Mock/Facebook.pm new file mode 100644 index 000000000..68c63f2d5 --- /dev/null +++ b/t/Mock/Facebook.pm @@ -0,0 +1,52 @@ +package t::Mock::Facebook; + +use JSON; +use Web::Simple; +use MooX::Types::MooseLike::Base qw(:all); + +has json => ( + is => 'lazy', + default => sub { + JSON->new->pretty->allow_blessed->convert_blessed; + }, +); + +has returns_email => ( + is => 'rw', + isa => Bool, + default => 1, +); + +sub dispatch_request { + my $self = shift; + + sub (GET + /v2.2/dialog/oauth + ?*) { + my ($self) = @_; + return [ 200, [ 'Content-Type' => 'text/html' ], [ 'FB login page' ] ]; + }, + + sub (GET + /v2.2/oauth/access_token + ?*) { + my ($self) = @_; + return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'access_token=access_token&expires=never' ] ]; + }, + + sub (GET + /me + ?fields=) { + my ($self, $fields) = @_; + my $data = { + id => '123456789', + name => 'Fiona Tester', + }; + $data->{email} = 'facebook@example.org' if $self->returns_email; + my $json = $self->json->encode($data); + return [ 200, [ 'Content-Type' => 'text/html' ], [ $json ] ]; + }, + + sub (GET + /search + ?q=) { + my ($self, $q) = @_; + my $response = $self->query($q); + my $json = $self->json->encode($response); + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, +} + +__PACKAGE__->run_if_script; diff --git a/t/Mock/MapIt.pm b/t/Mock/MapIt.pm new file mode 100644 index 000000000..a3bfb10d4 --- /dev/null +++ b/t/Mock/MapIt.pm @@ -0,0 +1,51 @@ +package t::Mock::MapIt; + +use JSON; +use Web::Simple; + +use mySociety::Locale; + +has json => ( + is => 'lazy', + default => sub { + JSON->new->pretty->allow_blessed->convert_blessed; + }, +); + +sub dispatch_request { + my $self = shift; + + sub (GET + /postcode/*) { + my ($self, $postcode) = @_; + my $response = $self->postcode($postcode); + # We must make sure we output correctly for testing purposes, we might + # be within a different locale here... + my $json = mySociety::Locale::in_gb_locale { + $self->json->encode($response) }; + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, + + sub (GET + /point/**) { + my ($self, $point) = @_; + my $response = { + "63999" => {"parent_area" => 2245, "generation_high" => 25, "all_names" => {}, "id" => 63999, "codes" => {"ons" => "00HYNS", "gss" => "E05008366", "unit_id" => "44025"}, "name" => "Kington", "country" => "E", "type_name" => "Unitary Authority electoral division (UTE)", "generation_low" => 12, "country_name" => "England", "type" => "UTE"}, + "65822" => {"parent_area" => undef, "generation_high" => 25, "all_names" => {}, "id" => 65822, "codes" => {"gss" => "E14000860", "unit_id" => "24903"}, "name" => "North Wiltshire", "country" => "E", "type_name" => "UK Parliament constituency", "generation_low" => 13, "country_name" => "England", "type" => "WMC"}, + "11814" => {"parent_area" => undef, "generation_high" => 25, "all_names" => {}, "id" => 11814, "codes" => {"ons" => "09", "gss" => "E15000009", "unit_id" => "41427"}, "name" => "South West", "country" => "E", "type_name" => "European region", "generation_low" => 1, "country_name" => "England", "type" => "EUR"}, + "2245" => {"parent_area" => undef, "generation_high" => 25, "all_names" => {}, "id" => 2245, "codes" => {"ons" => "00HY", "gss" => "E06000054", "unit_id" => "43925"}, "name" => "Wiltshire Council", "country" => "E", "type_name" => "Unitary Authority", "generation_low" => 11, "country_name" => "England", "type" => "UTA"} + }; + # We must make sure we output correctly for testing purposes, we might + # be within a different locale here... + my $json = mySociety::Locale::in_gb_locale { + $self->json->encode($response) }; + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, +} + +sub postcode { + my ($self, $postcode) = @_; + return { + wgs84_lat => 51.5, wgs84_lon => -2.1, postcode => $postcode, coordsyst => 'G', + }; +} + +__PACKAGE__->run_if_script; diff --git a/t/Mock/Nominatim.pm b/t/Mock/Nominatim.pm new file mode 100644 index 000000000..3f3cef69a --- /dev/null +++ b/t/Mock/Nominatim.pm @@ -0,0 +1,37 @@ +package t::Mock::Nominatim; + +use JSON; +use Web::Simple; + +has json => ( + is => 'lazy', + default => sub { + JSON->new->pretty->allow_blessed->convert_blessed; + }, +); + +sub dispatch_request { + my $self = shift; + + sub (GET + /search + ?q=) { + my ($self, $q) = @_; + my $response = $self->query($q); + my $json = $self->json->encode($response); + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, +} + +sub query { + my ($self, $q) = @_; + if ($q eq 'high street') { + return [ + {"osm_type"=>"way","osm_id"=>"4684282","lat"=>"55.9504009","lon"=>"-3.1858425","display_name"=>"High Street, Old Town, City of Edinburgh, Scotland, EH1 1SP, United Kingdom","class"=>"highway","type"=>"tertiary","importance"=>0.55892577838734}, + {"osm_type"=>"node","osm_id"=>"27424410","lat"=>"55.8596449","lon"=>"-4.240377","display_name"=>"High Street, Collegelands, Merchant City, Glasgow, Glasgow City, Scotland, G, United Kingdom","class"=>"railway","type"=>"station","importance"=>0.53074299592768} + ]; + } + return []; +} + + + +__PACKAGE__->run_if_script; 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; |