Skip to content

Commit

Permalink
fix: token rotation flaky tests (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaylaBrady authored Nov 21, 2024
1 parent f36b9d6 commit 68ee20c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
10 changes: 9 additions & 1 deletion lib/mobile_app_backend/mapbox_token_rotator.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/mobile_app_backend/mapbox_token_rotator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -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)

Expand Down

0 comments on commit 68ee20c

Please sign in to comment.