pass user settings controller test

This commit is contained in:
shibao 2022-02-16 21:20:04 -05:00
parent 954cdf185c
commit 025706e391
5 changed files with 90 additions and 26 deletions

View File

@ -4,6 +4,7 @@ defmodule CanneryWeb.UserAuthTest do
""" """
use CanneryWeb.ConnCase, async: true use CanneryWeb.ConnCase, async: true
import CanneryWeb.Gettext
alias Cannery.Accounts alias Cannery.Accounts
alias CanneryWeb.UserAuth alias CanneryWeb.UserAuth
@ -138,7 +139,7 @@ defmodule CanneryWeb.UserAuthTest do
conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([]) conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([])
assert conn.halted assert conn.halted
assert redirected_to(conn) == Routes.user_session_path(conn, :new) assert redirected_to(conn) == Routes.user_session_path(conn, :new)
assert get_flash(conn, :error) == "You must log in to access this page." assert get_flash(conn, :error) == dgettext("errors", "You must log in to access this page.")
end end
test "stores the path to redirect to on GET", %{conn: conn} do test "stores the path to redirect to on GET", %{conn: conn} do

View File

@ -31,7 +31,14 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
}) })
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ dgettext("prompts", "If your email is in our system")
assert get_flash(conn, :info) =~
dgettext(
"prompts",
"If your email is in our system and it has not been confirmed yet, " <>
"you will receive an email with instructions shortly."
)
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm" assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
end end
@ -44,8 +51,13 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
}) })
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ "If your email is in our system"
refute Repo.get_by(Accounts.UserToken, user_id: user.id) assert get_flash(conn, :info) =~
dgettext(
"prompts",
"If your email is in our system and it has not been confirmed yet, " <>
"you will receive an email with instructions shortly."
)
end end
test "does not send confirmation token if email is invalid", %{conn: conn} do test "does not send confirmation token if email is invalid", %{conn: conn} do
@ -55,7 +67,14 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
}) })
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ dgettext("prompts", "If your email is in our system")
assert get_flash(conn, :info) =~
dgettext(
"prompts",
"If your email is in our system and it has not been confirmed yet, " <>
"you will receive an email with instructions shortly."
)
assert Repo.all(Accounts.UserToken) == [] assert Repo.all(Accounts.UserToken) == []
end end
end end
@ -69,7 +88,10 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token)) conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ dgettext("prompts", "%{email} confirmed successfully", email: user.email)
assert get_flash(conn, :info) =~
dgettext("prompts", "%{email} confirmed successfully", email: user.email)
assert Accounts.get_user!(user.id).confirmed_at assert Accounts.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert Repo.all(Accounts.UserToken) == [] assert Repo.all(Accounts.UserToken) == []
@ -77,7 +99,9 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
# When not logged in # When not logged in
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token)) conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :error) =~ dgettext("errors", "User confirmation link is invalid or it has expired")
assert get_flash(conn, :error) =~
dgettext("errors", "User confirmation link is invalid or it has expired")
# When logged in # When logged in
conn = conn =
@ -92,7 +116,10 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
test "does not confirm email with invalid token", %{conn: conn, user: user} do test "does not confirm email with invalid token", %{conn: conn, user: user} do
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, "oops")) conn = get(conn, Routes.user_confirmation_path(conn, :confirm, "oops"))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :error) =~ dgettext("errors", "User confirmation link is invalid or it has expired")
assert get_flash(conn, :error) =~
dgettext("errors", "User confirmation link is invalid or it has expired")
refute Accounts.get_user!(user.id).confirmed_at refute Accounts.get_user!(user.id).confirmed_at
end end
end end

View File

@ -32,7 +32,10 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
"user" => valid_user_attributes(email: email) "user" => valid_user_attributes(email: email)
}) })
assert get_session(conn, :phoenix_flash) == %{"info" => dgettext("prompts", "Please check your email to verify your account")} assert get_session(conn, :phoenix_flash) == %{
"info" => dgettext("prompts", "Please check your email to verify your account")
}
assert redirected_to(conn) =~ "/" assert redirected_to(conn) =~ "/"
# Now do a logged in request and assert on the menu # Now do a logged in request and assert on the menu

View File

@ -30,7 +30,14 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
}) })
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ dgettext("prompts", "If your email is in our system")
assert get_flash(conn, :info) =~
dgettext(
"prompts",
"If your email is in our system and it has not been confirmed yet, " <>
"you will receive an email with instructions shortly."
)
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password" assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password"
end end
@ -41,7 +48,14 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
}) })
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ dgettext("prompts", "If your email is in our system")
assert get_flash(conn, :info) =~
dgettext(
"prompts",
"If your email is in our system and it has not been confirmed yet, " <>
"you will receive an email with instructions shortly."
)
assert Repo.all(Accounts.UserToken) == [] assert Repo.all(Accounts.UserToken) == []
end end
end end

View File

@ -1,8 +1,13 @@
defmodule CanneryWeb.UserSettingsControllerTest do defmodule CanneryWeb.UserSettingsControllerTest do
use CanneryWeb.ConnCase, async: true @moduledoc """
Tests the user settings controller
"""
use CanneryWeb.ConnCase, async: true
import CanneryWeb.Gettext
alias Cannery.Accounts alias Cannery.Accounts
import Cannery.AccountsFixtures
@moduletag :user_settings_controller_test
setup :register_and_log_in_user setup :register_and_log_in_user
@ -10,7 +15,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
test "renders settings page", %{conn: conn} do test "renders settings page", %{conn: conn} do
conn = get(conn, Routes.user_settings_path(conn, :edit)) conn = get(conn, Routes.user_settings_path(conn, :edit))
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "<h1>Settings</h1>" assert response =~ gettext("Settings")
end end
test "redirects if user is not logged in" do test "redirects if user is not logged in" do
@ -34,7 +39,10 @@ defmodule CanneryWeb.UserSettingsControllerTest do
assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit) assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit)
assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token) assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
assert get_flash(new_password_conn, :info) =~ "Password updated successfully"
assert get_flash(new_password_conn, :info) =~
dgettext("actions", "Password updated successfully")
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end end
@ -50,10 +58,10 @@ defmodule CanneryWeb.UserSettingsControllerTest do
}) })
response = html_response(old_password_conn, 200) response = html_response(old_password_conn, 200)
assert response =~ "<h1>Settings</h1>" assert response =~ gettext("Settings")
assert response =~ "should be at least 12 character(s)" assert response =~ dgettext("errors", "should be at least 12 character(s)")
assert response =~ "does not match password" assert response =~ dgettext("errors", "does not match password")
assert response =~ "is not valid" assert response =~ dgettext("errors", "is not valid")
assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token) assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)
end end
@ -70,7 +78,13 @@ defmodule CanneryWeb.UserSettingsControllerTest do
}) })
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
assert get_flash(conn, :info) =~ "A link to confirm your email"
assert get_flash(conn, :info) =~
dgettext(
"prompts",
"A link to confirm your email change has been sent to the new address."
)
assert Accounts.get_user_by_email(user.email) assert Accounts.get_user_by_email(user.email)
end end
@ -83,9 +97,9 @@ defmodule CanneryWeb.UserSettingsControllerTest do
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "<h1>Settings</h1>" assert response =~ gettext("Settings")
assert response =~ "must have the @ sign and no spaces" assert response =~ dgettext("errors", "must have the @ sign and no spaces")
assert response =~ "is not valid" assert response =~ dgettext("errors", "is not valid")
end end
end end
@ -104,19 +118,24 @@ defmodule CanneryWeb.UserSettingsControllerTest do
test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token)) conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
assert get_flash(conn, :info) =~ "Email changed successfully" assert get_flash(conn, :info) =~ dgettext("prompts", "Email changed successfully")
refute Accounts.get_user_by_email(user.email) refute Accounts.get_user_by_email(user.email)
assert Accounts.get_user_by_email(email) assert Accounts.get_user_by_email(email)
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token)) conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
assert get_flash(conn, :error) =~
dgettext("errors", "Email change link is invalid or it has expired")
end end
test "does not update email with invalid token", %{conn: conn, user: user} do test "does not update email with invalid token", %{conn: conn, user: user} do
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, "oops")) conn = get(conn, Routes.user_settings_path(conn, :confirm_email, "oops"))
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
assert get_flash(conn, :error) =~
dgettext("errors", "Email change link is invalid or it has expired")
assert Accounts.get_user_by_email(user.email) assert Accounts.get_user_by_email(user.email)
end end