Skip to content

Commit

Permalink
Extend tests fro RegisterForm
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed Jan 8, 2025
1 parent df3b970 commit ad86a09
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions test/plausible_web/live/register_form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ defmodule PlausibleWeb.Live.RegisterFormTest do
inviter = new_user()
site = new_site(owner: inviter)

invitation =
guest_invitation =
invite_guest(site, "[email protected]", role: :editor, inviter: inviter)

{:ok, %{site: site, invitation: invitation, inviter: inviter}}
{:ok, %{site: site, guest_invitation: guest_invitation, inviter: inviter}}
end

test "registers user from invitation", %{conn: conn, invitation: invitation} do
test "registers user from guest invitation", %{conn: conn, guest_invitation: guest_invitation} do
mock_captcha_success()

lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
lv = get_liveview(conn, "/register/invitation/#{guest_invitation.invitation_id}")

type_into_input(lv, "user[name]", "Mary Sue")
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
Expand Down Expand Up @@ -192,16 +192,53 @@ defmodule PlausibleWeb.Live.RegisterFormTest do
assert String.length(password_hash) > 0
end

test "registers user from team invitation", %{conn: conn, inviter: inviter} do
mock_captcha_success()

team = team_of(inviter)

team_invitation =
invite_member(team, "[email protected]", role: :editor, inviter: inviter)

lv = get_liveview(conn, "/register/invitation/#{team_invitation.invitation_id}")

type_into_input(lv, "user[name]", "Mary Sue")
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")

html = lv |> element("form") |> render_submit()

on_ee do
assert_push_event(lv, "send-metrics", %{event_name: "Signup via invitation"})
end

assert [
csrf_input,
action_input,
email_input,
name_input,
password_input,
password_confirmation_input | _
] = find(html, "input")

assert String.length(text_of_attr(csrf_input, "value")) > 0
assert text_of_attr(action_input, "value") == "register_from_invitation_form"
assert text_of_attr(name_input, "value") == "Mary Sue"
assert text_of_attr(email_input, "value") == "[email protected]"
assert text_of_attr(password_input, "value") == "very-long-and-very-secret-123"
assert text_of_attr(password_confirmation_input, "value") == "very-long-and-very-secret-123"
end

test "preserves trial_expiry_date when invitation role is :owner", %{
conn: conn,
site: site,
inviter: inviter
} do
mock_captcha_success()

invitation = invite_transfer(site, "[email protected]", inviter: inviter)
site_transfer = invite_transfer(site, "[email protected]", inviter: inviter)

lv = get_liveview(conn, "/register/invitation/#{invitation.transfer_id}")
lv = get_liveview(conn, "/register/invitation/#{site_transfer.transfer_id}")

type_into_input(lv, "user[name]", "Mary Sue")
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
Expand All @@ -214,10 +251,13 @@ defmodule PlausibleWeb.Live.RegisterFormTest do
assert team_of(user).trial_expiry_date != nil
end

test "always uses original email from the invitation", %{conn: conn, invitation: invitation} do
test "always uses original email from the invitation", %{
conn: conn,
guest_invitation: guest_invitation
} do
mock_captcha_success()

lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
lv = get_liveview(conn, "/register/invitation/#{guest_invitation.invitation_id}")

type_into_input(lv, "user[name]", "Mary Sue")
type_into_input(lv, "user[email]", "[email protected]")
Expand Down Expand Up @@ -247,10 +287,10 @@ defmodule PlausibleWeb.Live.RegisterFormTest do
assert html =~ "Your invitation has expired or been revoked"
end

test "renders error on failed captcha", %{conn: conn, invitation: invitation} do
test "renders error on failed captcha", %{conn: conn, guest_invitation: guest_invitation} do
mock_captcha_failure()

lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
lv = get_liveview(conn, "/register/invitation/#{guest_invitation.invitation_id}")

type_into_input(lv, "user[name]", "Mary Sue")
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
Expand All @@ -265,9 +305,9 @@ defmodule PlausibleWeb.Live.RegisterFormTest do

test "pushing send-metrics-after event submits the form", %{
conn: conn,
invitation: invitation
guest_invitation: guest_invitation
} do
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
lv = get_liveview(conn, "/register/invitation/#{guest_invitation.invitation_id}")

refute render(lv) =~ ~s|phx-trigger-action="phx-trigger-action"|

Expand Down

0 comments on commit ad86a09

Please sign in to comment.