pass user settings controller test

This commit is contained in:
shibao 2022-02-17 20:45:42 -05:00
parent c2346a0bb8
commit a27a647cc4

View File

@ -26,7 +26,8 @@ defmodule CanneryWeb.UserSettingsControllerTest do
end end
describe "PUT /users/settings (change password form)" do describe "PUT /users/settings (change password form)" do
test "updates the user password and resets tokens", %{conn: conn, user: user} do test "updates the user password and resets tokens",
%{conn: conn, current_user: current_user} do
new_password_conn = new_password_conn =
put(conn, Routes.user_settings_path(conn, :update), %{ put(conn, Routes.user_settings_path(conn, :update), %{
"action" => "update_password", "action" => "update_password",
@ -43,7 +44,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
assert get_flash(new_password_conn, :info) =~ assert get_flash(new_password_conn, :info) =~
dgettext("actions", "Password updated successfully") 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(current_user.email, "new valid password")
end end
test "does not update password on invalid data", %{conn: conn} do test "does not update password on invalid data", %{conn: conn} do
@ -69,7 +70,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
describe "PUT /users/settings (change email form)" do describe "PUT /users/settings (change email form)" do
@tag :capture_log @tag :capture_log
test "updates the user email", %{conn: conn, user: user} do test "updates the user email", %{conn: conn, current_user: current_user} do
conn = conn =
put(conn, Routes.user_settings_path(conn, :update), %{ put(conn, Routes.user_settings_path(conn, :update), %{
"action" => "update_email", "action" => "update_email",
@ -85,7 +86,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
"A link to confirm your email change has been sent to the new address." "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(current_user.email)
end end
test "does not update email on invalid data", %{conn: conn} do test "does not update email on invalid data", %{conn: conn} do
@ -104,22 +105,27 @@ defmodule CanneryWeb.UserSettingsControllerTest do
end end
describe "GET /users/settings/confirm_email/:token" do describe "GET /users/settings/confirm_email/:token" do
setup %{user: user} do setup %{current_user: current_user} do
email = unique_user_email() email = unique_user_email()
token = token =
extract_user_token(fn url -> extract_user_token(fn url ->
Accounts.deliver_update_email_instructions(%{user | email: email}, user.email, url) Accounts.deliver_update_email_instructions(
%{current_user | email: email},
current_user.email,
url
)
end) end)
%{token: token, email: email} %{token: token, email: email}
end end
test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do test "updates the user email once",
%{conn: conn, current_user: current_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) =~ dgettext("prompts", "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(current_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))
@ -129,14 +135,14 @@ defmodule CanneryWeb.UserSettingsControllerTest do
dgettext("errors", "Email change link is invalid or it has expired") 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, current_user: current_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) =~ assert get_flash(conn, :error) =~
dgettext("errors", "Email change link is invalid or it has expired") 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(current_user.email)
end end
test "redirects if user is not logged in", %{token: token} do test "redirects if user is not logged in", %{token: token} do