From 0429398f3db0c6e96e2eb34bbdd4af0aa9f2e67e Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 11 Mar 2021 09:10:12 +1100 Subject: [PATCH] feat(pacticipant versions): support use of PATCH with application/merge-patch+json to create versions --- lib/pact_broker/versions/repository.rb | 4 +- lib/pact_broker/versions/service.rb | 2 +- spec/features/create_version_spec.rb | 76 ++++++++----------- ...ed_versions_for_version_and_environment.rb | 2 +- 4 files changed, 34 insertions(+), 50 deletions(-) diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index 00f8cb203..c3a3bcbdd 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -71,8 +71,8 @@ def create_or_update(pacticipant, version_number, open_struct_version) # Upsert is only for race conditions # Upsert blanks out any fields that are not provided saved_version = PactBroker::Domain::Version.new( - open_struct_version.to_h.merge( - pacticipant_id: pacticipant, + params.merge( + pacticipant_id: pacticipant.id, number: version_number ) ).upsert diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index ece471b55..8d5fc7310 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -14,7 +14,7 @@ def self.conflict_errors(existing_version, open_struct_version, version_url) new_branch: open_struct_version.branch, version_url: version_url } - error_message = message("errors.validation.cannot_modify_version_branch", message_params).tap { |it| puts it } + error_message = message("errors.validation.cannot_modify_version_branch", message_params) { branch: [error_message] } else {} diff --git a/spec/features/create_version_spec.rb b/spec/features/create_version_spec.rb index 15c993faa..00b0129e5 100644 --- a/spec/features/create_version_spec.rb +++ b/spec/features/create_version_spec.rb @@ -1,6 +1,7 @@ describe "Creating a pacticipant version" do let(:path) { "/pacticipants/Foo/versions/1234" } - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } + let(:headers) { { 'CONTENT_TYPE' => content_type } } + let(:content_type) { 'application/json' } let(:response_body) { JSON.parse(subject.body, symbolize_names: true)} let(:version_hash) do { @@ -10,65 +11,48 @@ } end - subject { put(path, version_hash.to_json, headers) } + context "with a PUT" do + subject { put(path, version_hash.to_json, headers) } - it "returns a 201 response" do - expect(subject.status).to be 201 - end - - it "returns a HAL JSON Content Type" do - expect(subject.headers['Content-Type']).to eq 'application/hal+json;charset=utf-8' - end - - it "returns the newly created version" do - expect(response_body).to include branch: "main", buildUrl: "http://build" - expect(response_body[:_embedded][:tags].size).to eq 2 - end - - it "creates the specified tags" do - expect { subject }.to change { PactBroker::Domain::Tag.count }.by(2) - end + it "returns a 201 response" do + expect(subject.status).to be 201 + end - context "when the version already exists" do - before do - td.subtract_day - .create_consumer("Foo") - .create_consumer_version("1234", branch: "original-branch", build_url: "original-build-url") - .create_consumer_version_tag("dev") + it "returns a HAL JSON Content Type" do + expect(subject.headers['Content-Type']).to eq 'application/hal+json;charset=utf-8' end - context "when the branch is attempted to be changed" do - let(:version_hash) { { branch: "new-branch" } } + it "returns the newly created version" do + expect(response_body).to include branch: "main", buildUrl: "http://build" + expect(response_body[:_embedded][:tags].size).to eq 2 + end - its(:status) { is_expected.to eq 409 } + it "creates the specified tags" do + expect { subject }.to change { PactBroker::Domain::Tag.count }.by(2) end + end - context "when the branch is not attempted to be changed" do - let(:version_hash) { { branch: "original-branch" } } + context "with a PATCH" do + let(:content_type) { 'application/merge-patch+json' } - it "overwrites the direct properties and blanks out any unprovided ones" do - expect(response_body[:branch]).to eq "original-branch" - expect(response_body).to_not have_key(:buildUrl) - end - end + subject { patch(path, version_hash.to_json, headers) } - context "when no tags are specified" do - it "does not change the tags" do - expect { subject }.to_not change { PactBroker::Domain::Version.for("Foo", "1234").tags } - end + it "returns a 201 response" do + expect(subject.status).to be 201 end - context "when tags are specified" do - let(:version_hash) { { branch: "original-branch", tags: [ { name: "main" }] } } + it "returns a HAL JSON Content Type" do + expect(subject.headers['Content-Type']).to eq 'application/hal+json;charset=utf-8' + end - it "overwrites the tags" do - expect(response_body[:_embedded][:tags].size).to eq 1 - expect(response_body[:_embedded][:tags].first[:name]).to eq "main" - end + it "returns the newly created version" do + expect(response_body).to include branch: "main", buildUrl: "http://build" + expect(response_body[:_embedded][:tags].size).to eq 2 end - it "does not change the created date" do - expect { subject }.to_not change { PactBroker::Domain::Version.for("Foo", "1234").created_at } + it "creates the specified tags" do + expect { subject }.to change { PactBroker::Domain::Tag.count }.by(2) end end + end diff --git a/spec/features/get_deployed_versions_for_version_and_environment.rb b/spec/features/get_deployed_versions_for_version_and_environment.rb index 42c415f09..951cb899b 100644 --- a/spec/features/get_deployed_versions_for_version_and_environment.rb +++ b/spec/features/get_deployed_versions_for_version_and_environment.rb @@ -16,7 +16,7 @@ let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) } - subject { get(path, nil, { "HTTP_ACCEPT" => "application/hal+json" }).tap { |it| puts it.body } } + subject { get(path, nil, { "HTTP_ACCEPT" => "application/hal+json" }) } it "returns a list of deployed versions" do expect(response_body_hash[:_embedded][:deployedVersions]).to be_a(Array)