2021-06-28 11:14:14 +02:00
|
|
|
defmodule PokemonCoutureWeb.UserSessionControllerTest do
|
|
|
|
use PokemonCoutureWeb.ConnCase, async: true
|
|
|
|
|
|
|
|
import PokemonCouture.AccountsFixtures
|
|
|
|
|
|
|
|
setup do
|
|
|
|
%{user: user_fixture()}
|
|
|
|
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)
|
|
|
|
assert response =~ "<h1>Log in</h1>"
|
|
|
|
assert response =~ "Log in</a>"
|
|
|
|
assert response =~ "Register</a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "redirects if already logged in", %{conn: conn, user: user} do
|
|
|
|
conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new))
|
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /users/log_in" do
|
|
|
|
test "logs the user in", %{conn: conn, user: user} do
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
2021-10-13 12:18:37 +02:00
|
|
|
"user" => %{"username" => user.username, "password" => valid_user_password()}
|
2021-06-28 11:14:14 +02: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)
|
2021-10-13 12:18:37 +02:00
|
|
|
assert response =~ user.username
|
2021-06-28 11:14:14 +02:00
|
|
|
assert response =~ "Settings</a>"
|
|
|
|
assert response =~ "Log out</a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "logs the user in with remember me", %{conn: conn, user: user} do
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
|
|
|
"user" => %{
|
2021-10-13 12:18:37 +02:00
|
|
|
"username" => user.username,
|
2021-06-28 11:14:14 +02:00
|
|
|
"password" => valid_user_password(),
|
|
|
|
"remember_me" => "true"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert conn.resp_cookies["_pokemon_couture_web_user_remember_me"]
|
|
|
|
assert redirected_to(conn) =~ "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "logs the user in with return to", %{conn: conn, user: user} do
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> init_test_session(user_return_to: "/foo/bar")
|
|
|
|
|> post(Routes.user_session_path(conn, :create), %{
|
|
|
|
"user" => %{
|
2021-10-13 12:18:37 +02:00
|
|
|
"username" => user.username,
|
2021-06-28 11:14:14 +02:00
|
|
|
"password" => valid_user_password()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/foo/bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "emits error message with invalid credentials", %{conn: conn, user: user} do
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_session_path(conn, :create), %{
|
2021-10-13 12:18:37 +02:00
|
|
|
"user" => %{"username" => user.username, "password" => "invalid_password"}
|
2021-06-28 11:14:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
response = html_response(conn, 200)
|
|
|
|
assert response =~ "<h1>Log in</h1>"
|
2021-10-13 12:18:37 +02:00
|
|
|
assert response =~ "Invalid username or password"
|
2021-06-28 11:14:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /users/log_out" do
|
|
|
|
test "logs the user out", %{conn: conn, user: user} do
|
|
|
|
conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete))
|
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
refute get_session(conn, :user_token)
|
|
|
|
assert get_flash(conn, :info) =~ "Logged out successfully"
|
|
|
|
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)
|
|
|
|
assert get_flash(conn, :info) =~ "Logged out successfully"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|