From fcb02aa6907c66bcf58df93fb95f8e9d6d02103d Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 16 Jun 2022 09:48:22 +1000 Subject: [PATCH] fix: update validation for creating pacticipants to return a 400 when name is missing --- .../contracts/pacticipant_create_schema.rb | 28 +++++++++++++ lib/pact_broker/api/resources/pacticipants.rb | 4 +- spec/features/create_pacticipant_spec.rb | 6 +++ .../pacticipant_create_schema_spec.rb | 40 +++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 lib/pact_broker/api/contracts/pacticipant_create_schema.rb create mode 100644 spec/lib/pact_broker/api/contracts/pacticipant_create_schema_spec.rb diff --git a/lib/pact_broker/api/contracts/pacticipant_create_schema.rb b/lib/pact_broker/api/contracts/pacticipant_create_schema.rb new file mode 100644 index 000000000..b2ae305c9 --- /dev/null +++ b/lib/pact_broker/api/contracts/pacticipant_create_schema.rb @@ -0,0 +1,28 @@ +require "pact_broker/api/contracts/pacticipant_schema" + +module PactBroker + module Api + module Contracts + class PacticipantCreateSchema + extend DryValidationWorkarounds + extend PactBroker::Messages + using PactBroker::HashRefinements + + SCHEMA = Dry::Validation.Schema do + configure do + predicates(DryValidationPredicates) + config.messages_file = File.expand_path("../../../locale/en.yml", __FILE__) + end + required(:name).filled(:str?, :single_line?) + end + + def self.call(params_with_string_keys) + params = params_with_string_keys&.symbolize_keys + update_errors = PacticipantSchema::SCHEMA.call(params).messages(full: true) + create_errors = SCHEMA.call(params).messages(full: true) + select_first_message(flatten_indexed_messages(update_errors.merge(create_errors))) + end + end + end + end +end diff --git a/lib/pact_broker/api/resources/pacticipants.rb b/lib/pact_broker/api/resources/pacticipants.rb index 077ad8ce3..6131dd057 100644 --- a/lib/pact_broker/api/resources/pacticipants.rb +++ b/lib/pact_broker/api/resources/pacticipants.rb @@ -2,7 +2,7 @@ require "pact_broker/api/decorators/pacticipant_decorator" require "pact_broker/domain/pacticipant" require "pact_broker/hash_refinements" -require "pact_broker/api/contracts/pacticipant_schema" +require "pact_broker/api/contracts/pacticipant_create_schema" module PactBroker module Api @@ -66,7 +66,7 @@ def policy_name private def schema - PactBroker::Api::Contracts::PacticipantSchema + PactBroker::Api::Contracts::PacticipantCreateSchema end def pacticipants diff --git a/spec/features/create_pacticipant_spec.rb b/spec/features/create_pacticipant_spec.rb index 098168b59..9e0d39d67 100644 --- a/spec/features/create_pacticipant_spec.rb +++ b/spec/features/create_pacticipant_spec.rb @@ -33,4 +33,10 @@ subject expect(response_body).to include pacticipant_hash end + + context "with an empty JSON document" do + let(:pacticipant_hash) { {} } + + its(:status) { is_expected.to eq 400 } + end end diff --git a/spec/lib/pact_broker/api/contracts/pacticipant_create_schema_spec.rb b/spec/lib/pact_broker/api/contracts/pacticipant_create_schema_spec.rb new file mode 100644 index 000000000..8927c9db4 --- /dev/null +++ b/spec/lib/pact_broker/api/contracts/pacticipant_create_schema_spec.rb @@ -0,0 +1,40 @@ +require "pact_broker/api/contracts/pacticipant_schema" + +module PactBroker + module Api + module Contracts + describe PacticipantCreateSchema do + let(:params) do + { + name: "pact-broker", + displayName: "Pact Broker", + mainBranch: main_branch, + repositoryUrl: "https://github.com/pact-foundation/pact_broker", + repositoryName: "pact_broker", + repositoryNamespace: "pact-foundation" + } + end + + let(:main_branch) { "main" } + + subject { PacticipantCreateSchema.call(params) } + + context "with valid params" do + it { is_expected.to be_empty } + end + + context "with empty params" do + let(:params) do + { + repositoryUrl: "", + repositoryName: "", + repositoryNamespace: "" + } + end + + its([:name, 0]) { is_expected.to include "name is missing" } + end + end + end + end +end