2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserLive.RegistrationTest do
|
|
|
|
use Shift73kWeb.ConnCase
|
2021-03-01 13:42:26 -05:00
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
2021-03-05 19:23:32 -05:00
|
|
|
import Shift73k.AccountsFixtures
|
2021-03-01 13:42:26 -05:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Accounts.User
|
2021-03-01 13:42:26 -05:00
|
|
|
|
|
|
|
describe "Registration" do
|
|
|
|
setup %{conn: conn} do
|
|
|
|
user_return_to = "/path-requires-auth"
|
|
|
|
conn = init_test_session(conn, %{"user_return_to" => user_return_to})
|
|
|
|
%{conn: conn, user_return_to: user_return_to}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "displays registration form", %{conn: conn} do
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, _view, html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
2021-03-01 13:42:26 -05:00
|
|
|
|
2021-03-03 08:15:14 -05:00
|
|
|
assert html =~ "Register\n </h2>"
|
2021-03-01 13:42:26 -05:00
|
|
|
assert html =~ "Email</label>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render errors for invalid data", %{conn: conn} do
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, view, _html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
2021-03-01 13:42:26 -05:00
|
|
|
|
|
|
|
html =
|
|
|
|
view
|
|
|
|
|> form("#reg_form", %{"user" => %{"email" => "abc", "password" => "abc"}})
|
|
|
|
|> render_change()
|
|
|
|
|
2021-03-03 08:15:14 -05:00
|
|
|
assert html =~ "Register\n </h2>"
|
2021-03-01 13:42:26 -05:00
|
|
|
assert html =~ "must be a valid email address"
|
|
|
|
assert html =~ "should be at least #{User.min_password()} character(s)"
|
2021-03-02 10:35:36 -05:00
|
|
|
assert html =~ "type=\"submit\" disabled=\"disabled\""
|
2021-03-01 13:42:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@tag :capture_log
|
2021-03-02 10:35:36 -05:00
|
|
|
test "creates account, sets login token & phx-trigger-action", %{
|
2021-03-01 13:42:26 -05:00
|
|
|
conn: conn,
|
|
|
|
user_return_to: user_return_to
|
|
|
|
} do
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, view, html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
2021-03-01 13:42:26 -05:00
|
|
|
|
|
|
|
# Login trigger form not triggered yet
|
|
|
|
refute html =~ "phx-trigger-action=\"phx-trigger-action\""
|
|
|
|
|
|
|
|
# Render registering a new user
|
|
|
|
email = unique_user_email()
|
|
|
|
form_data = %{"user" => %{"email" => email, "password" => valid_user_password()}}
|
|
|
|
html = form(view, "#reg_form", form_data) |> render_submit()
|
|
|
|
|
|
|
|
# Confirm user was registered
|
|
|
|
%User{email: new_user_email, id: new_user_id} = Accounts.get_user_by_email(email)
|
|
|
|
assert new_user_email == email
|
|
|
|
|
|
|
|
# Login trigger form activated?
|
|
|
|
assert html =~ "phx-trigger-action=\"phx-trigger-action\""
|
|
|
|
|
|
|
|
# Collect the rendered login params token
|
|
|
|
[params_token] = Floki.attribute(html, "input#user_params_token", "value")
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, params} = Phoenix.Token.decrypt(Shift73kWeb.Endpoint, "login_params", params_token)
|
2021-03-01 13:42:26 -05:00
|
|
|
%{user_id: param_user_id, user_return_to: param_return_path} = params
|
|
|
|
|
|
|
|
# Token in login trigger form has correct user ID?
|
|
|
|
assert new_user_id == param_user_id
|
|
|
|
# ... and correct user_return_to path?
|
|
|
|
assert user_return_to == param_return_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|