Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 9, 2020
1 parent 1b1edd4 commit 8e35bf5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use Mix.Config

config :phoenix, :json_library, Jason

config :logger, level: :warn
config :logger, :console, format: "[$level] $message\n"

config :phoenix, :stacktrace_depth, 20
6 changes: 5 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ defmodule DemoWeb.Router do
import Phoenix.LiveDashboard.Router

pipeline :browser do
plug :fetch_session
plug :fetch_flash
end

live_dashboard("/dashboard", metrics: DemoWeb.Telemetry)
scope "/" do
pipe_through :browser
live_dashboard("/dashboard", metrics: DemoWeb.Telemetry)
end
end

defmodule DemoWeb.Endpoint do
Expand Down
29 changes: 29 additions & 0 deletions test/phoenix/live_dashboard/live/home_live_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Phoenix.LiveDashboard.HomeLiveTest do
use ExUnit.Case, async: true

import Phoenix.ConnTest
import Phoenix.LiveViewTest
@endpoint Phoenix.LiveDashboardTest.Endpoint

test "redirects when host is missing" do
conn = get(build_conn(), "/dashboard")
assert redirected_to(conn) == "/dashboard/nonode%40nohost"
end

test "shows system information" do
{:ok, live, _} = live(build_conn(), "/dashboard/nonode@nohost")
rendered = render(live)
assert rendered =~ "Update every"
assert rendered =~ to_string(:erlang.system_info(:system_version))
assert rendered =~ "Dashboard version: #{Application.spec(:phoenix_live_dashboard, :vsn)}"
assert rendered =~ ~r"Atoms: \d+ / \d+ \(\d+% used\)"
assert rendered =~ ~r"Ports: \d+ / \d+ \(\d+% used\)"
assert rendered =~ ~r"Processes: \d+ / \d+ \(\d+% used\)"
end

test "redirects to new node" do
{:ok, live, _} = live(build_conn(), "/dashboard/nonode@nohost")
send(live.pid, {:node_redirect, "foo@bar"})
assert_redirect live, "/dashboard/foo%40bar"
end
end
8 changes: 4 additions & 4 deletions test/phoenix/live_dashboard/logger_pubsub_backend_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ defmodule Phoenix.LiveDashboard.LoggerPubSubBackendTest do
@tag :capture_log
test "broadcasts messages when metadata matches" do
Phoenix.PubSub.subscribe(PubSub, "hello:world")
Logger.info("refute_received")
Logger.info("assert_received", logger_pubsub_backend: {PubSub, "hello:world"})
assert_receive {:logger, :info, msg}, 1000
assert IO.iodata_to_binary(msg) == "[info] assert_received\n"
Logger.error("refute_received")
Logger.error("assert_received", logger_pubsub_backend: {PubSub, "hello:world"})
assert_receive {:logger, :error, msg}, 1000
assert IO.iodata_to_binary(msg) == "[error] assert_received\n"
refute_received {:logger, _, _}
end
end
65 changes: 45 additions & 20 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
Phoenix.PubSub.PG2.start_link(name: Phoenix.LiveDashboardTest.PubSub)
Application.put_env(:phoenix_live_dashboard, Phoenix.LiveDashboardTest.Endpoint,
secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
live_view: [signing_salt: "hMegieSe"],
check_origin: false,
pubsub: [name: Phoenix.LiveDashboardTest.PubSub, adapter: Phoenix.PubSub.PG2]
)

defmodule Phoenix.LiveDashboardTest.Endpoint do
def url(), do: "http://localhost:4000"
def instrument(_, _, _, func), do: func.()
def config(:live_view), do: [signing_salt: "112345678212345678312345678412"]
def config(:secret_key_base), do: "5678567899556789656789756789856789956789"
def config(:pubsub_server), do: Phoenix.LiveDashboardTest.PubSub

def init(opts), do: opts

@parsers Plug.Parsers.init(
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
)

def call(conn, _) do
conn
|> Plug.Parsers.call(@parsers)
|> Plug.Conn.put_private(:phoenix_endpoint, __MODULE__)
defmodule Phoenix.LiveDashboardTest.Telemetry do
import Telemetry.Metrics

def metrics do
[
counter("a.b.c"),
counter("a.b.d"),
counter("e.f.g")
]
end
end

defmodule Phoenix.LiveDashboardTest.Router do
use Phoenix.Router
import Phoenix.LiveDashboard.Router

pipeline :browser do
plug :fetch_session
end

scope "/" do
pipe_through :browser
live_dashboard("/dashboard", metrics: Phoenix.LiveDashboardTest.Telemetry)
end
end

defmodule Phoenix.LiveDashboardTest.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_live_dashboard

plug Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger",
cookie_key: "request_logger"

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

plug Phoenix.LiveDashboardTest.Router
end

Phoenix.LiveDashboardTest.Endpoint.start_link()
ExUnit.start()

0 comments on commit 8e35bf5

Please sign in to comment.