diff --git a/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb b/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb index c1948aea5..9ea4aa20a 100644 --- a/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb +++ b/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb @@ -25,6 +25,14 @@ def post_is_create? true end + def malformed_request? + if request.post? + malformed_post_request? + else + false + end + end + def create_path deployed_version_url(OpenStruct.new(uuid: deployed_version_uuid), base_url) end @@ -67,12 +75,21 @@ def deployed_version_uuid end def replaced_previous_deployed_version - params[:replacedPreviousDeployedVersion] == true + params(default: {})[:replacedPreviousDeployedVersion] end def title "Deployed versions for #{pacticipant_name} version #{pacticipant_version_number}" end + + def malformed_post_request? + if ![true, false].include?(replaced_previous_deployed_version) + set_json_validation_error_messages({ replacedPreviousDeployedVersion: ["must be one of true, false"] }) + true + else + false + end + end end end end diff --git a/spec/features/record_deployment_spec.rb b/spec/features/record_deployment_spec.rb index 48dc00f95..329861454 100644 --- a/spec/features/record_deployment_spec.rb +++ b/spec/features/record_deployment_spec.rb @@ -19,8 +19,9 @@ .find{ |relation| relation["name"] == "test" } .fetch("href") end + let(:request_body) { { replacedPreviousDeployedVersion: replaced_previous }.to_json } - subject { post(path, { replacedPreviousDeployedVersion: replaced_previous }.to_json, headers) } + subject { post(path, request_body, headers) } it { is_expected.to be_a_hal_json_created_response } @@ -33,14 +34,32 @@ end it "marks the previous deployment as not currently deployed" do + expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.collect(&:uuid) } + end + + it "does not change the overall count of currently deployed versions" do expect { subject }.to_not change { PactBroker::Deployments::DeployedVersion.currently_deployed.count } end + context "with an empty body" do + let(:request_body) { nil } + + it { is_expected.to be_a_json_error_response("must be one of true, false") } + + it "does not change the overall count of currently deployed versions" do + expect { subject }.to_not change { PactBroker::Deployments::DeployedVersion.currently_deployed.count } + end + end + context "when the deployment does not replace the previous deployed version" do let(:replaced_previous) { false } it "leaves the previous deployed version as currently deployed" do expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1) end + + it "increases the overall count of currently deployed versions" do + expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1) + end end end