towerops/e2e/scripts/create_test_user.exs
Graham McIntire 0bdec8653f
docs: add TOTP setup guide and test user creation script
- Add comprehensive TOTP_SETUP.md with multiple setup options
- Create scripts/create_test_user.exs for automated test user creation
- Update .gitignore to exclude Playwright browser binaries and artifacts
- Update README to reference TOTP setup guide
2026-03-06 15:06:23 -06:00

121 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("\n🎭 Creating E2E Test User...")
IO.puts("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\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
}) do
{:ok, user} ->
IO.puts("✅ 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("✅ TOTP enabled!")
# Create organization
IO.puts("🏢 Creating test organization...")
case Organizations.create_organization(%{name: "E2E Test Org"}, user.id) do
{:ok, org} ->
IO.puts("✅ Organization created: #{org.name}")
# Print credentials
IO.puts("\n" <> String.duplicate("", 40))
IO.puts("\n✨ E2E 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("❌ Failed to create organization:")
IO.inspect(changeset.errors)
end
{:error, changeset} ->
IO.puts("❌ Failed to set up TOTP:")
IO.inspect(changeset.errors)
end
{:error, changeset} ->
IO.puts("❌ Failed to create user:")
IO.inspect(changeset.errors)
end
user ->
IO.puts("⚠️ 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("\n✨ E2E 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("⚠️ 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⚠️ 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✅ User has #{length(orgs)} organization(s)")
end
end