test(AshAuthentication): updated tests for signed in user and added test for authcontroller

This commit is contained in:
carla 2025-07-02 11:32:48 +02:00 committed by carla
parent c7b13c0ecb
commit fba9abc2c1
8 changed files with 116 additions and 2 deletions

View file

@ -31,6 +31,38 @@ defmodule MvWeb.ConnCase do
end
end
@doc """
Creates a test user and returns the user struct.
"""
def create_test_user(attrs \\ %{}) do
email = "user@example.com"
password = "password"
{:ok, hashed_password} = AshAuthentication.BcryptProvider.hash(password)
Ash.Seed.seed!(Mv.Accounts.User, %{
email: email,
hashed_password: hashed_password
})
end
@doc """
Signs in a user via OIDC for testing by creating a session with the user's token.
"""
def sign_in_user_via_oidc(conn, user) do
# Mock OIDC sign-in by creating a token directly
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> AshAuthentication.Plug.Helpers.store_in_session(user)
end
@doc """
Signs in a user via OIDC and returns a connection with the user authenticated.
"""
def conn_with_oidc_user(conn, user_attrs \\ %{}) do
user = create_test_user(user_attrs)
sign_in_user_via_oidc(conn, user)
end
setup tags do
Mv.DataCase.setup_sandbox(tags)
{:ok, conn: Phoenix.ConnTest.build_conn()}