defmodule MvWeb.LiveUserAuthTest do @moduledoc """ Regression tests for the `MvWeb.LiveUserAuth` on_mount guards: the unauthenticated `:live_user_required` redirect to the sign-in page and the authenticated `:live_no_user` redirect away from the sign-in page. """ use MvWeb.ConnCase, async: false import Phoenix.LiveViewTest describe ":live_user_required" do @tag role: :unauthenticated test "unauthenticated request to a protected route is redirected to sign-in", %{conn: conn} do assert {:error, {:redirect, %{to: to}}} = live(conn, "/members") assert to == "/sign-in" end @tag role: :admin test "authenticated user can mount a protected route", %{conn: conn} do assert {:ok, _view, _html} = live(conn, "/members") end end describe ":live_no_user" do @tag role: :admin test "authenticated user visiting the sign-in page is redirected to root", %{conn: conn} do assert {:error, {:redirect, %{to: "/"}}} = live(conn, "/sign-in") end @tag role: :unauthenticated test "unauthenticated user can reach the sign-in page", %{conn: conn} do assert {:ok, _view, _html} = live(conn, "/sign-in") end end end