cannery/test/cannery_web/controllers/user_session_controller_test.exs

101 lines
3.2 KiB
Elixir
Raw 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
conn = get(conn, Routes.user_session_path(conn, :new))
response = html_response(conn, 200)
2023-03-28 21:55:49 -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
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-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 =
post(conn, Routes.user_session_path(conn, :create), %{
2023-03-28 21:55:49 -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-17 20:56:49 -05:00
assert response =~ current_user.email
2023-03-28 21:55:49 -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 =
post(conn, Routes.user_session_path(conn, :create), %{
2023-03-28 21:55:49 -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"]
assert redirected_to(conn) =~ "/"
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")
|> post(Routes.user_session_path(conn, :create), %{
2023-03-28 21:55:49 -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
2021-03-11 21:12:55 -05:00
conn =
post(conn, Routes.user_session_path(conn, :create), %{
2023-03-28 21:55:49 -04:00
user: %{email: current_user.email, password: "bad"}
2021-03-11 21:12:55 -05:00
})
response = html_response(conn, 200)
2023-03-28 21:55:49 -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
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)
2023-03-28 21:55:49 -04:00
assert get_flash(conn, :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
conn = delete(conn, Routes.user_session_path(conn, :delete))
assert redirected_to(conn) == "/"
refute get_session(conn, :user_token)
2023-03-28 21:55:49 -04:00
assert get_flash(conn, :info) =~ "Logged out successfully"
2021-03-11 21:12:55 -05:00
end
end
end