defmodule VigilWeb.UserLive.RegistrationInviteTest do
@moduledoc "Invite-only registration (the default outside tests), as the sibling apps."
# Not async: flips the global :registration_open config.
use VigilWeb.ConnCase, async: false
import Phoenix.LiveViewTest
import Vigil.AccountsFixtures
alias Vigil.Accounts
setup do
Application.put_env(:vigil, :registration_open, false)
on_exit(fn -> Application.put_env(:vigil, :registration_open, true) end)
end
test "without an invite there's no form, nor sign-up links", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/users/register")
assert has_element?(view, "#registration-gated")
refute has_element?(view, "#registration_form")
{:ok, home, _html} = live(conn, ~p"/")
refute has_element?(home, "#nav-register")
refute has_element?(home, "#landing-register")
end
test "a bad or spent invite is no invite", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/users/register?invite=nope")
assert has_element?(view, "#registration-gated")
{:ok, invite} = Accounts.create_invite(nil)
{:ok, _} = Accounts.consume_invite(invite, user_fixture())
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#registration-gated")
end
test "an unaddressed invite lets one person sign up by email", %{conn: conn} do
{:ok, invite} = Accounts.create_invite(nil)
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#registration-invited")
email = unique_user_email()
{:ok, _view, _html} =
view
|> form("#registration_form", user: %{email: email})
|> render_submit()
|> follow_redirect(conn, ~p"/users/log-in")
user = Accounts.get_user_by_email(email)
refute user.confirmed_at
refute Accounts.get_active_invite(invite.token)
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#registration-gated")
end
test "an addressed invite makes that account, confirmed, and signs in", %{conn: conn} do
{:ok, invite} = Accounts.create_invite(nil, %{email: "Sam@Example.com"})
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#invite_registration_form")
refute has_element?(view, "#registration_form")
conn = post(conn, ~p"/users/register/invite", %{"invite" => invite.token})
assert get_session(conn, :user_token)
assert Accounts.get_user_by_email("sam@example.com").confirmed_at
# Spent: a second go makes nothing.
conn = post(build_conn(), ~p"/users/register/invite", %{"invite" => invite.token})
refute get_session(conn, :user_token)
end
test "an addressed invite never signs in to an existing account", %{conn: conn} do
user = user_fixture()
{:ok, invite} = Accounts.create_invite(nil, %{email: user.email})
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#registration-has-account")
conn = post(conn, ~p"/users/register/invite", %{"invite" => invite.token})
refute get_session(conn, :user_token)
assert redirected_to(conn) == ~p"/users/log-in"
end
describe "the Invites page" do
setup :register_and_log_in_user
test "makes a link, shown once, and replaces and revokes it", %{conn: conn, user: user} do
{:ok, view, _html} = live(conn, ~p"/users/invites")
view |> form("#invite-form", invite: %{email: "pal@example.com"}) |> render_submit()
assert has_element?(view, "#shared-link-url")
[invite] = Accounts.list_invites(user)
assert invite.email == "pal@example.com"
view |> element("#new-link-sent-#{invite.id}") |> render_click()
[rotated] = Accounts.list_invites(user)
assert rotated.token_hash != invite.token_hash
view |> element("#revoke-sent-#{invite.id}") |> render_click()
refute Accounts.list_invites(user) |> hd() |> Vigil.Accounts.Invite.active?()
end
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
4.0 KiB · text
History
573fef1