towerops/test/towerops_web/controllers/user_registration_controller_test.exs
2026-01-28 16:16:59 -06:00

84 lines
2.7 KiB
Elixir

defmodule ToweropsWeb.UserRegistrationControllerTest do
use ToweropsWeb.ConnCase, async: true
import Towerops.AccountsFixtures
describe "GET /users/register" do
test "renders registration page", %{conn: conn} do
conn = get(conn, ~p"/users/register")
response = html_response(conn, 200)
assert response =~ "Register"
assert response =~ ~p"/users/log-in"
assert response =~ ~p"/users/register"
end
test "redirects if already logged in", %{conn: conn} do
# user_fixture now creates users with TOTP enabled by default
conn = conn |> log_in_user(user_fixture()) |> get(~p"/users/register")
assert redirected_to(conn) == ~p"/orgs"
end
end
describe "POST /users/register" do
@tag :capture_log
test "creates account and logs in", %{conn: conn} do
email = unique_user_email()
conn =
post(conn, ~p"/users/register", %{
"user" => valid_user_attributes(email: email)
})
assert get_session(conn, :user_token)
# New users redirected to TOTP enrollment (TOTP is mandatory)
assert redirected_to(conn) == ~p"/account/totp-enrollment"
assert conn.assigns.flash["info"] =~ "Account created successfully"
end
test "render errors for invalid data", %{conn: conn} do
conn =
post(conn, ~p"/users/register", %{
"user" => %{"email" => "with spaces", "password" => "short"}
})
response = html_response(conn, 200)
assert response =~ "Register"
assert response =~ "must have the @ sign and no spaces"
assert response =~ "should be at least 12 character"
end
test "render errors when password is missing", %{conn: conn} do
conn =
post(conn, ~p"/users/register", %{
"user" => %{"email" => unique_user_email(), "password" => ""}
})
response = html_response(conn, 200)
assert response =~ "Register"
assert response =~ "can't be blank"
end
test "creates organization with provided name", %{conn: conn} do
email = unique_user_email()
conn =
post(conn, ~p"/users/register", %{
"user" => %{
"email" => email,
"password" => valid_user_password(),
"organization_name" => "Acme Corp"
}
})
assert get_session(conn, :user_token)
# User redirected to TOTP enrollment (TOTP is mandatory)
assert redirected_to(conn) == ~p"/account/totp-enrollment"
# Verify organization was created with the provided name
user = Towerops.Accounts.get_user_by_email(email)
[org | _] = Towerops.Organizations.list_user_organizations(user.id)
assert org.name == "Acme Corp"
end
end
end