From fedd9606d99baafb5d12178689ec195bc3cee0dc Mon Sep 17 00:00:00 2001 From: pawelsierant Date: Thu, 29 Feb 2024 14:46:06 +0100 Subject: [PATCH] Added block controller specs and updated tests --- .../pipelines/blocks/block_controller.ex | 44 ++++++++++++++----- .../organizations/pipelines/blocks/blocks.ex | 24 ++++++++++ ...ization_pipeline_block_controller_test.exs | 26 +++++------ 3 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 apps/api/lib/buildel_web/schemas/organizations/pipelines/blocks/blocks.ex diff --git a/apps/api/lib/buildel_web/controllers/organizations/pipelines/blocks/block_controller.ex b/apps/api/lib/buildel_web/controllers/organizations/pipelines/blocks/block_controller.ex index 8bb6b3ac6..87f28668d 100644 --- a/apps/api/lib/buildel_web/controllers/organizations/pipelines/blocks/block_controller.ex +++ b/apps/api/lib/buildel_web/controllers/organizations/pipelines/blocks/block_controller.ex @@ -1,6 +1,6 @@ defmodule BuildelWeb.OrganizationPipelineBlockController do use BuildelWeb, :controller - use BuildelWeb.Validator + use OpenApiSpex.ControllerSpecs import BuildelWeb.UserAuth @@ -14,20 +14,40 @@ defmodule BuildelWeb.OrganizationPipelineBlockController do plug(:fetch_current_user) plug(:require_authenticated_user) - defparams(:create) do - required(:block, :map) do - required(:name, :string) - required(:type, :string) - required(:opts, :map) - end - end + plug OpenApiSpex.Plug.CastAndValidate, + json_render_error_v2: true, + render_error: BuildelWeb.ErrorRendererPlug + + tags ["block"] + + operation :create, + summary: "Create block", + parameters: [ + organization_id: [in: :path, description: "Organization ID", type: :integer, required: true], + pipeline_id: [in: :path, description: "Pipeline ID", type: :integer, required: true] + ], + request_body: {"block", "application/json", BuildelWeb.Schemas.Blocks.CreateRequest}, + responses: [ + created: {"created", "application/json", nil}, + unprocessable_entity: + {"unprocessable entity", "application/json", + BuildelWeb.Schemas.Errors.UnprocessableEntity}, + unauthorized: + {"unauthorized", "application/json", BuildelWeb.Schemas.Errors.UnauthorizedResponse}, + forbidden: {"forbidden", "application/json", BuildelWeb.Schemas.Errors.ForbiddenResponse} + ] + + def create(conn, _params) do + %{block: block_config} = conn.body_params + + %{ + organization_id: organization_id, + pipeline_id: pipeline_id + } = conn.params - def create(conn, %{"organization_id" => organization_id, "pipeline_id" => pipeline_id} = params) do user = conn.assigns.current_user - with {:ok, %{block: block_config}} <- - validate(:create, params), - {:ok, organization} <- Organizations.get_user_organization(user, organization_id), + with {:ok, organization} <- Organizations.get_user_organization(user, organization_id), {:ok, %Pipeline{} = pipeline} <- Pipelines.get_organization_pipeline(organization, pipeline_id), {:ok, %Pipeline{} = _pipeline} <- diff --git a/apps/api/lib/buildel_web/schemas/organizations/pipelines/blocks/blocks.ex b/apps/api/lib/buildel_web/schemas/organizations/pipelines/blocks/blocks.ex new file mode 100644 index 000000000..60e4e09d3 --- /dev/null +++ b/apps/api/lib/buildel_web/schemas/organizations/pipelines/blocks/blocks.ex @@ -0,0 +1,24 @@ +defmodule BuildelWeb.Schemas.Blocks do + alias OpenApiSpex.Schema + + defmodule CreateRequest do + require OpenApiSpex + + OpenApiSpex.schema(%{ + title: "BlockCreateRequest", + type: :object, + properties: %{ + block: %Schema{ + type: :object, + properties: %{ + name: %Schema{type: :string, description: "Block name"}, + type: %Schema{type: :string, description: "Block type"}, + opts: %Schema{type: :object, description: "Block options"} + }, + required: [:name, :type, :opts] + } + }, + required: [:block] + }) + end +end diff --git a/apps/api/test/buildel_web/controllers/organization_pipeline_block_controller_test.exs b/apps/api/test/buildel_web/controllers/organization_pipeline_block_controller_test.exs index e286705fb..79583dc9b 100644 --- a/apps/api/test/buildel_web/controllers/organization_pipeline_block_controller_test.exs +++ b/apps/api/test/buildel_web/controllers/organization_pipeline_block_controller_test.exs @@ -6,7 +6,10 @@ defmodule BuildelWeb.OrganizationPipelineBlockControllerTest do alias Buildel.Organizations setup %{conn: conn} do - {:ok, conn: put_req_header(conn, "accept", "application/json")} + {:ok, + conn: + put_req_header(conn, "accept", "application/json") + |> put_req_header("content-type", "application/json")} end setup [ @@ -16,19 +19,14 @@ defmodule BuildelWeb.OrganizationPipelineBlockControllerTest do ] describe "create" do - test "requires authentication", %{conn: conn, organization: organization, pipeline: pipeline} do - conn = conn |> log_out_user() - - conn = - post(conn, ~p"/api/organizations/#{organization}/pipelines/#{pipeline}/blocks", %{ - block: %{ - name: "block", - type: "text_input", - opts: %{} - } - }) - - assert json_response(conn, 401) + test_requires_authentication %{conn: conn, organization: organization, pipeline: pipeline} do + post(conn, ~p"/api/organizations/#{organization}/pipelines/#{pipeline}/blocks", %{ + block: %{ + name: "block", + type: "text_input", + opts: %{} + } + }) end test "requires organization membership", %{conn: conn, another_pipeline: pipeline} do