Add authorised routes with tests

This commit is contained in:
João Gilberto Balsini Moura 2020-09-14 12:21:48 -03:00 committed by Joao Gilberto Balsini Moura
parent 37985ac1cf
commit 8590df5032
6 changed files with 131 additions and 1 deletions

View file

@ -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"""
<section class="phx-hero">
<h1>Welcome to the admin dashboard!</h1>
</section>
"""
end
end

View file

@ -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"""
<section class="phx-hero">
<h1>Welcome to the user dashboard!</h1>
</section>
"""
end
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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]")