1.5 KiB · text History 3208487
defmodule VigilWeb.HealthPlugTest do
  # Changes app env for /metrics access: not alongside other tests.
  use VigilWeb.ConnCase, async: false

  test "liveness, and readiness in Sentry's shape", %{conn: conn} do
    assert conn |> get("/_health/") |> response(200) == "ok"

    assert %{"problems" => [], "healthy" => %{"database" => true, "oban" => true}} =
             build_conn() |> get("/api/0/internal/health/") |> json_response(200)
  end

  test "/metrics is off by default, behind a token, or public" do
    assert build_conn() |> get("/metrics") |> response(404)

    Application.put_env(:vigil, :metrics_token, "s3cret")
    on_exit(fn -> Application.delete_env(:vigil, :metrics_token) end)

    assert build_conn() |> get("/metrics") |> response(401)

    assert build_conn()
           |> put_req_header("authorization", "Bearer wrong")
           |> get("/metrics")
           |> response(401)

    :telemetry.execute([:vigil, :ingest, :accepted], %{count: 3}, %{type: "event"})

    body =
      build_conn()
      |> put_req_header("authorization", "Bearer s3cret")
      |> get("/metrics")
      |> response(200)

    assert body =~ ~s(vigil_ingest_accepted_count{type="event"})
    assert body =~ "vigil_repo_query_total_time"

    Application.delete_env(:vigil, :metrics_token)
    Application.put_env(:vigil, :metrics_public, true)
    on_exit(fn -> Application.delete_env(:vigil, :metrics_public) end)
    assert build_conn() |> get("/metrics") |> response(200)
  end
end