memEx/test/memex_web/controllers/user_session_controller_test.exs

101 lines
3.0 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserSessionControllerTest do
2022-02-25 21:53:15 -05:00
@moduledoc """
Tests the user session controller
"""
2022-07-25 19:31:54 -04:00
use MemexWeb.ConnCase, async: true
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
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/log_in")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "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
2023-04-13 23:29:29 -04:00
conn = conn |> log_in_user(current_user) |> get(~p"/users/log_in")
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 =
2023-04-13 23:29:29 -04:00
post(conn, ~p"/users/log_in", %{
2023-03-22 22:08:37 -04: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
2023-03-22 22:08:37 -04:00
assert response =~ "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 =
2023-04-13 23:29:29 -04:00
post(conn, ~p"/users/log_in", %{
2023-03-22 22:08:37 -04:00
user: %{
email: current_user.email,
password: valid_user_password(),
remember_me: "true"
2021-03-11 21:12:55 -05:00
}
})
2022-07-25 19:31:54 -04: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")
2023-04-13 23:29:29 -04:00
|> post(~p"/users/log_in", %{
2023-03-22 22:08:37 -04:00
user: %{
email: current_user.email,
password: valid_user_password()
2021-03-11 21:12:55 -05:00
}
})
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 =
2023-04-13 23:29:29 -04:00
post(conn, ~p"/users/log_in", %{
2023-03-22 22:08:37 -04:00
user: %{email: current_user.email, password: "bad"}
2021-03-11 21:12:55 -05:00
})
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "log in"
assert response =~ "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
2023-04-13 23:29:29 -04:00
conn = conn |> log_in_user(current_user) |> delete(~p"/users/log_out")
2021-03-11 21:12:55 -05:00
assert redirected_to(conn) == "/"
refute get_session(conn, :user_token)
2023-04-13 23:29:29 -04:00
conn.assigns.flash["info"] =~ "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
2023-04-13 23:29:29 -04:00
conn = delete(conn, ~p"/users/log_out")
2021-03-11 21:12:55 -05:00
assert redirected_to(conn) == "/"
refute get_session(conn, :user_token)
2023-04-13 23:29:29 -04:00
conn.assigns.flash["info"] =~ "logged out successfully"
2021-03-11 21:12:55 -05:00
end
end
end