Skip to content

Commit

Permalink
feat: return link to latest pact if more pacts exist when deleting pa…
Browse files Browse the repository at this point in the history
…cts by tag
  • Loading branch information
bethesque committed Nov 30, 2020
1 parent 2c948fb commit b87ea70
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
15 changes: 2 additions & 13 deletions lib/pact_broker/api/resources/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
require 'pact_broker/api/contracts/put_pact_params_contract'
require 'pact_broker/webhooks/execution_configuration'
require 'pact_broker/api/resources/webhook_execution_methods'
require 'pact_broker/api/resources/pact_resource_methods'

module PactBroker
module Api
module Resources
class Pact < BaseResource
include PacticipantResourceMethods
include WebhookExecutionMethods
include PactResourceMethods

def content_types_provided
[
Expand Down Expand Up @@ -110,19 +112,6 @@ def pact_params
@pact_params ||= PactBroker::Pacts::PactParams.from_request request, path_info
end

def set_post_deletion_response
latest_pact = pact_service.find_latest_pact(pact_params)
response_body = { "_links" => { index: { href: base_url } } }
if latest_pact
response_body["_links"]["pb:latest-pact-version"] = {
href: latest_pact_url(base_url, latest_pact),
title: "Latest pact"
}
end
response.body = response_body.to_json
response.headers["Content-Type" => "application/hal+json;charset=utf-8"]
end

def webhook_options
{
database_connector: database_connector,
Expand Down
23 changes: 23 additions & 0 deletions lib/pact_broker/api/resources/pact_resource_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module PactBroker
module Api
module Resources
module PactResourceMethods
def set_post_deletion_response
latest_pact = pact_service.find_latest_pact(
consumer_name: pact_params[:consumer_name],
provider_name: pact_params[:provider_name]
)
response_body = { "_links" => { index: { href: base_url } } }
if latest_pact
response_body["_links"]["pb:latest-pact-version"] = {
href: latest_pact_url(base_url, latest_pact),
title: "Latest pact"
}
end
response.body = response_body.to_json
response.headers["Content-Type" => "application/hal+json;charset=utf-8"]
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/pact_broker/api/resources/tagged_pact_versions.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'pact_broker/api/resources/base_resource'
require 'pact_broker/configuration'
require 'pact_broker/api/decorators/tagged_pact_versions_decorator'
require 'pact_broker/api/resources/pact_resource_methods'

module PactBroker
module Api
module Resources
class TaggedPactVersions < BaseResource
include PactResourceMethods

def content_types_provided
[["application/hal+json", :to_json]]
end
Expand All @@ -24,6 +27,7 @@ def to_json

def delete_resource
pact_service.delete_all_pact_publications_between consumer_name, and: provider_name, tag: identifier_from_path[:tag]
set_post_deletion_response
true
end

Expand Down
4 changes: 2 additions & 2 deletions spec/features/delete_tagged_pact_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
expect{ subject }.to change{ PactBroker::Pacts::PactPublication.count }.by(-1)
end

it "returns a 204" do
expect(subject.status).to be 204
it "returns a 200" do
expect(subject.status).to be 200
end
end

Expand Down
29 changes: 20 additions & 9 deletions spec/lib/pact_broker/api/resources/pact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Resources
let(:json) { {some: 'json'}.to_json }

describe "GET" do

context "Accept: text/html" do

let(:json_content) { 'json_content' }
Expand Down Expand Up @@ -130,20 +129,20 @@ module Resources
end

describe "DELETE" do

subject { delete "/pacts/provider/Provider/consumer/Consumer/version/1.2", json, {'CONTENT_TYPE' => "application/json"} ; last_response }

let(:pact) { double('pact') }
let(:pact_service) { PactBroker::Pacts::Service }
let(:response) { subject; last_response }

before do
allow(pact_service).to receive(:find_pact).and_return(pact)
allow(pact_service).to receive(:delete)
allow(pact_service).to receive(:find_latest_pact).and_return(latest_pact)
allow_any_instance_of(described_class).to receive(:latest_pact_url).and_return("http://latest-pact")
end

context "when the pact exists" do
let(:pact) { double('pact') }
let(:pact_service) { PactBroker::Pacts::Service }
let(:latest_pact) { double('latest pact') }

subject { delete "/pacts/provider/Provider/consumer/Consumer/version/1.2", json, {'CONTENT_TYPE' => "application/json"} ; last_response }

context "when the pact exists" do
it "deletes the pact using the pact service" do
expect(pact_service).to receive(:delete).with(instance_of(PactBroker::Pacts::PactParams))
subject
Expand All @@ -152,6 +151,18 @@ module Resources
it "returns a 200" do
expect(subject.status).to eq 200
end

it "returns a link to the latest pact" do
expect(JSON.parse(subject.body)["_links"]["pb:latest-pact-version"]["href"]).to eq "http://latest-pact"
end

context "with there are no more pacts" do
let(:latest_pact) { nil }

it "does not return a link" do
expect(JSON.parse(subject.body)["_links"]).to_not have_key("pb:latest-pact-version")
end
end
end

context "when the pact does not exist" do
Expand Down
12 changes: 10 additions & 2 deletions spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,25 @@ module Resources
context "DELETE" do
before do
allow(pact_service).to receive(:delete_all_pact_publications_between)
allow(pact_service).to receive(:find_latest_pact).and_return(latest_pact)
allow_any_instance_of(described_class).to receive(:latest_pact_url).and_return("http://latest-pact")
end

let(:latest_pact) { double('pact') }

subject { delete(path) }

it "deletes all the pacts with the given consumer/provider/tag" do
expect(pact_service).to receive(:delete_all_pact_publications_between).with("Foo", and: "Bar", tag: "prod")
subject
end

it "returns a 204" do
expect(subject.status).to eq 204
it "returns a 200" do
expect(subject.status).to eq 200
end

it "returns a link to the latest pact" do
expect(JSON.parse(subject.body)["_links"]["pb:latest-pact-version"]["href"]).to eq "http://latest-pact"
end
end
end
Expand Down

0 comments on commit b87ea70

Please sign in to comment.