comfycamp/test/comfycamp_web/live/user_login_live_test.exs

88 lines
2.5 KiB
Elixir
Raw Normal View History

2024-07-11 01:14:16 +05:00
defmodule ComfycampWeb.UserLoginLiveTest do
use ComfycampWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Comfycamp.AccountsFixtures
describe "Log in page" do
test "renders log in page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/users/log_in")
2024-07-21 22:22:36 +05:00
assert html =~ "Войти"
assert html =~ "Зарегистрироваться"
assert html =~ "Забыли пароль?"
2024-07-11 01:14:16 +05:00
end
test "redirects if already logged in", %{conn: conn} do
result =
conn
|> log_in_user(user_fixture())
|> live(~p"/users/log_in")
|> follow_redirect(conn, "/")
assert {:ok, _conn} = result
end
end
describe "user login" do
test "redirects if user login with valid credentials", %{conn: conn} do
password = "123456789abcd"
user = user_fixture(%{password: password})
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
form =
form(lv, "#login_form", user: %{email: user.email, password: password, remember_me: true})
conn = submit_form(form, conn)
assert redirected_to(conn) == ~p"/"
end
test "redirects to login page with a flash error if there are no valid credentials", %{
conn: conn
} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
form =
form(lv, "#login_form",
user: %{email: "test@email.com", password: "123456", remember_me: true}
)
conn = submit_form(form, conn)
assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Invalid email or password"
assert redirected_to(conn) == "/users/log_in"
end
end
describe "login navigation" do
test "redirects to registration page when the Register button is clicked", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
{:ok, _login_live, login_html} =
lv
|> element(~s|main a:fl-contains("Создайте")|)
2024-07-11 01:14:16 +05:00
|> render_click()
|> follow_redirect(conn, ~p"/users/register")
assert login_html =~ "Зарегистрироваться"
2024-07-11 01:14:16 +05:00
end
test "redirects to forgot password page when the Forgot Password button is clicked", %{
conn: conn
} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
{:ok, conn} =
lv
|> element(~s|main a:fl-contains("Забыли пароль?")|)
2024-07-11 01:14:16 +05:00
|> render_click()
|> follow_redirect(conn, ~p"/users/reset_password")
assert conn.resp_body =~ "Забыли пароль?"
2024-07-11 01:14:16 +05:00
end
end
end