From 9746776bd78524b3e1180b5da9c6bccf0177ece1 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Mon, 24 Jun 2024 15:07:48 +0700 Subject: [PATCH] Add ability to list checks --- lib/polar/machines.ex | 4 ++ lib/polar/machines/check/manager.ex | 4 ++ .../publish/testing/check_controller.ex | 11 +++++ .../controllers/publish/testing/check_json.ex | 11 +++++ lib/polar_web/router.ex | 1 + .../{version => }/event_controller_test.exs | 2 +- .../publish/testing/check_controller_test.exs | 42 +++++++++++++++++++ 7 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/polar_web/controllers/publish/testing/check_json.ex rename test/polar_web/controllers/publish/{version => }/event_controller_test.exs (95%) create mode 100644 test/polar_web/controllers/publish/testing/check_controller_test.exs diff --git a/lib/polar/machines.ex b/lib/polar/machines.ex index e8cfec7..06a6fa1 100644 --- a/lib/polar/machines.ex +++ b/lib/polar/machines.ex @@ -11,6 +11,10 @@ defmodule Polar.Machines do alias __MODULE__.Check + defdelegate list_checks(), + to: Check.Manager, + as: :list + defdelegate create_check(params), to: Check.Manager, as: :create diff --git a/lib/polar/machines/check/manager.ex b/lib/polar/machines/check/manager.ex index 31e0d88..34cfcff 100644 --- a/lib/polar/machines/check/manager.ex +++ b/lib/polar/machines/check/manager.ex @@ -2,6 +2,10 @@ defmodule Polar.Machines.Check.Manager do alias Polar.Repo alias Polar.Machines.Check + def list() do + Repo.all(Check) + end + def create(params) do %Check{} |> Check.changeset(params) diff --git a/lib/polar_web/controllers/publish/testing/check_controller.ex b/lib/polar_web/controllers/publish/testing/check_controller.ex index e69de29..7707ca8 100644 --- a/lib/polar_web/controllers/publish/testing/check_controller.ex +++ b/lib/polar_web/controllers/publish/testing/check_controller.ex @@ -0,0 +1,11 @@ +defmodule PolarWeb.Publish.Testing.CheckController do + use PolarWeb, :controller + + alias Polar.Machines + + def index(conn, _params) do + checks = Machines.list_checks() + + render(conn, :index, %{checks: checks}) + end +end diff --git a/lib/polar_web/controllers/publish/testing/check_json.ex b/lib/polar_web/controllers/publish/testing/check_json.ex new file mode 100644 index 0000000..2f06e34 --- /dev/null +++ b/lib/polar_web/controllers/publish/testing/check_json.ex @@ -0,0 +1,11 @@ +defmodule PolarWeb.Publish.Testing.CheckJSON do + alias Polar.Machines.Check + + def index(%{checks: checks}) do + %{data: Enum.map(checks, &data/1)} + end + + def data(%Check{} = check) do + %{id: check.id, slug: check.slug} + end +end diff --git a/lib/polar_web/router.ex b/lib/polar_web/router.ex index 3cb9acf..33f6d0c 100644 --- a/lib/polar_web/router.ex +++ b/lib/polar_web/router.ex @@ -108,6 +108,7 @@ defmodule PolarWeb.Router do resources "/versions/:version_id/events", EventController, only: [:create] scope "/testing", as: :testing do + resources "/checks", Testing.CheckController, only: [:index] resources "/clusters", Testing.ClusterController, only: [:index] resources "/assessments/:assessment_id/events", EventController, only: [:create] diff --git a/test/polar_web/controllers/publish/version/event_controller_test.exs b/test/polar_web/controllers/publish/event_controller_test.exs similarity index 95% rename from test/polar_web/controllers/publish/version/event_controller_test.exs rename to test/polar_web/controllers/publish/event_controller_test.exs index c5cc2a1..8fd5ba8 100644 --- a/test/polar_web/controllers/publish/version/event_controller_test.exs +++ b/test/polar_web/controllers/publish/event_controller_test.exs @@ -1,4 +1,4 @@ -defmodule PolarWeb.Publish.Version.EventControllerTest do +defmodule PolarWeb.Publish.EventControllerTest do use PolarWeb.ConnCase import Polar.AccountsFixtures diff --git a/test/polar_web/controllers/publish/testing/check_controller_test.exs b/test/polar_web/controllers/publish/testing/check_controller_test.exs new file mode 100644 index 0000000..93c5ce7 --- /dev/null +++ b/test/polar_web/controllers/publish/testing/check_controller_test.exs @@ -0,0 +1,42 @@ +defmodule PolarWeb.Publish.Testing.CheckControllerTest do + use PolarWeb.ConnCase + + alias Polar.Accounts + alias Polar.Machines + + import Polar.AccountsFixtures + + setup do + password = Accounts.generate_automation_password() + + bot = bot_fixture(%{password: password}) + + user = Accounts.get_user_by_email_and_password(bot.email, password) + + session_token = + Accounts.generate_user_session_token(user) + |> Base.encode64() + + conn = + build_conn() + |> put_req_header("authorization", session_token) + + {:ok, _check} = + Machines.create_check(%{ + name: "ipv4-issuing", + description: "checks that ipv4 is correctly issued" + }) + + {:ok, conn: conn} + end + + describe "GET /publish/testing/checks" do + test "get list of available checks", %{conn: conn} do + conn = get(conn, "/publish/testing/checks") + + assert %{"data" => data} = json_response(conn, 200) + + assert Enum.count(data) == 1 + end + end +end