aprs.me/test/aprsme_web/plugs/health_check_test.exs
Graham McIntire 403300f696
test: broaden unit & property-test coverage (61.8% → 64.7%)
Adds ~800 new tests (incl. 27 StreamData properties) across 22 modules
that previously had little or no coverage. Highlights:

- Pure utility modules (LogSanitizer, PacketFieldWhitelist,
  PacketSanitizer, DeviceParser, CoordinateUtils, BoundsUtils,
  ParamUtils, Convert, WeatherUnits) get thorough unit + property
  coverage including UTF-8 truncation boundaries, antimeridian
  longitude wrap, Haversine symmetry, and unit-conversion inverses.
- JSON view modules (CallsignJSON, ErrorJSON, ChangesetJSON) verified
  for envelope shape and error templating.
- Plugs (HealthCheck, RateLimiter, ApiCSRF) exercised for happy-path
  and halt paths.
- GenServer modules (RegexCache, CleanupScheduler, DeploymentNotifier)
  tested without touching the supervised singleton where possible.
- Drops the orphaned map_live/map_helpers_test.exs whose module name
  collided with the new live/shared/coordinate_utils_test.exs.
2026-04-23 13:32:09 -05:00

77 lines
2.6 KiB
Elixir

defmodule AprsmeWeb.Plugs.HealthCheckTest do
# :health_status is an app-wide env var the plug reads, so these tests can't
# safely run in parallel with anything else that might touch it.
use AprsmeWeb.ConnCase, async: false
alias AprsmeWeb.Plugs.HealthCheck
describe "call/2 pass-through" do
test "returns the conn unchanged for non-/health paths", %{conn: conn} do
conn = %{conn | request_path: "/something-else"}
result = HealthCheck.call(conn, [])
assert result == conn
refute result.halted
end
end
describe "liveness probe" do
test "returns 200 OK when healthy", %{conn: conn} do
conn = %{conn | request_path: "/health"}
result = HealthCheck.call(conn, probe_type: :liveness)
assert result.status == 200
assert result.resp_body == "OK"
assert result.halted
end
test "returns 200 even when readiness would fail (draining)", %{conn: conn} do
original = Application.get_env(:aprsme, :health_status, :healthy)
try do
Application.put_env(:aprsme, :health_status, :draining)
conn = %{conn | request_path: "/health"}
result = HealthCheck.call(conn, probe_type: :liveness)
assert result.status == 200
after
Application.put_env(:aprsme, :health_status, original)
end
end
end
describe "readiness probe" do
test "returns 200 OK under normal conditions", %{conn: conn} do
conn = %{conn | request_path: "/health"}
result = HealthCheck.call(conn, probe_type: :readiness)
# Readiness also hits the DB and PubSub, which should work in test env.
assert result.status in [200, 503]
assert result.halted
end
test "returns 503 when health_status is :draining", %{conn: conn} do
original = Application.get_env(:aprsme, :health_status, :healthy)
try do
Application.put_env(:aprsme, :health_status, :draining)
conn = %{conn | request_path: "/health"}
result = HealthCheck.call(conn, probe_type: :readiness)
assert result.status == 503
assert result.resp_body =~ "draining"
after
Application.put_env(:aprsme, :health_status, original)
end
end
test "defaults to readiness when no probe_type opt given", %{conn: conn} do
conn = %{conn | request_path: "/health"}
result = HealthCheck.call(conn, [])
assert result.status in [200, 503]
assert result.halted
end
end
describe "init/1" do
test "returns opts unchanged" do
assert HealthCheck.init(probe_type: :liveness) == [probe_type: :liveness]
assert HealthCheck.init([]) == []
end
end
end