Add authorised routes with tests
This commit is contained in:
parent
37985ac1cf
commit
8590df5032
6 changed files with 131 additions and 1 deletions
test
real_estate_web/live
support/fixtures
30
test/real_estate_web/live/admin_dashboard_live_test.exs
Normal file
30
test/real_estate_web/live/admin_dashboard_live_test.exs
Normal 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
|
32
test/real_estate_web/live/user_dashboard_live_test.exs
Normal file
32
test/real_estate_web/live/user_dashboard_live_test.exs
Normal 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
|
|
@ -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]")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue