106 lines
3.1 KiB
Elixir
106 lines
3.1 KiB
Elixir
defmodule ToweropsWeb.UserRegistrationLiveTest do
|
|
use ToweropsWeb.ConnCase, async: true
|
|
|
|
import Phoenix.LiveViewTest
|
|
import Towerops.AccountsFixtures
|
|
import Towerops.OrganizationsFixtures
|
|
|
|
describe "registration page" do
|
|
test "renders registration page", %{conn: conn} do
|
|
{:ok, _view, html} = live(conn, ~p"/users/register")
|
|
|
|
assert html =~ "Register for an account"
|
|
assert html =~ "Create an account"
|
|
end
|
|
|
|
test "renders invitation-based registration page", %{conn: conn} do
|
|
user = user_fixture()
|
|
organization = organization_fixture(user.id)
|
|
|
|
{:ok, invitation} =
|
|
Towerops.Organizations.create_invitation(%{
|
|
organization_id: organization.id,
|
|
invited_by_id: user.id,
|
|
email: "invited@example.com",
|
|
role: "member"
|
|
})
|
|
|
|
{:ok, _view, html} = live(conn, ~p"/users/register?token=#{invitation.token}")
|
|
|
|
assert html =~ "Join #{organization.name}"
|
|
assert html =~ "Accept invitation and create account"
|
|
end
|
|
end
|
|
|
|
describe "form events" do
|
|
test "validate event re-renders the form", %{conn: conn} do
|
|
{:ok, lv, _html} = live(conn, ~p"/users/register")
|
|
|
|
html =
|
|
lv
|
|
|> form("form",
|
|
user: %{
|
|
"email" => "test@example.com",
|
|
"password" => "weak",
|
|
"privacy_policy_consent" => "on",
|
|
"terms_of_service_consent" => "on"
|
|
}
|
|
)
|
|
|> render_change()
|
|
|
|
assert html =~ "Register for an account"
|
|
end
|
|
|
|
test "save with valid params triggers form action", %{conn: conn} do
|
|
{:ok, lv, _html} = live(conn, ~p"/users/register")
|
|
|
|
html =
|
|
lv
|
|
|> form("form",
|
|
user: %{
|
|
"email" => "newuser-#{System.unique_integer([:positive])}@example.com",
|
|
"password" => "strong-password-12345!",
|
|
"privacy_policy_consent" => "on",
|
|
"terms_of_service_consent" => "on"
|
|
}
|
|
)
|
|
|> render_submit()
|
|
|
|
# Either trigger_action true (success) or re-renders with errors
|
|
assert is_binary(html)
|
|
end
|
|
|
|
test "check_password_breach with empty payload is a no-op", %{conn: conn} do
|
|
{:ok, lv, _html} = live(conn, ~p"/users/register")
|
|
assert render_hook(lv, "check_password_breach", %{})
|
|
end
|
|
end
|
|
|
|
describe "normalize_consent_params/1" do
|
|
alias ToweropsWeb.UserRegistrationLive
|
|
|
|
test "converts 'on' to true" do
|
|
params = %{
|
|
"privacy_policy_consent" => "on",
|
|
"terms_of_service_consent" => "on",
|
|
"email" => "x@y.z"
|
|
}
|
|
|
|
result = UserRegistrationLive.normalize_consent_params(params)
|
|
assert result["privacy_policy_consent"] == true
|
|
assert result["terms_of_service_consent"] == true
|
|
assert result["email"] == "x@y.z"
|
|
end
|
|
|
|
test "leaves other values unchanged" do
|
|
params = %{
|
|
"privacy_policy_consent" => "off",
|
|
"terms_of_service_consent" => nil
|
|
}
|
|
|
|
result = UserRegistrationLive.normalize_consent_params(params)
|
|
assert result["privacy_policy_consent"] == "off"
|
|
assert result["terms_of_service_consent"] == nil
|
|
end
|
|
end
|
|
end
|