test: expand UserRegistrationLive form events + normalize_consent_params

This commit is contained in:
Graham McIntire 2026-05-07 14:35:56 -05:00
parent bc462c5831
commit cc40b14afd

View file

@ -32,14 +32,75 @@ defmodule ToweropsWeb.UserRegistrationLiveTest do
end
end
# NOTE: Password breach checking tests are placeholders
# Full implementation requires Req mocking setup with Mox
# These tests verify the LiveView renders correctly
# Real HIBP integration would be tested in integration tests
describe "form events" do
test "validate event re-renders the form", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/register")
# Timezone detection integration tests removed - tested via manual verification
# The timezone flow is tested in:
# - Task 1: CaptureTimezone plug tests
# - Task 3: User.registration_changeset tests accepting timezone
# - Manual: Actual registration with Cloudflare header
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