From cc40b14afddea9c3ae6a21be3acd387941c35bd8 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 7 May 2026 14:35:56 -0500 Subject: [PATCH] test: expand UserRegistrationLive form events + normalize_consent_params --- .../live/user_registration_live_test.exs | 79 ++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/test/towerops_web/live/user_registration_live_test.exs b/test/towerops_web/live/user_registration_live_test.exs index e5947d6e..c29f870c 100644 --- a/test/towerops_web/live/user_registration_live_test.exs +++ b/test/towerops_web/live/user_registration_live_test.exs @@ -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