reset_password styling & live form working; other updates & fixes

This commit is contained in:
Adam Piontek 2021-03-02 10:35:36 -05:00
parent 4a87fb9baa
commit 24502e2667
17 changed files with 215 additions and 113 deletions

View file

@ -13,7 +13,7 @@ defmodule Bones73kWeb.UserResetPasswordControllerTest do
test "renders the reset password page", %{conn: conn} do
conn = get(conn, Routes.user_reset_password_path(conn, :new))
response = html_response(conn, 200)
assert response =~ "<h1>Forgot your password?</h1>"
assert response =~ "Forgot your password?\n </h3>"
end
end
@ -52,9 +52,10 @@ defmodule Bones73kWeb.UserResetPasswordControllerTest do
%{token: token}
end
test "renders reset password", %{conn: conn, token: token} do
test "renders reset password with user_id in session", %{conn: conn, token: token, user: user} do
conn = get(conn, Routes.user_reset_password_path(conn, :edit, token))
assert html_response(conn, 200) =~ "<h1>Reset password</h1>"
assert get_session(conn, "user_id") == user.id
assert html_response(conn, 200) =~ "Reset password\n </h3>"
end
test "does not render reset password with invalid token", %{conn: conn} do
@ -73,41 +74,5 @@ defmodule Bones73kWeb.UserResetPasswordControllerTest do
%{token: token}
end
test "resets password once", %{conn: conn, user: user, token: token} do
conn =
put(conn, Routes.user_reset_password_path(conn, :update, token), %{
"user" => %{
"password" => "new valid password",
"password_confirmation" => "new valid password"
}
})
assert redirected_to(conn) == Routes.user_session_path(conn, :new)
refute get_session(conn, :user_token)
assert get_flash(conn, :info) =~ "Password reset successfully"
assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end
test "does not reset password on invalid data", %{conn: conn, token: token} do
conn =
put(conn, Routes.user_reset_password_path(conn, :update, token), %{
"user" => %{
"password" => "too short",
"password_confirmation" => "does not match"
}
})
response = html_response(conn, 200)
assert response =~ "<h1>Reset password</h1>"
assert response =~ "should be at least 12 character(s)"
assert response =~ "does not match password"
end
test "does not reset password with invalid token", %{conn: conn} do
conn = put(conn, Routes.user_reset_password_path(conn, :update, "oops"))
assert redirected_to(conn) == "/"
assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired"
end
end
end

View file

@ -1,8 +1,6 @@
defmodule Bones73kWeb.UserLive.RegistrationTest do
use Bones73kWeb.ConnCase
# import Plug.Conn
# import Phoenix.ConnTest
import Phoenix.LiveViewTest
import Bones73k.AccountsFixtures
@ -34,10 +32,11 @@ defmodule Bones73kWeb.UserLive.RegistrationTest do
assert html =~ "Register\n </h3>"
assert html =~ "must be a valid email address"
assert html =~ "should be at least #{User.min_password()} character(s)"
assert html =~ "type=\"submit\" disabled=\"disabled\""
end
@tag :capture_log
test "creates account and sets login params_token and phx-trigger-action", %{
test "creates account, sets login token & phx-trigger-action", %{
conn: conn,
user_return_to: user_return_to
} do

View file

@ -0,0 +1,57 @@
defmodule Bones73kWeb.UserLive.ResetPasswordTest do
use Bones73kWeb.ConnCase
import Phoenix.LiveViewTest
import Bones73k.AccountsFixtures
alias Bones73k.Repo
alias Bones73k.Accounts
alias Bones73k.Accounts.User
alias Bones73k.Accounts.UserToken
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
{:ok, _view, html} = live_isolated(conn, Bones73kWeb.UserLive.ResetPassword)
assert html =~ "Reset password\n </h3>"
assert html =~ user.email
assert html =~ "New password</label>"
end
test "render errors for invalid data", %{conn: conn} do
{:ok, view, _html} = live_isolated(conn, Bones73kWeb.UserLive.ResetPassword)
form_data = %{"user" => %{"password" => "abc", "password_confirmation" => "def"}}
html = form(view, "#pw_reset_form", form_data) |> render_change()
assert html =~ "Reset password\n </h3>"
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
{:ok, view, _html} = live_isolated(conn, Bones73kWeb.UserLive.ResetPassword)
# Render submitting a new password
new_pw = "valid_new_pass_123"
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))
assert flash["success"] == "Password reset successfully."
# 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