Skip to content

Commit

Permalink
Add route for admin dashboard w/ tests and auth
Browse files Browse the repository at this point in the history
  • Loading branch information
susanwalker committed Oct 29, 2023
1 parent 72b782d commit 4b0b189
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ if config_env() != :test do
String.to_integer(
System.get_env("SECRET_EXPIRATION_CHECK_PERIOD_MS", "5000")
)

config :heimdall,
admin_user: System.get_env("ADMIN_USER", "admin"),
admin_password: System.get_env("ADMIN_PASSWORD", "admin")
end
4 changes: 4 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ config :heimdall, dev_routes: true
config :heimdall, Heimdall.SecretsPruner,
enabled: true,
time_interval_ms: 1_000_000

config :heimdall,
admin_user: "admin",
admin_password: "admin"
13 changes: 13 additions & 0 deletions lib/heimdall/admin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Heimdall.Admin do
@moduledoc """
Admin-related tasks
"""

@doc """
Returns Heimdall stats using given parameters
"""
@spec stats(map()) :: map()
def stats(params) do
params
end
end
10 changes: 10 additions & 0 deletions lib/heimdall_web/controllers/admin/dashboard_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule HeimdallWeb.Admin.DashboardController do
use HeimdallWeb, :controller

alias Heimdall.Admin

def index(conn, params) do
stats = Admin.stats(params)
render(conn, :index, stats: stats)
end
end
5 changes: 5 additions & 0 deletions lib/heimdall_web/controllers/admin/dashboard_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule HeimdallWeb.Admin.DashboardHTML do
use HeimdallWeb, :html

embed_templates "dashboard_html/*"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Admin Dashboard: TODO
33 changes: 33 additions & 0 deletions lib/heimdall_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ defmodule HeimdallWeb.Router do
plug RemoteIp
end

pipeline :admin_browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {HeimdallWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug RemoteIp
plug :admin_auth
end

pipeline :api do
plug :accepts, ["json"]
end
Expand All @@ -29,6 +40,12 @@ defmodule HeimdallWeb.Router do
get "/secret_404", SecretController, :secret_404
end

scope "/admin", HeimdallWeb.Admin do
pipe_through :admin_browser

get "/", DashboardController, :index
end

scope "/api", HeimdallWeb.API do
pipe_through :api

Expand All @@ -44,4 +61,20 @@ defmodule HeimdallWeb.Router do
live_dashboard "/dashboard", metrics: HeimdallWeb.Telemetry
end
end

defp admin_auth(conn, _params) do
Plug.BasicAuth.basic_auth(
conn,
username: admin_user(),
password: admin_password()
)
end

defp admin_user do
Application.get_env(:heimdall, :admin_user)
end

defp admin_password do
Application.get_env(:heimdall, :admin_password)
end
end
1 change: 0 additions & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
alias Heimdall.Repo
41 changes: 41 additions & 0 deletions test/heimdall_web/controllers/admin/dashboard_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule HeimdallWeb.Admin.DashboardControllerTest do
use HeimdallWeb.ConnCase

describe "index/2 (GET /admin)" do
test "doesn't allow view page without user and password", %{conn: conn} do
conn = get(conn, ~p"/admin")

assert conn.status == 401
end

test "shows dashboard if authenticated", %{conn: conn} do
conn = add_admin_auth(conn)

conn = get(conn, ~p"/admin")

refute conn.status == 401
end
end

defp add_admin_auth(conn) do
basic_auth =
Plug.BasicAuth.encode_basic_auth(
admin_user(),
admin_password()
)

put_req_header(
conn,
"authorization",
basic_auth
)
end

defp admin_user do
Application.get_env(:heimdall, :admin_user)
end

defp admin_password do
Application.get_env(:heimdall, :admin_password)
end
end

0 comments on commit 4b0b189

Please sign in to comment.