Files
self c20a3b4f71 feat: prefix Supertest plugin slug
Rename the Supertest fixture plugin to tribe-one-supertest across manifest, runtime API routes, metrics, e2e fixtures, and tests.
2026-06-17 22:33:39 +02:00

113 lines
3.5 KiB
Elixir

defmodule TribeOne.TribesPlugin.SupertestWeb.APIPlug do
@moduledoc """
Local-only JSON API used by the infra supertest runner.
"""
import Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
if loopback?(conn.remote_ip) do
dispatch(conn, supertest_path(conn.path_info), conn.method)
else
send_json(conn, 403, %{ok: false, error: "local-only"})
end
end
defp dispatch(conn, ["health"], "GET") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.health())
end
defp dispatch(conn, ["schema"], "GET") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.schema())
end
defp dispatch(conn, ["reset"], "POST") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.reset(conn.body_params))
end
defp dispatch(conn, ["cases"], "POST") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.create_case(conn.body_params))
end
defp dispatch(conn, ["cases", id, "increment"], "POST") do
conn
|> send_result(
TribeOne.TribesPlugin.Supertest.API.increment_case(Map.put(conn.body_params, "id", id))
)
end
defp dispatch(conn, ["events"], "POST") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.create_event(conn.body_params))
end
defp dispatch(conn, ["cluster-pubsub", "broadcast"], "POST") do
conn
|> send_result(TribeOne.TribesPlugin.Supertest.API.broadcast_cluster_pubsub(conn.body_params))
end
defp dispatch(conn, ["cluster-pubsub", "reset"], "POST") do
conn
|> send_result(TribeOne.TribesPlugin.Supertest.API.reset_cluster_pubsub(conn.body_params))
end
defp dispatch(conn, ["cluster-pubsub", "received"], "GET") do
conn
|> send_result(TribeOne.TribesPlugin.Supertest.API.received_cluster_pubsub(conn.query_params))
end
defp dispatch(conn, ["metrics", "probe"], "POST") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.emit_metrics_probe(conn.body_params))
end
defp dispatch(conn, ["scheduler", "probes"], "POST") do
conn
|> send_result(TribeOne.TribesPlugin.Supertest.API.start_scheduler_probe(conn.body_params))
end
defp dispatch(conn, ["scheduler", "probes", run_id], "GET") do
conn
|> send_result(TribeOne.TribesPlugin.Supertest.API.scheduler_probe(%{"run_id" => run_id}))
end
defp dispatch(conn, ["scheduler", "probes", run_id, "stop"], "POST") do
conn
|> send_result(
TribeOne.TribesPlugin.Supertest.API.stop_scheduler_probe(%{"run_id" => run_id})
)
end
defp dispatch(conn, ["state"], "GET") do
conn |> send_result(TribeOne.TribesPlugin.Supertest.API.state(conn.query_params))
end
defp dispatch(conn, _path, _method) do
send_json(conn, 404, %{ok: false, error: "not-found"})
end
defp send_result(conn, {:ok, payload}), do: send_json(conn, 200, payload)
defp send_result(conn, {:error, reason}) do
send_json(conn, 422, %{ok: false, error: inspect(reason)})
end
defp send_json(conn, status, payload) do
body = Jason.encode!(payload)
conn
|> put_resp_content_type("application/json")
|> send_resp(status, body)
|> halt()
end
defp supertest_path(["plugins-api", "tribe-one-supertest" | rest]), do: rest
defp supertest_path(["tribe-one-supertest" | rest]), do: rest
defp supertest_path(path), do: path
defp loopback?({127, _second, _third, _fourth}), do: true
defp loopback?({0, 0, 0, 0, 0, 0, 0, 1}), do: true
defp loopback?({0, 0, 0, 0, 0, 65_535, high, _low}) when high in 0x7F00..0x7FFF, do: true
defp loopback?(_remote_ip), do: false
end