diff --git a/ee/app/models/dast_site.rb b/ee/app/models/dast_site.rb index d0809942519829a32d60d0e3088c0b089ef1e962..5bf52dc714a7fe3a00090aaec8c4c983081ab622 100644 --- a/ee/app/models/dast_site.rb +++ b/ee/app/models/dast_site.rb @@ -11,8 +11,14 @@ class DastSite < ApplicationRecord validates :project_id, presence: true validate :dast_site_validation_project_id_fk + after_destroy :cleanup_dast_site_token + private + def cleanup_dast_site_token + DastSiteToken.where(project_id: project.id, url: url).delete_all + end + def dast_site_validation_project_id_fk return unless dast_site_validation_id diff --git a/ee/spec/models/dast_site_spec.rb b/ee/spec/models/dast_site_spec.rb index 583a157f9935aee7039cde78c06b7b42bb0cef1a..a84be0c7cf86b1f9100e962da17e1275ff163eb8 100644 --- a/ee/spec/models/dast_site_spec.rb +++ b/ee/spec/models/dast_site_spec.rb @@ -44,4 +44,16 @@ end end end + + describe 'callbacks' do + context 'when there is a related site token' do + let_it_be(:dast_site) { create(:dast_site, project: project) } + let_it_be(:dast_site_token) { create(:dast_site_token, project: dast_site.project, url: dast_site.url) } + let_it_be(:dast_site_validations) { create_list(:dast_site_validation, 5, dast_site_token: dast_site_token) } + + it 'ensures it and associated site validations cleaned up on destroy' do + expect { dast_site.destroy! }.to change { DastSiteToken.count }.from(1).to(0).and change { DastSiteValidation.count }.from(5).to(0) + end + end + end end