# 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