Commit baf65910 authored by Philip Cunningham's avatar Philip Cunningham Committed by Patrick Bajao

Clean up DastSite on DastSiteProfile destroy

parent 313d4d0f
......@@ -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