2023-02-26 00:47:51 -05:00
|
|
|
defmodule MemexWeb.UserSessionControllerTest do
|
2022-02-25 21:53:15 -05:00
|
|
|
@moduledoc """
|
|
|
|
Tests the user session controller
|
|
|
|
"""
|
|
|
|
|
2023-02-26 00:47:51 -05:00
|
|
|
use MemexWeb.ConnCase, async: true
|
|
|
|
import MemexWeb.Gettext
|
2021-03-11 21:12:55 -05:00
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
@moduletag :user_session_controller_test
|
2021-03-11 21:12:55 -05:00
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
setup %{conn: conn} do
|
|
|
|
[current_user: user_fixture() |> confirm_user(), conn: conn]
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /users/log_in" do
|
|
|
|
test "renders log in page", %{conn: conn} do
|
|
|
|
conn = get(conn, Routes.user_session_path(conn, :new))
|
|
|
|
response = html_response(conn, 200)
|
2022-02-25 21:53:15 -05:00
|
|
|
assert response =~ dgettext("actions", "Log in")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
test "redirects if already logged in", %{conn: conn, current_user: current_user} do
|
|
|
|
conn = conn |> log_in_user(current_user) |> get(Routes.user_session_path(conn, :new))
|
2021-03-11 21:12:55 -05:00
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /users/log_in" do
|
2022-02-25 21:53:15 -05:00
|
|
|
test "logs the user in", %{conn: conn, current_user: current_user} do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
2022-02-25 21:53:15 -05:00
|
|
|
"user" => %{"email" => current_user.email, "password" => valid_user_password()}
|
2021-03-11 21:12:55 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
assert get_session(conn, :user_token)
|
|
|
|
assert redirected_to(conn) =~ "/"
|
|
|
|
|
|
|
|
# Now do a logged in request and assert on the menu
|
|
|
|
conn = get(conn, "/")
|
|
|
|
response = html_response(conn, 200)
|
2022-02-25 21:53:15 -05:00
|
|
|
assert response =~ current_user.email
|
|
|
|
assert response =~ dgettext("prompts", "Are you sure you want to log out?")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
test "logs the user in with remember me", %{conn: conn, current_user: current_user} do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
|
|
|
"user" => %{
|
2022-02-25 21:53:15 -05:00
|
|
|
"email" => current_user.email,
|
2021-03-11 21:12:55 -05:00
|
|
|
"password" => valid_user_password(),
|
|
|
|
"remember_me" => "true"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-02-26 00:47:51 -05:00
|
|
|
assert conn.resp_cookies["_memex_web_user_remember_me"]
|
2021-03-11 21:12:55 -05:00
|
|
|
assert redirected_to(conn) =~ "/"
|
|
|
|
end
|
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
test "logs the user in with return to", %{conn: conn, current_user: current_user} do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> init_test_session(user_return_to: "/foo/bar")
|
|
|
|
|> post(Routes.user_session_path(conn, :create), %{
|
|
|
|
"user" => %{
|
2022-02-25 21:53:15 -05:00
|
|
|
"email" => current_user.email,
|
2021-03-11 21:12:55 -05:00
|
|
|
"password" => valid_user_password()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/foo/bar"
|
|
|
|
end
|
|
|
|
|
2022-02-25 21:53:15 -05:00
|
|
|
test "emits error message with invalid credentials",
|
|
|
|
%{conn: conn, current_user: current_user} do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
2022-02-25 21:53:15 -05:00
|
|
|
"user" => %{"email" => current_user.email, "password" => "bad"}
|
2021-03-11 21:12:55 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
response = html_response(conn, 200)
|
2022-02-25 21:53:15 -05:00
|
|
|
assert response =~ dgettext("actions", "Log in")
|
|
|
|
assert response =~ dgettext("errors", "Invalid email or password")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /users/log_out" do
|
2022-02-25 21:53:15 -05:00
|
|
|
test "logs the user out", %{conn: conn, current_user: current_user} do
|
|
|
|
conn = conn |> log_in_user(current_user) |> delete(Routes.user_session_path(conn, :delete))
|
2021-03-11 21:12:55 -05:00
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
refute get_session(conn, :user_token)
|
2022-02-25 21:53:15 -05:00
|
|
|
assert get_flash(conn, :info) =~ gettext("Logged out successfully")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "succeeds even if the user is not logged in", %{conn: conn} do
|
|
|
|
conn = delete(conn, Routes.user_session_path(conn, :delete))
|
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
refute get_session(conn, :user_token)
|
2022-02-25 21:53:15 -05:00
|
|
|
assert get_flash(conn, :info) =~ gettext("Logged out successfully")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|