prop/test/microwaveprop_web/metrics_plug_test.exs
Graham McIntire f99d07bd29
test: push line coverage from 71.7% → 78.67%
Adds ~600 new test cases (2013 → 2613 tests; 170 properties; 0
failures) across 12 new test files plus expansions of eight existing
files. Big lifts per module:

  ContactLive.Mechanism      33 → 100%
  MetricsPlug                54 → ~100%
  Microwaveprop.Release      15.8 → 70%+
  Telemetry                  38 → 76%
  HrrrNativeGridWorker       27.7 → 76%
  ContactLive.Show           34 → 48% (handler + render branches)
  Admin.ContactEditLive      49.7 → 70%+
  GefsFetchWorker            35.8 → ~55%
  IonosphereFetchWorker      56.3 → 75%
  PathLive                   58.9 → 67%
  WeatherMapLive             66.9 → 80.2%
  RoverLive                  0 → 70.3%
  ContactMapLive             59.2 → 89.8%
  ContactMapController       0 → 100%
  Mix tasks (Rust.Golden, Notebook, Backtest, HrrrBackfill,
    HrrrClimatology, HrrrNativeBackfill, NexradBackfill,
    RadarBackfill, ImportContestLogs, PropagationGrid,
    ResetEnrichment, Hrrr.PurgeGridPoints)  0 → ~60-100%

Two incidental fixes made while adding tests:

- ContactLive.Show.handle_event("toggle_flag", ...) was passing
  socket.assigns.current_scope to admin?/1 instead of socket.assigns,
  so admins never matched the pattern and every toggle ran the
  "Admins only." flash branch. Flag now toggles for admins again.

- Commercial.PollWorker.fetch_weather/1 promoted from private to
  @doc'd public so the IEM-fetch + ASOS-upsert path can be tested
  directly without driving perform/1 through live SNMP (which was
  timing out for ~50 s per test in the earlier attempt).

Stable property-test additions cover the dewpoint-from-RH monotonicity,
nearest_run/1 idempotence, and the ionosphere station envelope.
2026-04-23 18:43:18 -05:00

81 lines
2.6 KiB
Elixir

defmodule MicrowavepropWeb.MetricsPlugTest do
@moduledoc """
MetricsPlug mounts as a `forward "/metrics"` in the router, with an
optional bearer-token gate read from `Application.get_env/2`. These
tests exercise both the open and gated paths end-to-end.
"""
use MicrowavepropWeb.ConnCase, async: false
setup do
previous = Application.get_env(:microwaveprop, :prometheus_auth_token)
on_exit(fn -> Application.put_env(:microwaveprop, :prometheus_auth_token, previous) end)
:ok
end
describe "GET /metrics (no token configured)" do
test "returns 200 with Prometheus text-exposition body", %{conn: conn} do
Application.put_env(:microwaveprop, :prometheus_auth_token, nil)
conn = get(conn, ~p"/metrics")
assert conn.status == 200
assert [ctype] = get_resp_header(conn, "content-type")
assert ctype =~ "text/plain"
# PromEx's text body starts with '# HELP ...' or '# TYPE ...' metrics
# metadata lines. Exact metrics depend on what's registered, but
# any non-empty Prometheus body contains at least one '#' comment.
assert conn.resp_body =~ "#"
end
test "blank-string token behaves the same as an unset token", %{conn: conn} do
Application.put_env(:microwaveprop, :prometheus_auth_token, "")
conn = get(conn, ~p"/metrics")
assert conn.status == 200
end
end
describe "GET /metrics (bearer-token gated)" do
setup do
Application.put_env(:microwaveprop, :prometheus_auth_token, "s3cret-token")
:ok
end
test "401s without an Authorization header", %{conn: conn} do
conn = get(conn, ~p"/metrics")
assert conn.status == 401
assert conn.resp_body == "unauthorized"
# Standards-compliant www-authenticate challenge.
assert [challenge] = get_resp_header(conn, "www-authenticate")
assert challenge =~ ~s(Bearer realm="metrics")
end
test "401s when the Authorization header isn't a Bearer scheme", %{conn: conn} do
conn =
conn
|> put_req_header("authorization", "Basic dXNlcjpwYXNz")
|> get(~p"/metrics")
assert conn.status == 401
end
test "401s on a wrong bearer token", %{conn: conn} do
conn =
conn
|> put_req_header("authorization", "Bearer wrong-token")
|> get(~p"/metrics")
assert conn.status == 401
end
test "200s and serves the body when the bearer token matches", %{conn: conn} do
conn =
conn
|> put_req_header("authorization", "Bearer s3cret-token")
|> get(~p"/metrics")
assert conn.status == 200
assert conn.resp_body =~ "#"
end
end
end