53 lines
1.7 KiB
Elixir
53 lines
1.7 KiB
Elixir
# Seeds for E2E testing
|
|
# Creates a test user with TOTP enabled for Playwright tests
|
|
# Run with: mix run priv/repo/seeds_e2e.exs
|
|
|
|
alias Towerops.Accounts
|
|
alias Towerops.Organizations
|
|
alias Towerops.Repo
|
|
|
|
# Test user credentials (must match what's used in e2e/tests/auth.setup.ts)
|
|
test_email = "test@example.com"
|
|
test_password = "TestPassword123!"
|
|
# Fixed TOTP secret for reproducible tests (base32 encoded)
|
|
test_totp_secret = "JBSWY3DPEHPK3PXP"
|
|
|
|
# Delete existing test user if present (for idempotency)
|
|
case Repo.get_by(Towerops.Accounts.User, email: test_email) do
|
|
nil -> :ok
|
|
existing_user -> Repo.delete!(existing_user)
|
|
end
|
|
|
|
# Create test user
|
|
{:ok, user} =
|
|
Accounts.register_user(%{
|
|
email: test_email,
|
|
password: test_password,
|
|
password_confirmation: test_password,
|
|
terms_of_service_consent: true,
|
|
privacy_policy_consent: true
|
|
})
|
|
|
|
# Confirm the user's email directly (bypass token flow for seeding)
|
|
{:ok, user} =
|
|
user
|
|
|> Ecto.Changeset.change(%{confirmed_at: Towerops.Time.now()})
|
|
|> Repo.update()
|
|
|
|
# Enable TOTP for the user
|
|
{:ok, _device, _secret} =
|
|
Accounts.create_totp_device(user.id, "E2E Test Authenticator", test_totp_secret)
|
|
|
|
# Create a test organization so user doesn't get redirected to /orgs
|
|
{:ok, organization} =
|
|
Organizations.create_organization(%{name: "Test Organization"}, user.id)
|
|
|
|
IO.puts("✅ Created test user: #{test_email}")
|
|
IO.puts(" Organization: #{organization.name}")
|
|
IO.puts(" Password: #{test_password}")
|
|
IO.puts(" TOTP Secret: #{test_totp_secret}")
|
|
IO.puts("")
|
|
IO.puts("Set these in your CI environment or .env file:")
|
|
IO.puts(" TEST_USER_EMAIL=#{test_email}")
|
|
IO.puts(" TEST_USER_PASSWORD=#{test_password}")
|
|
IO.puts(" TEST_USER_TOTP_SECRET=#{test_totp_secret}")
|