Refactor test setup: use global setup and fix MembershipFees domain alias
- Remove redundant setup blocks from member_live tests - Add build_unauthenticated_conn helper for AuthController tests - Add global setup in conn_case.ex
This commit is contained in:
parent
bc87893134
commit
075a06ba6f
9 changed files with 216 additions and 81 deletions
|
|
@ -99,6 +99,7 @@ defmodule MvWeb.ConnCase do
|
|||
@doc """
|
||||
Signs in a user via OIDC and returns a connection with the user authenticated.
|
||||
By default creates a user with "user@example.com" for consistency.
|
||||
The user will have an admin role for authorization.
|
||||
"""
|
||||
def conn_with_oidc_user(conn, user_attrs \\ %{}) do
|
||||
# Ensure unique email for OIDC users
|
||||
|
|
@ -109,8 +110,22 @@ defmodule MvWeb.ConnCase do
|
|||
oidc_id: "oidc_#{unique_id}"
|
||||
}
|
||||
|
||||
# Create user using Ash.Seed (supports oidc_id)
|
||||
user = create_test_user(Map.merge(default_attrs, user_attrs))
|
||||
sign_in_user_via_oidc(conn, user)
|
||||
|
||||
# Create admin role and assign it
|
||||
admin_role = Mv.Fixtures.role_fixture("admin")
|
||||
|
||||
{:ok, user} =
|
||||
user
|
||||
|> Ash.Changeset.for_update(:update, %{})
|
||||
|> Ash.Changeset.manage_relationship(:role, admin_role, type: :append_and_remove)
|
||||
|> Ash.update()
|
||||
|
||||
# Load role for authorization
|
||||
user_with_role = Ash.load!(user, :role, domain: Mv.Accounts)
|
||||
|
||||
sign_in_user_via_oidc(conn, user_with_role)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
@ -122,6 +137,15 @@ defmodule MvWeb.ConnCase do
|
|||
|> AshAuthentication.Plug.Helpers.store_in_session(user)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a connection with an authenticated user that has an admin role.
|
||||
This is useful for tests that need full access to resources.
|
||||
"""
|
||||
def conn_with_admin_user(conn) do
|
||||
admin_user = Mv.Fixtures.user_with_role_fixture("admin")
|
||||
conn_with_password_user(conn, admin_user)
|
||||
end
|
||||
|
||||
setup tags do
|
||||
pid = Mv.DataCase.setup_sandbox(tags)
|
||||
|
||||
|
|
@ -130,6 +154,11 @@ defmodule MvWeb.ConnCase do
|
|||
# to share the test's database connection in async tests
|
||||
conn = Plug.Conn.put_private(conn, :ecto_sandbox, pid)
|
||||
|
||||
{:ok, conn: conn}
|
||||
# Create admin user with role for all tests (unless test overrides with its own user)
|
||||
# This ensures all tests have an authenticated user with proper authorization
|
||||
admin_user = Mv.Fixtures.user_with_role_fixture("admin")
|
||||
authenticated_conn = conn_with_password_user(conn, admin_user)
|
||||
|
||||
{:ok, conn: authenticated_conn, current_user: admin_user}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue