From e9d772eb7acefd8ba141327704ca86a214845949 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 19 Oct 2022 10:25:54 +1100 Subject: [PATCH] fix: add validation to ensure an environment or to tag is specified for the /can-i-deploy endpoint --- lib/pact_broker/locale/en.yml | 1 + .../matrix/can_i_deploy_query_schema.rb | 6 ++++++ .../matrix/can_i_deploy_query_schema_spec.rb | 15 +++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/lib/pact_broker/locale/en.yml b/lib/pact_broker/locale/en.yml index 3b34628b5..7f27e6ddb 100644 --- a/lib/pact_broker/locale/en.yml +++ b/lib/pact_broker/locale/en.yml @@ -85,6 +85,7 @@ en: invalid_webhook_uuid: The UUID can only contain the characters A-Z, a-z, 0-9, _ and -, and must be 16 or more characters. pacticipant_not_found: No pacticipant with name '%{name}' found environment_name_must_be_unique: Another environment with name '%{name}' already exists. + must_specify_environment_or_tag: Must specify either an environment or a 'to' tag. cannot_specify_tag_and_environment: Cannot specify both a 'to' tag and an environment. cannot_specify_latest_and_environment: Cannot specify both latest=true and an environment. cannot_specify_more_than_one_destination_identifier: Cannot specify more than one of tag, environment and mainBranch. diff --git a/lib/pact_broker/matrix/can_i_deploy_query_schema.rb b/lib/pact_broker/matrix/can_i_deploy_query_schema.rb index 58369354f..04065385f 100644 --- a/lib/pact_broker/matrix/can_i_deploy_query_schema.rb +++ b/lib/pact_broker/matrix/can_i_deploy_query_schema.rb @@ -2,12 +2,14 @@ require "pact_broker/messages" require "pact_broker/api/contracts/dry_validation_predicates" require "pact_broker/project_root" +require "pact_broker/string_refinements" module PactBroker module Api module Contracts class CanIDeployQuerySchema extend PactBroker::Messages + using PactBroker::StringRefinements SCHEMA = Dry::Validation.Schema do configure do @@ -26,6 +28,10 @@ def self.call(params) result[:to] ||= [] result[:to] << message("errors.validation.cannot_specify_tag_and_environment") end + if params[:to].blank? && params[:environment].blank? + result[:environment] ||= [] + result[:environment] << message("errors.validation.must_specify_environment_or_tag") + end result end diff --git a/spec/lib/pact_broker/matrix/can_i_deploy_query_schema_spec.rb b/spec/lib/pact_broker/matrix/can_i_deploy_query_schema_spec.rb index 51715c37d..0917548c6 100644 --- a/spec/lib/pact_broker/matrix/can_i_deploy_query_schema_spec.rb +++ b/spec/lib/pact_broker/matrix/can_i_deploy_query_schema_spec.rb @@ -35,6 +35,21 @@ module Contracts it { is_expected.to_not be_empty } end + context "with neither a to tag or an environment specified" do + before do + allow(PactBroker::Deployments::EnvironmentService).to receive(:find_by_name).and_return(double("environment")) + end + + let(:params) do + { + pacticipant: "foo", + version: "1" + } + end + + it { is_expected.to_not be_empty } + end + context "when the environment does exist" do before do allow(PactBroker::Deployments::EnvironmentService).to receive(:find_by_name).and_return(double("environment"))