diff --git a/lib/mobile_app_backend/mapbox_token_rotator.ex b/lib/mobile_app_backend/mapbox_token_rotator.ex index 531eb315..3d468f77 100644 --- a/lib/mobile_app_backend/mapbox_token_rotator.ex +++ b/lib/mobile_app_backend/mapbox_token_rotator.ex @@ -1,6 +1,14 @@ +defmodule MobileAppBackend.MapboxTokenRotator.Behaviour do + @callback get_public_token() :: String.t() +end + defmodule MobileAppBackend.MapboxTokenRotator do use GenServer + alias MobileAppBackend.MapboxTokenRotator + + @behaviour MapboxTokenRotator.Behaviour + defmodule State do defstruct [:primary_token, :username, :current_public_token, :expire_ms, :rotate_ms] end @@ -10,7 +18,7 @@ defmodule MobileAppBackend.MapboxTokenRotator do GenServer.start_link(__MODULE__, nil, name: server_name) end - @spec get_public_token(GenServer.server()) :: String.t() + @impl MapboxTokenRotator.Behaviour def get_public_token(server \\ __MODULE__) do GenServer.call(server, :get_public_token) end diff --git a/lib/mobile_app_backend_web/controllers/client_config_controller.ex b/lib/mobile_app_backend_web/controllers/client_config_controller.ex index 33efd205..d791d1fb 100644 --- a/lib/mobile_app_backend_web/controllers/client_config_controller.ex +++ b/lib/mobile_app_backend_web/controllers/client_config_controller.ex @@ -6,8 +6,11 @@ defmodule MobileAppBackendWeb.ClientConfigController do @spec config(Plug.Conn.t(), any()) :: Plug.Conn.t() def config(conn, _params) do + token_rotator_module = + Application.get_env(:mobile_app_backend, MapboxTokenRotator, MapboxTokenRotator) + client_config = %ClientConfig{ - mapbox_public_token: MapboxTokenRotator.get_public_token() + mapbox_public_token: token_rotator_module.get_public_token() } json(conn, client_config) diff --git a/test/mobile_app_backend/mapbox_token_rotator_test.exs b/test/mobile_app_backend/mapbox_token_rotator_test.exs index 0218a726..5d122051 100644 --- a/test/mobile_app_backend/mapbox_token_rotator_test.exs +++ b/test/mobile_app_backend/mapbox_token_rotator_test.exs @@ -137,6 +137,6 @@ defmodule MobileAppBackend.MapboxTokenRotatorTest do actual_interval = second_token_time - first_token_time - assert (actual_interval - refresh_interval) in -5..5 + assert (actual_interval - refresh_interval) in -50..50 end end diff --git a/test/mobile_app_backend_web/controllers/client_config_controller_test.exs b/test/mobile_app_backend_web/controllers/client_config_controller_test.exs index e785ca95..fcabb1cb 100644 --- a/test/mobile_app_backend_web/controllers/client_config_controller_test.exs +++ b/test/mobile_app_backend_web/controllers/client_config_controller_test.exs @@ -3,22 +3,24 @@ defmodule MobileAppBackendWeb.ClientControllerTest do use MobileAppBackendWeb.ConnCase import Test.Support.Helpers import Mox - alias MobileAppBackend.MapboxTokenRotator describe "GET /api/protected/config" do setup do verify_on_exit!() - reassign_env(:mobile_app_backend, MobileAppBackend.ClientConfig, - mapbox_public_token: "fake_mapbox_token" + reassign_env( + :mobile_app_backend, + MobileAppBackend.MapboxTokenRotator, + MapboxTokenRotatorMock ) - MapboxTokenRotator |> Process.whereis() |> Process.exit(:refresh_config) - :ok end test "returns config", %{conn: conn} do + MapboxTokenRotatorMock + |> expect(:get_public_token, fn -> "fake_mapbox_token" end) + conn = get(conn, "/api/protected/config") %{"mapbox_public_token" => "fake_mapbox_token"} = json_response(conn, 200) end diff --git a/test/test_helper.exs b/test/test_helper.exs index 170b17b6..116f4f5f 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,14 +1,15 @@ -Mox.defmock(AlertsStoreMock, for: MBTAV3API.Store) Mox.defmock(AlertsPubSubMock, for: MobileAppBackend.Alerts.PubSub.Behaviour) +Mox.defmock(AlertsStoreMock, for: MBTAV3API.Store) Mox.defmock(GlobalDataCacheMock, for: MobileAppBackend.GlobalDataCache) -Mox.defmock(RepositoryMock, for: MBTAV3API.Repository) -Mox.defmock(StaticInstanceMock, for: MBTAV3API.Stream.StaticInstance) +Mox.defmock(MapboxTokenRotatorMock, for: MobileAppBackend.MapboxTokenRotator.Behaviour) Mox.defmock(MobileAppBackend.HTTPMock, for: MobileAppBackend.HTTP) -Mox.defmock(StreamSubscriberMock, for: MobileAppBackend.Predictions.StreamSubscriber) Mox.defmock(PredictionsPubSubMock, for: MobileAppBackend.Predictions.PubSub.Behaviour) Mox.defmock(PredictionsStoreMock, for: MBTAV3API.Store) -Mox.defmock(VehiclesStoreMock, for: MBTAV3API.Store) +Mox.defmock(RepositoryMock, for: MBTAV3API.Repository) +Mox.defmock(StaticInstanceMock, for: MBTAV3API.Stream.StaticInstance) +Mox.defmock(StreamSubscriberMock, for: MobileAppBackend.Predictions.StreamSubscriber) Mox.defmock(VehiclesPubSubMock, for: MobileAppBackend.Vehicles.PubSub.Behaviour) +Mox.defmock(VehiclesStoreMock, for: MBTAV3API.Store) Application.put_env(:mobile_app_backend, MobileAppBackend.HTTP, MobileAppBackend.HTTPMock)