Commit 6b3a3d12 authored by Patrick Bajao's avatar Patrick Bajao

Merge branch 'add-dast-site-cleanup-on-dast-site-profile-delete-231271' into 'master'

Clean up DastSite on DastSiteProfile destroy

See merge request gitlab-org/gitlab!38653
parents 0d2e4897 baf65910
......@@ -10,8 +10,14 @@ class DastSiteProfile < ApplicationRecord
scope :with_dast_site, -> { includes(:dast_site) }
after_destroy :cleanup_dast_site
private
def cleanup_dast_site
dast_site.destroy if dast_site.dast_site_profiles.empty?
end
def dast_site_project_id_fk
unless project_id == dast_site&.project_id
errors.add(:project_id, 'does not match dast_site.project')
......
......@@ -43,4 +43,24 @@ RSpec.describe DastSiteProfile, type: :model do
end
end
end
describe '#destroy!' do
context 'when the associated dast_site has no dast_site_profiles' do
it 'is also destroyed' do
subject.destroy!
expect { subject.dast_site.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when the associated dast_site has dast_site_profiles' do
it 'is not destroyed' do
create(:dast_site_profile, dast_site: subject.dast_site, project: subject.project)
subject.destroy!
expect { subject.dast_site.reload }.not_to raise_error
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment