- Remove emoji from create_test_user.exs output - Remove emoji from setup.sh - Keep emoji in test files (used for matching actual app emoji)
123 lines
3.7 KiB
Elixir
123 lines
3.7 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_user_credential(user, %{
|
|
type: :totp,
|
|
totp_secret: totp_secret,
|
|
enabled: true
|
|
}) do
|
|
{:ok, _credential} ->
|
|
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 credential
|
|
credential = Repo.get_by(Accounts.UserCredential, user_id: user.id, type: :totp)
|
|
|
|
if credential 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=#{credential.totp_secret}")
|
|
IO.puts("\n" <> String.duplicate("=", 40) <> "\n")
|
|
else
|
|
IO.puts("[WARN] No TOTP credential 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_user_credential(user, %{
|
|
type: :totp,
|
|
totp_secret: "#{totp_secret}",
|
|
enabled: true
|
|
})
|
|
""")
|
|
end
|
|
|
|
# Check for organizations
|
|
orgs =
|
|
user.id
|
|
|> Organizations.list_user_organizations()
|
|
|> Enum.filter(&(&1.role == :owner))
|
|
|
|
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
|