From bfbadd16e9586759ce8f6c0ad0b6e47c8d11c42e Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 12 Apr 2021 12:16:39 +0300 Subject: [PATCH] Add fake data for geolocation tests --- config/runtime.exs | 4 ++-- config/test.exs | 9 +++++++++ .../controllers/api/external_controller_test.exs | 5 +++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index c0b065c19d86..57cdfeec7766 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -56,7 +56,7 @@ cron_enabled = String.to_existing_atom(System.get_env("CRON_ENABLED", "false")) custom_domain_server_ip = System.get_env("CUSTOM_DOMAIN_SERVER_IP") custom_domain_server_user = System.get_env("CUSTOM_DOMAIN_SERVER_USER") custom_domain_server_password = System.get_env("CUSTOM_DOMAIN_SERVER_PASSWORD") -geolite2_country_db = System.get_env("GEOLITE2_COUNTRY_DB", "priv/geodb/dbip-country.mmdb") +geolite2_country_db = System.get_env("EOLITE2_COUNTRY_DB", "priv/geodb/dbip-country.mmdb") disable_auth = String.to_existing_atom(System.get_env("DISABLE_AUTH", "false")) disable_registration = String.to_existing_atom(System.get_env("DISABLE_REGISTRATION", "false")) hcaptcha_sitekey = System.get_env("HCAPTCHA_SITEKEY") @@ -243,7 +243,7 @@ config :kaffy, ] ] -if geolite2_country_db do +if config_env() != :test && geolite2_country_db do config :geolix, databases: [ %{ diff --git a/config/test.exs b/config/test.exs index 5d75db8deaf5..3d3b6d9a7de9 100644 --- a/config/test.exs +++ b/config/test.exs @@ -18,5 +18,14 @@ config :plausible, paddle_api: Plausible.PaddleApi.Mock, google_api: Plausible.Google.Api.Mock +config :geolix, + databases: [ + %{ + id: :country, + adapter: Geolix.Adapter.Fake, + data: %{{1, 1, 1, 1} => %{country: %{iso_code: "US"}}} + } + ] + config :plausible, session_timeout: 0 diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 41ec87166a5a..6cae5141eb4c 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -454,6 +454,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert event.referrer == "" end + # Fake data is set up in config/test.exs test "looks up the country from the ip address", %{conn: conn} do params = %{ name: "pageview", @@ -463,12 +464,12 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do conn |> put_req_header("content-type", "text/plain") - |> put_req_header("x-forwarded-for", "78.76.58.149") + |> put_req_header("x-forwarded-for", "1.1.1.1") |> post("/api/event", Jason.encode!(params)) pageview = get_event("external-controller-test-20.com") - assert pageview.country_code == "SE" + assert pageview.country_code == "US" end test "URL is decoded", %{conn: conn} do