From b87ea7043c69a20bb93a981a49e77850d783d562 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 1 Dec 2020 10:33:45 +1100 Subject: [PATCH] feat: return link to latest pact if more pacts exist when deleting pacts by tag --- lib/pact_broker/api/resources/pact.rb | 15 ++-------- .../api/resources/pact_resource_methods.rb | 23 +++++++++++++++ .../api/resources/tagged_pact_versions.rb | 4 +++ .../delete_tagged_pact_versions_spec.rb | 4 +-- .../pact_broker/api/resources/pact_spec.rb | 29 +++++++++++++------ .../resources/tagged_pact_versions_spec.rb | 12 ++++++-- 6 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 lib/pact_broker/api/resources/pact_resource_methods.rb diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index da2524513..757b1c779 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -8,6 +8,7 @@ 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 @@ -15,6 +16,7 @@ module Resources class Pact < BaseResource include PacticipantResourceMethods include WebhookExecutionMethods + include PactResourceMethods def content_types_provided [ @@ -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, diff --git a/lib/pact_broker/api/resources/pact_resource_methods.rb b/lib/pact_broker/api/resources/pact_resource_methods.rb new file mode 100644 index 000000000..e4f6cf7da --- /dev/null +++ b/lib/pact_broker/api/resources/pact_resource_methods.rb @@ -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 \ No newline at end of file diff --git a/lib/pact_broker/api/resources/tagged_pact_versions.rb b/lib/pact_broker/api/resources/tagged_pact_versions.rb index 95b8738f9..569ce677e 100644 --- a/lib/pact_broker/api/resources/tagged_pact_versions.rb +++ b/lib/pact_broker/api/resources/tagged_pact_versions.rb @@ -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 @@ -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 diff --git a/spec/features/delete_tagged_pact_versions_spec.rb b/spec/features/delete_tagged_pact_versions_spec.rb index 5e38e14d0..bd418d5ed 100644 --- a/spec/features/delete_tagged_pact_versions_spec.rb +++ b/spec/features/delete_tagged_pact_versions_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/api/resources/pact_spec.rb b/spec/lib/pact_broker/api/resources/pact_spec.rb index c7b1bea2d..e21cbc6b3 100644 --- a/spec/lib/pact_broker/api/resources/pact_spec.rb +++ b/spec/lib/pact_broker/api/resources/pact_spec.rb @@ -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' } @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb b/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb index 97dae479d..51f37ab98 100644 --- a/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb +++ b/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb @@ -69,8 +69,12 @@ 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 @@ -78,8 +82,12 @@ module Resources 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