Split Federation as separate context
This commit is contained in:
74
test/service/geospatial/addok_test.exs
Normal file
74
test/service/geospatial/addok_test.exs
Normal file
@@ -0,0 +1,74 @@
|
||||
defmodule Mobilizon.Service.Geospatial.AddokTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase, async: false
|
||||
|
||||
import Mock
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Addok
|
||||
alias Mobilizon.Config
|
||||
|
||||
@httpoison_headers [
|
||||
{"User-Agent",
|
||||
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
|
||||
Mix.Project.config()[:version]
|
||||
}"}
|
||||
]
|
||||
|
||||
@endpoint get_in(Application.get_env(:mobilizon, Addok), [:endpoint])
|
||||
@fake_endpoint "https://domain.tld"
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address" do
|
||||
with_mock HTTPoison, get: fn _url, _headers -> "{}" end do
|
||||
Addok.search("10 Rue Jangot")
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get("#{@endpoint}/search/?q=10%20Rue%20Jangot&limit=10", @httpoison_headers)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "produces a valid search address with options" do
|
||||
with_mock HTTPoison, get: fn _url, _headers -> "{}" end do
|
||||
Addok.search("10 Rue Jangot",
|
||||
endpoint: @fake_endpoint,
|
||||
limit: 5,
|
||||
coords: %{lat: 49, lon: 12}
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"#{@fake_endpoint}/search/?q=10%20Rue%20Jangot&limit=5&lat=49&lon=12",
|
||||
@httpoison_headers
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/addok/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.search("10 rue Jangot") |> hd
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/addok/geocode" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.geocode(4.842569, 45.751718) |> hd
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
8
test/service/geospatial/geospatial_test.exs
Normal file
8
test/service/geospatial/geospatial_test.exs
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule Mobilizon.Service.GeospatialTest do
|
||||
use Mobilizon.DataCase
|
||||
alias Mobilizon.Service.Geospatial
|
||||
|
||||
describe "get service" do
|
||||
assert Geospatial.service() === Elixir.Mobilizon.Service.Geospatial.Mock
|
||||
end
|
||||
end
|
||||
88
test/service/geospatial/google_maps_test.exs
Normal file
88
test/service/geospatial/google_maps_test.exs
Normal file
@@ -0,0 +1,88 @@
|
||||
defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase, async: false
|
||||
|
||||
import Mock
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.GoogleMaps
|
||||
|
||||
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 "produces a valid search address with options" do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url ->
|
||||
{:ok,
|
||||
%HTTPoison.Response{status_code: 200, body: "{\"status\": \"OK\", \"results\": []}"}}
|
||||
end do
|
||||
GoogleMaps.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr",
|
||||
api_key: "toto"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?limit=5&key=toto&language=fr&address=10%20Rue%20Jangot"
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "triggers an error with an invalid API Key" do
|
||||
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
|
||||
use_cassette "geospatial/google_maps/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8424032, 45.75164940000001},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "gm:ChIJtW0QikTq9EcRLI4Vy6bRx0U"
|
||||
} ==
|
||||
GoogleMaps.search("10 rue Jangot",
|
||||
api_key: "toto"
|
||||
)
|
||||
|> hd
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/google_maps/geocode" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10bis Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10bis Rue Jangot",
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
96
test/service/geospatial/map_quest_test.exs
Normal file
96
test/service/geospatial/map_quest_test.exs
Normal file
@@ -0,0 +1,96 @@
|
||||
defmodule Mobilizon.Service.Geospatial.MapQuestTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase, async: false
|
||||
|
||||
import Mock
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.MapQuest
|
||||
alias Mobilizon.Config
|
||||
|
||||
@httpoison_headers [
|
||||
{"User-Agent",
|
||||
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
|
||||
Mix.Project.config()[:version]
|
||||
}"}
|
||||
]
|
||||
|
||||
describe "search address" do
|
||||
test "without API Key triggers an error" do
|
||||
assert_raise ArgumentError, "API Key required to use MapQuest", fn ->
|
||||
MapQuest.search("10 Rue Jangot")
|
||||
end
|
||||
end
|
||||
|
||||
test "produces a valid search address with options" do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers ->
|
||||
{:ok,
|
||||
%HTTPoison.Response{
|
||||
status_code: 200,
|
||||
body: "{\"info\": {\"statuscode\": 0}, \"results\": []}"
|
||||
}}
|
||||
end do
|
||||
MapQuest.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr",
|
||||
api_key: "toto"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://open.mapquestapi.com/geocoding/v1/address?key=toto&location=10%20Rue%20Jangot&maxResults=5",
|
||||
@httpoison_headers
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "triggers an error with an invalid API Key" do
|
||||
assert_raise ArgumentError, "The AppKey submitted with this request is invalid.", fn ->
|
||||
MapQuest.search("10 rue Jangot", api_key: "secret_key")
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/map_quest/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842566, 45.751714},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.search("10 rue Jangot", api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/map_quest/geocode" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842569, 45.751718},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.geocode(4.842569, 45.751718, api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
84
test/service/geospatial/nominatim_test.exs
Normal file
84
test/service/geospatial/nominatim_test.exs
Normal file
@@ -0,0 +1,84 @@
|
||||
defmodule Mobilizon.Service.Geospatial.NominatimTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase, async: false
|
||||
|
||||
import Mock
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Nominatim
|
||||
alias Mobilizon.Config
|
||||
|
||||
@httpoison_headers [
|
||||
{"User-Agent",
|
||||
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
|
||||
Mix.Project.config()[:version]
|
||||
}"}
|
||||
]
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address with options" do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers ->
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}}
|
||||
end do
|
||||
Nominatim.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://nominatim.openstreetmap.org/search?format=geocodejson&q=10%20Rue%20Jangot&limit=5&accept-language=fr&addressdetails=1&namedetails=1",
|
||||
@httpoison_headers
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/nominatim/search" do
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] == Nominatim.search("10 rue Jangot")
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/nominatim/geocode" do
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] ==
|
||||
Nominatim.geocode(4.842569, 45.751718)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
78
test/service/geospatial/photon_test.exs
Normal file
78
test/service/geospatial/photon_test.exs
Normal file
@@ -0,0 +1,78 @@
|
||||
defmodule Mobilizon.Service.Geospatial.PhotonTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase, async: false
|
||||
|
||||
import Mock
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Photon
|
||||
alias Mobilizon.Config
|
||||
|
||||
@httpoison_headers [
|
||||
{"User-Agent",
|
||||
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
|
||||
Mix.Project.config()[:version]
|
||||
}"}
|
||||
]
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address with options" do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers ->
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: "{\"features\": []"}}
|
||||
end do
|
||||
Photon.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://photon.komoot.de/api/?q=10%20Rue%20Jangot&lang=fr&limit=5",
|
||||
@httpoison_headers
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/photon/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} == Photon.search("10 rue Jangot") |> hd
|
||||
end
|
||||
end
|
||||
|
||||
# Photon returns something quite wrong, so no need to test this right now.
|
||||
# test "returns a valid address from reverse geocode" do
|
||||
# use_cassette "geospatial/photon/geocode" do
|
||||
# assert %Address{
|
||||
# locality: "Lyon",
|
||||
# description: "",
|
||||
# region: "Auvergne-Rhône-Alpes",
|
||||
# country: "France",
|
||||
# postal_code: "69007",
|
||||
# street: "10 Rue Jangot",
|
||||
# geom: %Geo.Point{
|
||||
# coordinates: {4.8425657, 45.7517141},
|
||||
# properties: %{},
|
||||
# srid: 4326
|
||||
# }
|
||||
# } ==
|
||||
# Photon.geocode(4.8425657, 45.7517141)
|
||||
# |> hd
|
||||
# end
|
||||
# end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user