cannery/test/cannery_web/controllers/user_session_controller_test.exs

97 lines
3.0 KiB
Elixir
Raw Permalink Normal View History

2021-03-11 21:12:55 -05:00
defmodule CanneryWeb.UserSessionControllerTest do
2022-02-16 21:12:04 -05:00
@moduledoc """
Tests the user session controller
"""
2021-03-11 21:12:55 -05:00
use CanneryWeb.ConnCase, async: true
2022-02-16 21:12:04 -05:00
@moduletag :user_session_controller_test
2022-02-08 23:58:54 -05:00
2022-02-17 20:56:49 -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-14 23:34:11 -04:00
conn = get(conn, ~p"/users/log_in")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-28 21:57:29 -04:00
assert response =~ "Log in"
2021-03-11 21:12:55 -05:00
end
2022-02-17 20:56:49 -05:00
test "redirects if already logged in", %{conn: conn, current_user: current_user} do
2023-04-14 23:34:11 -04:00
conn = conn |> log_in_user(current_user) |> get(~p"/users/log_in")
assert redirected_to(conn) == ~p"/"
2021-03-11 21:12:55 -05:00
end
end
describe "POST /users/log_in" do
2022-02-17 20:56:49 -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-14 23:34:11 -04:00
post(conn, ~p"/users/log_in", %{
2023-03-28 21:57:29 -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)
2023-04-14 23:34:11 -04:00
assert redirected_to(conn) =~ ~p"/"
2021-03-11 21:12:55 -05:00
# Now do a logged in request and assert on the menu
2023-04-14 23:34:11 -04:00
conn = get(conn, ~p"/")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2022-02-17 20:56:49 -05:00
assert response =~ current_user.email
2023-03-28 21:57:29 -04:00
assert response =~ "Are you sure you want to log out?"
2021-03-11 21:12:55 -05:00
end
2022-02-17 20:56:49 -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-14 23:34:11 -04:00
post(conn, ~p"/users/log_in", %{
2023-03-28 21:57:29 -04:00
user: %{
email: current_user.email,
password: valid_user_password(),
remember_me: "true"
2021-03-11 21:12:55 -05:00
}
})
assert conn.resp_cookies["_cannery_web_user_remember_me"]
2023-04-14 23:34:11 -04:00
assert redirected_to(conn) =~ ~p"/"
2021-03-11 21:12:55 -05:00
end
2022-02-17 20:56:49 -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-14 23:34:11 -04:00
|> post(~p"/users/log_in", %{
2023-03-28 21:57:29 -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-17 20:56:49 -05:00
test "emits error message with invalid credentials",
%{conn: conn, current_user: current_user} do
2023-04-14 23:34:11 -04:00
conn = post(conn, ~p"/users/log_in", %{user: %{email: current_user.email, password: "bad"}})
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-28 21:57:29 -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-17 20:56:49 -05:00
test "logs the user out", %{conn: conn, current_user: current_user} do
2023-04-14 23:34:11 -04:00
conn = conn |> log_in_user(current_user) |> delete(~p"/users/log_out")
assert redirected_to(conn) == ~p"/"
2021-03-11 21:12:55 -05:00
refute get_session(conn, :user_token)
2023-04-14 23:34:11 -04:00
assert 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-14 23:34:11 -04:00
conn = delete(conn, ~p"/users/log_out")
assert redirected_to(conn) == ~p"/"
2021-03-11 21:12:55 -05:00
refute get_session(conn, :user_token)
2023-04-14 23:34:11 -04:00
assert conn.assigns.flash["info"] =~ "Logged out successfully"
2021-03-11 21:12:55 -05:00
end
end
end