After "mix phx.gen.auth Admins Admin admins" with added working register and login path.

This commit is contained in:
2026-02-20 13:09:55 +01:00
parent a47931f40e
commit 53d19a3a18
28 changed files with 2830 additions and 0 deletions

View File

@ -76,4 +76,45 @@ defmodule BeetRoundServerWeb.ConnCase do
defp maybe_set_token_authenticated_at(token, authenticated_at) do
BeetRoundServer.AccountsFixtures.override_token_authenticated_at(token, authenticated_at)
end
@doc """
Setup helper that registers and logs in admins.
setup :register_and_log_in_admin
It stores an updated connection and a registered admin in the
test context.
"""
def register_and_log_in_admin(%{conn: conn} = context) do
admin = BeetRoundServer.AdminsFixtures.admin_fixture()
scope = BeetRoundServer.Admins.Scope.for_admin(admin)
opts =
context
|> Map.take([:token_authenticated_at])
|> Enum.into([])
%{conn: log_in_admin(conn, admin, opts), admin: admin, scope: scope}
end
@doc """
Logs the given `admin` into the `conn`.
It returns an updated `conn`.
"""
def log_in_admin(conn, admin, opts \\ []) do
token = BeetRoundServer.Admins.generate_admin_session_token(admin)
maybe_set_token_authenticated_at(token, opts[:token_authenticated_at])
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> Plug.Conn.put_session(:admin_token, token)
end
defp maybe_set_token_authenticated_at(_token, nil), do: nil
defp maybe_set_token_authenticated_at(token, authenticated_at) do
BeetRoundServer.AdminsFixtures.override_token_authenticated_at(token, authenticated_at)
end
end