defmodule BerrypodWeb.Auth.RegistrationTest do
  use BerrypodWeb.ConnCase

  import Phoenix.LiveViewTest
  import Berrypod.AccountsFixtures

  describe "Registration page" do
    test "redirects to setup when no admin exists (fresh install)", %{conn: conn} do
      assert {:error, {:redirect, %{to: "/setup"}}} = live(conn, ~p"/users/register")
    end

    test "redirects to login when admin already exists", %{conn: conn} do
      user_fixture()

      assert {:error,
              {:redirect, %{to: "/users/log-in", flash: %{"error" => "Registration is closed"}}}} =
               live(conn, ~p"/users/register")
    end

    test "redirects if already logged in", %{conn: conn} do
      result =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/users/register")
        |> follow_redirect(conn, ~p"/setup")

      assert {:ok, _conn} = result
    end
  end
end
