- TimeHelpers.to_datetime: `cond` with match?/2 guards becomes five function heads dispatched by input type — DateTime passes through, NaiveDateTime anchors to UTC, integer is a millisecond Unix epoch, binary tries ISO 8601, everything else is nil. - PageController.health: the three-branch `cond` + inner `if db_healthy` becomes a chain of small respond_* helpers, each pattern-matched on a single axis (health_status, shutting_down?, db_healthy?). db_healthy?/0 isolates the rescue. - format_uptime splits the cond into four format_uptime_parts/4 clauses. Adds tests for the PageController actions (health 200 / 503-draining, ready, sitemap.xml, .well-known/api-catalog, status.json). Coverage 66.81 → 67.29%.
82 lines
2.9 KiB
Elixir
82 lines
2.9 KiB
Elixir
defmodule AprsmeWeb.PageControllerTest do
|
|
# Mutates :aprsme, :health_status; must not run in parallel with other tests
|
|
# that read it (the HealthCheck plug, for example).
|
|
use AprsmeWeb.ConnCase, async: false
|
|
|
|
setup do
|
|
original = Application.get_env(:aprsme, :health_status)
|
|
on_exit(fn -> Application.put_env(:aprsme, :health_status, original || :healthy) end)
|
|
:ok
|
|
end
|
|
|
|
describe "health/2 controller action" do
|
|
# Call the action directly; the /health endpoint is otherwise served by
|
|
# the HealthCheck plug as text/plain.
|
|
test "returns 200 with ok status when app is healthy and DB is up" do
|
|
Application.put_env(:aprsme, :health_status, :healthy)
|
|
base = Phoenix.Controller.put_format(Phoenix.ConnTest.build_conn(), "json")
|
|
result = AprsmeWeb.PageController.health(base, %{})
|
|
assert result.status == 200
|
|
body = Jason.decode!(result.resp_body)
|
|
assert body["status"] == "ok"
|
|
assert body["database"] == "connected"
|
|
assert is_binary(body["version"])
|
|
end
|
|
|
|
test "returns 503 draining when app is draining" do
|
|
Application.put_env(:aprsme, :health_status, :draining)
|
|
base = Phoenix.Controller.put_format(Phoenix.ConnTest.build_conn(), "json")
|
|
result = AprsmeWeb.PageController.health(base, %{})
|
|
assert result.status == 503
|
|
body = Jason.decode!(result.resp_body)
|
|
assert body["status"] == "draining"
|
|
assert body["message"] =~ "draining"
|
|
end
|
|
end
|
|
|
|
describe "GET /ready" do
|
|
test "returns 200 with ready status and version", %{conn: conn} do
|
|
conn = get(conn, "/ready")
|
|
assert conn.status == 200
|
|
body = json_response(conn, 200)
|
|
assert body["status"] == "ready"
|
|
assert is_binary(body["version"])
|
|
end
|
|
end
|
|
|
|
describe "GET /sitemap.xml" do
|
|
test "returns an XML urlset with known paths", %{conn: conn} do
|
|
conn = get(conn, "/sitemap.xml")
|
|
assert conn.status == 200
|
|
xml = conn.resp_body
|
|
assert xml =~ ~s(<?xml version="1.0" encoding="UTF-8"?>)
|
|
assert xml =~ "<urlset"
|
|
assert xml =~ "/about"
|
|
assert xml =~ "/status"
|
|
end
|
|
end
|
|
|
|
describe "GET /.well-known/api-catalog" do
|
|
test "returns an RFC 9264-style linkset", %{conn: conn} do
|
|
conn = get(conn, "/.well-known/api-catalog")
|
|
assert conn.status == 200
|
|
body = Jason.decode!(conn.resp_body)
|
|
assert is_list(body["linkset"])
|
|
|
|
anchors = Enum.map(body["linkset"], & &1["anchor"])
|
|
assert Enum.any?(anchors, &String.contains?(&1, "/api/v1"))
|
|
assert Enum.any?(anchors, &String.contains?(&1, "/mobile/websocket"))
|
|
end
|
|
end
|
|
|
|
describe "GET /status.json (status_json/2)" do
|
|
test "returns application + aprs_is sections", %{conn: conn} do
|
|
conn = get(conn, "/status.json")
|
|
assert conn.status == 200
|
|
body = json_response(conn, 200)
|
|
assert Map.has_key?(body, "aprs_is")
|
|
assert Map.has_key?(body, "application")
|
|
assert is_binary(body["application"]["version"])
|
|
end
|
|
end
|
|
end
|