towerops/e2e/scripts/create_test_user.exs
Graham McIntire 659490c405
fix: access role from membership association in create_test_user script
Organizations have memberships with roles, not direct role attribute
2026-03-06 15:47:34 -06:00

117 lines
3.6 KiB
Elixir

# Script to create an e2e test user with TOTP enabled
#
# Usage:
# mix run e2e/scripts/create_test_user.exs
alias Towerops.Accounts
alias Towerops.Organizations
alias Towerops.Repo
email = "e2e-test@towerops.local"
password = "TestPassword123!"
totp_secret = "JBSWY3DPEHPK3PXP"
IO.puts("\nCreating E2E Test User...")
IO.puts(String.duplicate("=", 40) <> "\n")
# Check if user already exists
case Accounts.get_user_by_email(email) do
nil ->
# Create new user
IO.puts("Registering user: #{email}")
case Accounts.register_user(%{
email: email,
password: password,
password_confirmation: password,
terms_of_service_consent: true,
privacy_policy_consent: true
}) do
{:ok, user} ->
IO.puts("[OK] User created successfully!")
# Set up TOTP
IO.puts("Setting up TOTP authentication...")
case Accounts.create_totp_device(user.id, "E2E Test Device", totp_secret) do
{:ok, _device, _secret} ->
IO.puts("[OK] TOTP enabled!")
# Create organization
IO.puts("Creating test organization...")
case Organizations.create_organization(%{name: "E2E Test Org"}, user.id) do
{:ok, org} ->
IO.puts("[OK] Organization created: #{org.name}")
# Print credentials
IO.puts("\n" <> String.duplicate("=", 40))
IO.puts("\nE2E Test User Ready!")
IO.puts("\nAdd these to e2e/.env:\n")
IO.puts("TEST_USER_EMAIL=#{email}")
IO.puts("TEST_USER_PASSWORD=#{password}")
IO.puts("TEST_USER_TOTP_SECRET=#{totp_secret}")
IO.puts("\n" <> String.duplicate("=", 40) <> "\n")
{:error, changeset} ->
IO.puts("[ERROR] Failed to create organization:")
IO.inspect(changeset.errors)
end
{:error, changeset} ->
IO.puts("[ERROR] Failed to set up TOTP:")
IO.inspect(changeset.errors)
end
{:error, changeset} ->
IO.puts("[ERROR] Failed to create user:")
IO.inspect(changeset.errors)
end
user ->
IO.puts("[WARN] User already exists: #{email}")
IO.puts("\nExisting user details:")
# Get TOTP device
device = Repo.get_by(Accounts.UserTotpDevice, user_id: user.id)
if device do
IO.puts("\n" <> String.duplicate("=", 40))
IO.puts("\nE2E Test User Credentials:")
IO.puts("\nAdd these to e2e/.env:\n")
IO.puts("TEST_USER_EMAIL=#{email}")
IO.puts("TEST_USER_PASSWORD=#{password}")
IO.puts("TEST_USER_TOTP_SECRET=#{device.totp_secret}")
IO.puts("\n" <> String.duplicate("=", 40) <> "\n")
else
IO.puts("[WARN] No TOTP device found for this user")
IO.puts("\nTo set up TOTP manually, run:")
IO.puts("""
user = Towerops.Accounts.get_user_by_email("#{email}")
Towerops.Accounts.create_totp_device(user.id, "E2E Test Device", "#{totp_secret}")
""")
end
# Check for organizations
orgs =
user.id
|> Organizations.list_user_organizations()
|> Enum.filter(fn org ->
Enum.any?(org.memberships, &(&1.role == :owner))
end)
if Enum.empty?(orgs) do
IO.puts("\n[WARN] User has no organizations")
IO.puts("\nTo create one, run:")
IO.puts("""
user = Towerops.Accounts.get_user_by_email("#{email}")
Towerops.Organizations.create_organization(%{name: "E2E Test Org"}, user.id)
""")
else
IO.puts("\n[OK] User has #{length(orgs)} organization(s)")
end
end