diff --git a/lib/pact_broker/api/resources/verification.rb b/lib/pact_broker/api/resources/verification.rb index 28cc11408..07d5d5a19 100644 --- a/lib/pact_broker/api/resources/verification.rb +++ b/lib/pact_broker/api/resources/verification.rb @@ -20,7 +20,7 @@ def content_types_provided # Remember to update latest_verification_id_for_pact_version_and_provider_version # if/when DELETE is implemented def allowed_methods - ["GET", "OPTIONS"] + ["GET", "OPTIONS", "DELETE"] end def resource_exists? @@ -40,6 +40,11 @@ def to_extended_json extended_decorator_for(verification).to_json(user_options: { base_url: base_url }) end + def delete_resource + verification_service.delete(verification) + true + end + private def verification diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index 87bf5525d..0854b4e5f 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -33,6 +33,10 @@ def create verification, provider_version_number, pact verification end + def delete(verification_id) + PactBroker::Domain::Verification.where(id: verification_id).delete + end + def update_latest_verification_id verification params = { pact_version_id: verification.pact_version_id, diff --git a/lib/pact_broker/verifications/service.rb b/lib/pact_broker/verifications/service.rb index 9fce09fbc..32c07925d 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -39,6 +39,11 @@ def create next_verification_number, params, pact, webhook_options verification end + def delete(verification) + webhook_service.delete_all_webhook_related_objects_by_verification_ids(verification.id) + verification_repository.delete(verification.id) + end + def errors params contract = PactBroker::Api::Contracts::VerificationContract.new(PactBroker::Domain::Verification.new) contract.validate(params) diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index ce08c4992..3baecefe3 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -68,6 +68,10 @@ def self.delete_all_webhook_related_objects_by_pact_publication_ids pact_publica webhook_repository.delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids end + def self.delete_all_webhook_related_objects_by_verification_ids verification_ids + webhook_repository.delete_triggered_webhooks_by_verification_ids verification_ids + end + def self.find_all webhook_repository.find_all end diff --git a/spec/features/delete_verification_spec.rb b/spec/features/delete_verification_spec.rb new file mode 100644 index 000000000..04bc9dedc --- /dev/null +++ b/spec/features/delete_verification_spec.rb @@ -0,0 +1,29 @@ +require 'support/test_data_builder' +require 'pact_broker/api/pact_broker_urls' + + +describe "Delete a verification" do + + let!(:verification) do + TestDataBuilder.new + .create_pact_with_verification("Foo", "1", "Bar", "2") + .create_provider_webhook(event_names: ['provider_verification_published']) + .create_triggered_webhook + .create_webhook_execution + .and_return(:verification) + end + + let(:path) { PactBroker::Api::PactBrokerUrls.verification_url(verification, "") } + let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} + + subject { delete(path) } + + it "deletes the verification" do + expect { subject }.to change { PactBroker::Domain::Verification.count }.by(-1) + end + + it "returns a 204 response" do + subject + expect(last_response.status).to eq 204 + end +end