Skip to content

Commit

Permalink
Test and implementation for warning on missing Beacon.LiveAdmin.Plug …
Browse files Browse the repository at this point in the history
…in the :browser pipeline (#75)

* Test and implementation for raising error and providing a warning when Beacon.LiveAdmin.Plug is missing from the :browser pipeline. Closes #71.

* remove the lines from .gitignore that were added.

* Run mix format to pass CI check.

* Simplify session Map access and raise if beacon_live_admin_page_url not set.
  • Loading branch information
kgautreaux authored Sep 20, 2023
1 parent 94e7a65 commit ee9dc0d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ beacon_live_admin-*.tar

# Ignore assets that are produced by build tools.
/priv/static/assets/
/priv/static/beacon_live_admin.min.css.map

# Ignore digested assets cache.
/priv/static/cache_manifest.json
Expand Down
9 changes: 8 additions & 1 deletion lib/beacon/live_admin/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ defmodule Beacon.LiveAdmin.PageLive do
Cluster.maybe_discover_sites()

sites = Beacon.LiveAdmin.Cluster.running_sites()
%{"pages" => pages, "beacon_live_admin_page_url" => current_url} = session

%{"pages" => pages} = session
current_url = Map.get(session, "beacon_live_admin_page_url") || raise """
Failed to resolve Beacon.LiveAdmin page URL
You must add Beacon.LiveAdmin.Plug to the :browser pipeline that beacon_live_admin is piped through.
"""

page = lookup_page!(socket, current_url)

socket =
Expand Down
15 changes: 15 additions & 0 deletions test/beacon/live_admin/live/page_editor_live/index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ defmodule Beacon.LiveAdmin.PageEditorLive.IndexTest do
{:ok, _live, html} = live(conn, "/admin/site_a/pages")
assert html =~ "site_a_home_page"
end

test "raises when missing beacon_live_admin_url in the session" do
assert_raise RuntimeError, fn ->
conn =
Phoenix.ConnTest.dispatch(
build_conn(:get, "/admin/site_a/pages"),
Beacon.LiveAdminTest.PluglessEndpoint,
:get,
"/admin/site_a/pages",
nil
)

{:ok, _live, _html} = live(conn, "/admin/site_a/pages")
end
end
end
2 changes: 1 addition & 1 deletion test/beacon/live_admin/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule Beacon.LiveAdmin.RouterTest do
)
end

test "dose not assign root_layout" do
test "does not assign root_layout" do
assert_raise ArgumentError, fn ->
Router.__session_options__("admin", [], root_layout: {MyApp.Layouts, :other})
end
Expand Down
37 changes: 36 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,44 @@ defmodule Beacon.LiveAdminTest.Endpoint do
plug Beacon.LiveAdminTest.Router
end

# Load config and Endpoints for testing that plug Beacon.LiveAdmin.Plug is missing from the Router :browser pipeline
Application.put_env(:beacon_live_admin_plugless, Beacon.LiveAdminTest.PluglessEndpoint,
url: [host: "localhost", port: 4010],
secret_key_base: "TrXbWpjZWxk0GXclXOHFCoufQh1oRK0N5rev5GcpbPCsuf2C/kbYlMgeEEAXPayF",
live_view: [signing_salt: "nXvN+c8y"],
render_errors: [view: Beacon.LiveAdminTest.ErrorView],
check_origin: false
)

defmodule Beacon.LiveAdminTest.PluglessRouter do
use Phoenix.Router
use Beacon.LiveAdmin.Router

pipeline :browser do
plug :fetch_session
end

scope "/" do
pipe_through :browser
beacon_live_admin("/admin")
end
end

defmodule Beacon.LiveAdminTest.PluglessEndpoint do
use Phoenix.Endpoint, otp_app: :beacon_live_admin_plugless

plug Plug.Session,
store: :cookie,
key: "_live_view_key",
signing_salt: "/VEDsdfsffMnp5"

plug Beacon.LiveAdminTest.PluglessRouter
end

Supervisor.start_link(
[
Beacon.LiveAdminTest.Endpoint
Beacon.LiveAdminTest.Endpoint,
Beacon.LiveAdminTest.PluglessEndpoint
],
strategy: :one_for_one
)
Expand Down

0 comments on commit ee9dc0d

Please sign in to comment.