pass user session controller test

This commit is contained in:
shibao 2022-02-17 20:56:49 -05:00
parent 14b677996d
commit af3d76de1c

View File

@ -8,8 +8,8 @@ defmodule CanneryWeb.UserSessionControllerTest do
@moduletag :user_session_controller_test @moduletag :user_session_controller_test
setup do setup %{conn: conn} do
%{user: user_fixture()} [current_user: user_fixture() |> confirm_user(), conn: conn]
end end
describe "GET /users/log_in" do describe "GET /users/log_in" do
@ -19,17 +19,17 @@ defmodule CanneryWeb.UserSessionControllerTest do
assert response =~ dgettext("actions", "Log in") assert response =~ dgettext("actions", "Log in")
end end
test "redirects if already logged in", %{conn: conn, user: user} do test "redirects if already logged in", %{conn: conn, current_user: current_user} do
conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new)) conn = conn |> log_in_user(current_user) |> get(Routes.user_session_path(conn, :new))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
end end
end end
describe "POST /users/log_in" do describe "POST /users/log_in" do
test "logs the user in", %{conn: conn, user: user} do test "logs the user in", %{conn: conn, current_user: current_user} do
conn = conn =
post(conn, Routes.user_session_path(conn, :create), %{ post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{"email" => user.email, "password" => valid_user_password()} "user" => %{"email" => current_user.email, "password" => valid_user_password()}
}) })
assert get_session(conn, :user_token) assert get_session(conn, :user_token)
@ -38,15 +38,15 @@ defmodule CanneryWeb.UserSessionControllerTest do
# Now do a logged in request and assert on the menu # Now do a logged in request and assert on the menu
conn = get(conn, "/") conn = get(conn, "/")
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ user.email assert response =~ current_user.email
assert response =~ dgettext("prompts", "Are you sure you want to log out?") assert response =~ dgettext("prompts", "Are you sure you want to log out?")
end end
test "logs the user in with remember me", %{conn: conn, user: user} do test "logs the user in with remember me", %{conn: conn, current_user: current_user} do
conn = conn =
post(conn, Routes.user_session_path(conn, :create), %{ post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{ "user" => %{
"email" => user.email, "email" => current_user.email,
"password" => valid_user_password(), "password" => valid_user_password(),
"remember_me" => "true" "remember_me" => "true"
} }
@ -56,13 +56,13 @@ defmodule CanneryWeb.UserSessionControllerTest do
assert redirected_to(conn) =~ "/" assert redirected_to(conn) =~ "/"
end end
test "logs the user in with return to", %{conn: conn, user: user} do test "logs the user in with return to", %{conn: conn, current_user: current_user} do
conn = conn =
conn conn
|> init_test_session(user_return_to: "/foo/bar") |> init_test_session(user_return_to: "/foo/bar")
|> post(Routes.user_session_path(conn, :create), %{ |> post(Routes.user_session_path(conn, :create), %{
"user" => %{ "user" => %{
"email" => user.email, "email" => current_user.email,
"password" => valid_user_password() "password" => valid_user_password()
} }
}) })
@ -70,10 +70,11 @@ defmodule CanneryWeb.UserSessionControllerTest do
assert redirected_to(conn) == "/foo/bar" assert redirected_to(conn) == "/foo/bar"
end end
test "emits error message with invalid credentials", %{conn: conn, user: user} do test "emits error message with invalid credentials",
%{conn: conn, current_user: current_user} do
conn = conn =
post(conn, Routes.user_session_path(conn, :create), %{ post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{"email" => user.email, "password" => "invalid_password"} "user" => %{"email" => current_user.email, "password" => "bad"}
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
@ -83,8 +84,8 @@ defmodule CanneryWeb.UserSessionControllerTest do
end end
describe "DELETE /users/log_out" do describe "DELETE /users/log_out" do
test "logs the user out", %{conn: conn, user: user} do test "logs the user out", %{conn: conn, current_user: current_user} do
conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete)) conn = conn |> log_in_user(current_user) |> delete(Routes.user_session_path(conn, :delete))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert get_flash(conn, :info) =~ gettext("Logged out successfully") assert get_flash(conn, :info) =~ gettext("Logged out successfully")