2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserLive.ResetPasswordTest do
|
|
|
|
use Shift73kWeb.ConnCase
|
2021-03-02 10:35:36 -05:00
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
2021-03-05 19:23:32 -05:00
|
|
|
import Shift73k.AccountsFixtures
|
2021-03-02 10:35:36 -05:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Repo
|
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Accounts.{User, UserToken}
|
2021-03-02 10:35:36 -05:00
|
|
|
|
|
|
|
setup %{conn: conn} do
|
|
|
|
user = user_fixture()
|
|
|
|
conn = init_test_session(conn, %{"user_id" => user.id})
|
|
|
|
%{conn: conn, user: user}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "displays registration form", %{conn: conn, user: user} do
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, _view, html} = live_isolated(conn, Shift73kWeb.UserLive.ResetPassword)
|
2021-03-02 10:35:36 -05:00
|
|
|
|
2021-03-03 08:15:14 -05:00
|
|
|
assert html =~ "Reset password\n </h2>"
|
2021-03-02 10:35:36 -05:00
|
|
|
assert html =~ user.email
|
|
|
|
assert html =~ "New password</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.ResetPassword)
|
2021-03-02 10:35:36 -05:00
|
|
|
|
|
|
|
form_data = %{"user" => %{"password" => "abc", "password_confirmation" => "def"}}
|
|
|
|
html = form(view, "#pw_reset_form", form_data) |> render_change()
|
|
|
|
|
2021-03-03 08:15:14 -05:00
|
|
|
assert html =~ "Reset password\n </h2>"
|
2021-03-02 10:35:36 -05:00
|
|
|
assert html =~ "should be at least #{User.min_password()} character(s)"
|
|
|
|
assert html =~ "does not match password"
|
|
|
|
assert html =~ "type=\"submit\" disabled=\"disabled\""
|
|
|
|
end
|
|
|
|
|
|
|
|
@tag :capture_log
|
|
|
|
test "saves new password once", %{conn: conn, user: user} do
|
2021-03-05 19:23:32 -05:00
|
|
|
{:ok, view, _html} = live_isolated(conn, Shift73kWeb.UserLive.ResetPassword)
|
2021-03-02 10:35:36 -05:00
|
|
|
|
|
|
|
# Render submitting a new password
|
2021-04-10 12:23:11 -04:00
|
|
|
new_pw = "Valid_new_pass_123"
|
2021-03-02 10:35:36 -05:00
|
|
|
form_data = %{"user" => %{"password" => new_pw, "password_confirmation" => new_pw}}
|
|
|
|
_html = form(view, "#pw_reset_form", form_data) |> render_submit()
|
|
|
|
|
|
|
|
# Confirm redirected
|
|
|
|
flash = assert_redirected(view, Routes.user_session_path(conn, :new))
|
2021-03-02 16:48:00 -05:00
|
|
|
assert flash["info"] == "Password reset successfully."
|
2021-03-02 10:35:36 -05:00
|
|
|
|
|
|
|
# Confirm password was updated
|
|
|
|
assert Accounts.get_user_by_email_and_password(user.email, new_pw)
|
|
|
|
|
|
|
|
# Tokens have been deleted
|
|
|
|
assert [] == Repo.all(UserToken.user_and_contexts_query(user, :all))
|
|
|
|
end
|
|
|
|
end
|