diff --git a/lib/real_estate_web/live/admin_dashboard_live.ex b/lib/real_estate_web/live/admin_dashboard_live.ex new file mode 100644 index 0000000..8b1912f --- /dev/null +++ b/lib/real_estate_web/live/admin_dashboard_live.ex @@ -0,0 +1,18 @@ +defmodule RealEstateWeb.AdminDashboardLive do + use RealEstateWeb, :live_view + + @impl true + def mount(_params, session, socket) do + socket = assign_defaults(session, socket) + {:ok, socket} + end + + @impl true + def render(assigns) do + ~L""" +
+

Welcome to the admin dashboard!

+
+ """ + end +end diff --git a/lib/real_estate_web/live/user_dashboard_live.ex b/lib/real_estate_web/live/user_dashboard_live.ex new file mode 100644 index 0000000..4a03913 --- /dev/null +++ b/lib/real_estate_web/live/user_dashboard_live.ex @@ -0,0 +1,18 @@ +defmodule RealEstateWeb.UserDashboardLive do + use RealEstateWeb, :live_view + + @impl true + def mount(_params, session, socket) do + socket = assign_defaults(session, socket) + {:ok, socket} + end + + @impl true + def render(assigns) do + ~L""" +
+

Welcome to the user dashboard!

+
+ """ + end +end diff --git a/lib/real_estate_web/router.ex b/lib/real_estate_web/router.ex index 1f0b4f4..c71757a 100644 --- a/lib/real_estate_web/router.ex +++ b/lib/real_estate_web/router.ex @@ -1,7 +1,7 @@ defmodule RealEstateWeb.Router do use RealEstateWeb, :router - import RealEstateWeb.UserAuth + alias RealEstateWeb.EnsureRolePlug pipeline :browser do plug :accepts, ["html"] @@ -17,6 +17,14 @@ defmodule RealEstateWeb.Router do plug :accepts, ["json"] end + pipeline :user do + plug EnsureRolePlug, [:admin, :user] + end + + pipeline :admin do + plug EnsureRolePlug, :admin + end + # Other scopes may use custom stacks. # scope "/api", RealEstateWeb do # pipe_through :api @@ -73,4 +81,16 @@ defmodule RealEstateWeb.Router do post "/users/confirm", UserConfirmationController, :create get "/users/confirm/:token", UserConfirmationController, :confirm end + + scope "/", RealEstateWeb do + pipe_through [:browser, :require_authenticated_user, :user] + + live "/user_dashboard", UserDashboardLive, :index + end + + scope "/", RealEstateWeb do + pipe_through [:browser, :require_authenticated_user, :admin] + + live "/admin_dashboard", AdminDashboardLive, :index + end end diff --git a/test/real_estate_web/live/admin_dashboard_live_test.exs b/test/real_estate_web/live/admin_dashboard_live_test.exs new file mode 100644 index 0000000..9534804 --- /dev/null +++ b/test/real_estate_web/live/admin_dashboard_live_test.exs @@ -0,0 +1,30 @@ +defmodule RealEstateWeb.AdminDashboardLiveTest do + use RealEstateWeb.ConnCase + + import Phoenix.LiveViewTest + import RealEstate.AccountsFixtures + + test "disconnected and connected render without authentication should redirect to login page", + %{conn: conn} do + # If we don't previously log in we will be redirected to the login page + assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/admin_dashboard") + end + + test "disconnected and connected render authenticated with user role should redirect to index page", + %{ + conn: conn + } do + conn = conn |> log_in_user(user_fixture()) + assert {:error, {:redirect, %{to: "/"}}} = live(conn, "/admin_dashboard") + end + + test "disconnected and connected render authenticated with admin role should redirect to index page", + %{ + conn: conn + } do + conn = conn |> log_in_user(admin_fixture()) + {:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard") + assert disconnected_html =~ "Welcome to the admin dashboard!" + assert render(admin_dashboard) =~ "Welcome to the admin dashboard!" + end +end diff --git a/test/real_estate_web/live/user_dashboard_live_test.exs b/test/real_estate_web/live/user_dashboard_live_test.exs new file mode 100644 index 0000000..fcd20b3 --- /dev/null +++ b/test/real_estate_web/live/user_dashboard_live_test.exs @@ -0,0 +1,32 @@ +defmodule RealEstateWeb.UserDashboardLiveTest do + use RealEstateWeb.ConnCase + + import Phoenix.LiveViewTest + import RealEstate.AccountsFixtures + + test "disconnected and connected render without authentication should redirect to login page", + %{conn: conn} do + # If we don't previously log in we will be redirected to the login page + assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/user_dashboard") + end + + test "disconnected and connected render authenticated with user role should redirect to index page", + %{ + conn: conn + } do + conn = conn |> log_in_user(user_fixture()) + {:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard") + assert disconnected_html =~ "Welcome to the user dashboard!" + assert render(user_dashboard) =~ "Welcome to the user dashboard!" + end + + test "disconnected and connected render authenticated with admin role should redirect to index page", + %{ + conn: conn + } do + conn = conn |> log_in_user(admin_fixture()) + {:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard") + assert disconnected_html =~ "Welcome to the user dashboard!" + assert render(user_dashboard) =~ "Welcome to the user dashboard!" + end +end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index cb8d029..79c2c43 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -19,6 +19,18 @@ defmodule RealEstate.AccountsFixtures do user end + def admin_fixture(attrs \\ %{}) do + {:ok, user} = + attrs + |> Enum.into(%{ + email: unique_user_email(), + password: valid_user_password() + }) + |> RealEstate.Accounts.register_admin() + + user + end + def extract_user_token(fun) do {:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]") [_, token, _] = String.split(captured.body, "[TOKEN]")