2019-03-12 11:52:28 +01:00
|
|
|
defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
|
2019-12-20 13:04:34 +01:00
|
|
|
use Mobilizon.DataCase
|
2019-03-12 11:52:28 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
import Mox
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Addresses.Address
|
|
|
|
alias Mobilizon.Service.Geospatial.GoogleMaps
|
2020-08-30 23:29:56 +02:00
|
|
|
alias Mobilizon.Service.HTTP.GeospatialClient.Mock
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
describe "search address" do
|
|
|
|
test "without API Key triggers an error" do
|
|
|
|
assert_raise ArgumentError, "API Key required to use Google Maps", fn ->
|
|
|
|
GoogleMaps.search("10 Rue Jangot")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "triggers an error with an invalid API Key" do
|
2020-07-09 17:24:28 +02:00
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/geospatial/google_maps/api_key_invalid.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url:
|
|
|
|
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=secret_key&language=en&address=10%20rue%20Jangot"
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: data}}
|
|
|
|
end)
|
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
assert_raise ArgumentError, "The provided API key is invalid.", fn ->
|
|
|
|
GoogleMaps.search("10 rue Jangot", api_key: "secret_key")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns a valid address from search" do
|
2020-07-09 17:24:28 +02:00
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/geospatial/google_maps/search.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
data_2 =
|
|
|
|
File.read!("test/fixtures/geospatial/google_maps/search_2.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, 3, fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url:
|
|
|
|
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=toto&language=en&address=10%20rue%20Jangot"
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: data}}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: _url
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: data_2}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert %Address{
|
|
|
|
locality: "Lyon",
|
|
|
|
description: "10 Rue Jangot",
|
|
|
|
region: "Auvergne-Rhône-Alpes",
|
|
|
|
country: "France",
|
|
|
|
postal_code: "69007",
|
|
|
|
street: "10 Rue Jangot",
|
2021-10-12 08:49:12 +02:00
|
|
|
timezone: "Europe/Paris",
|
2020-07-09 17:24:28 +02:00
|
|
|
geom: %Geo.Point{
|
|
|
|
coordinates: {4.8424032, 45.75164940000001},
|
|
|
|
properties: %{},
|
|
|
|
srid: 4326
|
|
|
|
},
|
|
|
|
origin_id: "gm:ChIJtW0QikTq9EcRLI4Vy6bRx0U"
|
|
|
|
} ==
|
|
|
|
GoogleMaps.search("10 rue Jangot",
|
|
|
|
api_key: "toto"
|
|
|
|
)
|
|
|
|
|> hd
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns a valid address from reverse geocode" do
|
2020-07-09 17:24:28 +02:00
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/geospatial/google_maps/geocode.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
data_2 =
|
|
|
|
File.read!("test/fixtures/geospatial/google_maps/geocode_2.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, 3, fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url:
|
|
|
|
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=toto&language=en&latlng=45.751718,4.842569&result_type=street_address"
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: data}}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: _url
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: data_2}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert %Address{
|
|
|
|
locality: "Lyon",
|
|
|
|
description: "10bis Rue Jangot",
|
|
|
|
region: "Auvergne-Rhône-Alpes",
|
|
|
|
country: "France",
|
|
|
|
postal_code: "69007",
|
|
|
|
street: "10bis Rue Jangot",
|
2021-10-12 08:49:12 +02:00
|
|
|
timezone: "Europe/Paris",
|
2020-07-09 17:24:28 +02:00
|
|
|
geom: %Geo.Point{
|
|
|
|
coordinates: {4.8424966, 45.751725},
|
|
|
|
properties: %{},
|
|
|
|
srid: 4326
|
|
|
|
},
|
|
|
|
origin_id: "gm:ChIJrW0QikTq9EcR96jk2OnO75w"
|
|
|
|
} ==
|
|
|
|
GoogleMaps.geocode(4.842569, 45.751718, api_key: "toto")
|
|
|
|
|> hd
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|