Skip to content

Commit

Permalink
Added block controller specs and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsierant committed Feb 29, 2024
1 parent f1b053e commit fedd960
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule BuildelWeb.OrganizationPipelineBlockController do
use BuildelWeb, :controller
use BuildelWeb.Validator
use OpenApiSpex.ControllerSpecs

import BuildelWeb.UserAuth

Expand All @@ -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} <-
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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
Expand Down

0 comments on commit fedd960

Please sign in to comment.