Add test support for default role assignment
This commit is contained in:
parent
9557d8ae6b
commit
3b5b5044fb
2 changed files with 84 additions and 0 deletions
|
|
@ -16,6 +16,8 @@ defmodule Mv.DataCase do
|
|||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
require Ash.Query
|
||||
|
||||
using do
|
||||
quote do
|
||||
alias Mv.Repo
|
||||
|
|
@ -29,6 +31,8 @@ defmodule Mv.DataCase do
|
|||
|
||||
setup tags do
|
||||
Mv.DataCase.setup_sandbox(tags)
|
||||
# Ensure "Mitglied" role exists for AssignDefaultRole to work in tests
|
||||
Mv.DataCase.ensure_default_role()
|
||||
:ok
|
||||
end
|
||||
|
||||
|
|
@ -42,6 +46,36 @@ defmodule Mv.DataCase do
|
|||
pid
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the default "Mitglied" role exists in the test database.
|
||||
|
||||
This is necessary because AssignDefaultRole expects this role to exist.
|
||||
Tests run in sandbox mode, so the role needs to be created for each test.
|
||||
"""
|
||||
def ensure_default_role do
|
||||
# Check if "Mitglied" role already exists
|
||||
case Mv.Authorization.Role.get_mitglied_role() do
|
||||
{:ok, nil} ->
|
||||
# Create the role if it doesn't exist
|
||||
Mv.Authorization.Role
|
||||
|> Ash.Changeset.for_create(:create_role_with_system_flag, %{
|
||||
name: "Mitglied",
|
||||
description: "Default member role with access to own data only",
|
||||
permission_set_name: "own_data",
|
||||
is_system_role: true
|
||||
})
|
||||
|> Ash.create!(authorize?: false, domain: Mv.Authorization)
|
||||
|
||||
{:ok, _role} ->
|
||||
# Role already exists, do nothing
|
||||
:ok
|
||||
|
||||
{:error, _error} ->
|
||||
# Ignore errors (e.g., in tests that don't need roles)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
A helper that transforms changeset errors into a map of messages.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue