memEx/test/memex_web/controllers/user_settings_controller_test.exs

144 lines
4.8 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserSettingsControllerTest do
2022-02-25 21:53:15 -05:00
@moduledoc """
Tests the user settings controller
"""
2021-03-11 21:12:55 -05:00
2022-07-25 19:31:54 -04:00
use MemexWeb.ConnCase, async: true
alias Memex.Accounts
2022-02-25 21:53:15 -05:00
@moduletag :user_settings_controller_test
2021-03-11 21:12:55 -05:00
setup :register_and_log_in_user
describe "GET /users/settings" do
test "renders settings page", %{conn: conn} do
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "settings"
2021-03-11 21:12:55 -05:00
end
test "redirects if user is not logged in" do
conn = build_conn()
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings")
assert redirected_to(conn) == ~p"/users/log_in"
2021-03-11 21:12:55 -05:00
end
end
describe "PUT /users/settings (change password form)" do
2022-02-25 21:53:15 -05:00
test "updates the user password and resets tokens",
2023-02-04 17:22:06 -05:00
%{conn: conn, current_user: current_user} do
2021-03-11 21:12:55 -05:00
new_password_conn =
2023-04-13 23:29:29 -04:00
put(conn, ~p"/users/settings", %{
2023-03-22 22:08:37 -04:00
action: "update_password",
current_password: valid_user_password(),
user: %{
password: "new valid password",
password_confirmation: "new valid password"
2021-03-11 21:12:55 -05:00
}
})
2023-04-13 23:29:29 -04:00
assert redirected_to(new_password_conn) == ~p"/users/settings"
2021-03-11 21:12:55 -05:00
assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
2023-04-13 23:29:29 -04:00
new_password_conn.assigns.flash["info"] =~ "password updated successfully"
2022-02-25 21:53:15 -05:00
2023-02-04 17:22:06 -05:00
assert Accounts.get_user_by_email_and_password(current_user.email, "new valid password")
2021-03-11 21:12:55 -05:00
end
test "does not update password on invalid data", %{conn: conn} do
old_password_conn =
2023-04-13 23:29:29 -04:00
put(conn, ~p"/users/settings", %{
2023-03-22 22:08:37 -04:00
action: "update_password",
current_password: "invalid",
user: %{
password: "too short",
password_confirmation: "does not match"
2021-03-11 21:12:55 -05:00
}
})
response = html_response(old_password_conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "settings"
assert response =~ "should be at least 12 character(s)"
assert response =~ "does not match password"
assert response =~ "is not valid"
2021-03-11 21:12:55 -05:00
assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)
end
end
describe "PUT /users/settings (change email form)" do
@tag :capture_log
2023-02-04 17:22:06 -05:00
test "updates the user email", %{conn: conn, current_user: current_user} do
2021-03-11 21:12:55 -05:00
conn =
2023-04-13 23:29:29 -04:00
put(conn, ~p"/users/settings", %{
2023-03-22 22:08:37 -04:00
action: "update_email",
current_password: valid_user_password(),
user: %{"email" => unique_user_email()}
2021-03-11 21:12:55 -05:00
})
2023-04-13 23:29:29 -04:00
assert redirected_to(conn) == ~p"/users/settings"
2022-02-25 21:53:15 -05:00
2023-04-13 23:29:29 -04:00
conn.assigns.flash["info"] =~
"a link to confirm your email change has been sent to the new address."
2022-02-25 21:53:15 -05:00
2023-02-04 17:22:06 -05:00
assert Accounts.get_user_by_email(current_user.email)
2021-03-11 21:12:55 -05:00
end
test "does not update email on invalid data", %{conn: conn} do
conn =
2023-04-13 23:29:29 -04:00
put(conn, ~p"/users/settings", %{
2021-03-11 21:12:55 -05:00
"action" => "update_email",
"current_password" => "invalid",
"user" => %{"email" => "with spaces"}
})
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "settings"
assert response =~ "must have the @ sign and no spaces"
assert response =~ "is not valid"
2021-03-11 21:12:55 -05:00
end
end
describe "GET /users/settings/confirm_email/:token" do
2023-02-04 17:22:06 -05:00
setup %{current_user: current_user} do
2021-03-11 21:12:55 -05:00
email = unique_user_email()
token =
extract_user_token(fn url ->
2022-02-25 21:53:15 -05:00
Accounts.deliver_update_email_instructions(
2023-02-04 17:22:06 -05:00
%{current_user | email: email},
current_user.email,
2022-02-25 21:53:15 -05:00
url
)
2021-03-11 21:12:55 -05:00
end)
%{token: token, email: email}
end
2022-02-25 21:53:15 -05:00
test "updates the user email once",
2023-02-04 17:22:06 -05:00
%{conn: conn, current_user: current_user, token: token, email: email} do
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["info"] =~ "email changed successfully"
2023-02-04 17:22:06 -05:00
refute Accounts.get_user_by_email(current_user.email)
2021-03-11 21:12:55 -05:00
assert Accounts.get_user_by_email(email)
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["error"] =~ "email change link is invalid or it has expired"
2021-03-11 21:12:55 -05:00
end
2023-02-04 17:22:06 -05:00
test "does not update email with invalid token", %{conn: conn, current_user: current_user} do
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings/confirm_email/#{"oops"}")
assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["error"] =~ "email change link is invalid or it has expired"
2023-02-04 17:22:06 -05:00
assert Accounts.get_user_by_email(current_user.email)
2021-03-11 21:12:55 -05:00
end
test "redirects if user is not logged in", %{token: token} do
conn = build_conn()
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
assert redirected_to(conn) == ~p"/users/log_in"
2021-03-11 21:12:55 -05:00
end
end
end