Skip to content

Commit

Permalink
feat(pacticipant versions): support use of PATCH with application/mer…
Browse files Browse the repository at this point in the history
…ge-patch+json to create versions
  • Loading branch information
bethesque committed Mar 10, 2021
1 parent 4aa8e12 commit 0429398
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 50 deletions.
4 changes: 2 additions & 2 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
Expand Down
76 changes: 30 additions & 46 deletions spec/features/create_version_spec.rb
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0429398

Please sign in to comment.