|
| 1 | +RSpec.describe Mdm::ServiceLink, type: :model do |
| 2 | + it_should_behave_like 'Metasploit::Concern.run' |
| 3 | + |
| 4 | + context 'factory' do |
| 5 | + it 'should be valid' do |
| 6 | + service_link = FactoryBot.build(:mdm_service_link) |
| 7 | + expect(service_link).to be_valid |
| 8 | + end |
| 9 | + end |
| 10 | + |
| 11 | + context 'database' do |
| 12 | + |
| 13 | + context 'timestamps'do |
| 14 | + it { is_expected.to have_db_column(:created_at).of_type(:datetime).with_options(:null => false) } |
| 15 | + it { is_expected.to have_db_column(:updated_at).of_type(:datetime).with_options(:null => false) } |
| 16 | + end |
| 17 | + |
| 18 | + context 'columns' do |
| 19 | + it { is_expected.to have_db_column(:parent_id).of_type(:integer).with_options(:null => false) } |
| 20 | + it { is_expected.to have_db_column(:child_id).of_type(:integer).with_options(:null => false) } |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + context '#destroy' do |
| 25 | + it 'should successfully destroy one Mdm::ServiceLink' do |
| 26 | + service_link = FactoryBot.create(:mdm_service_link) |
| 27 | + expect { service_link.destroy }.to_not raise_error |
| 28 | + expect { service_link.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 29 | + end |
| 30 | + |
| 31 | + context 'with one parent and one child' do |
| 32 | + let(:parent_service1) { FactoryBot.create(:mdm_service, name: 'parent_service1') } |
| 33 | + let(:child_service1) { FactoryBot.create(:mdm_service, name: 'child_service1') } |
| 34 | + let!(:service_link1) { FactoryBot.create(:mdm_service_link, parent: parent_service1, child: child_service1) } |
| 35 | + |
| 36 | + it 'should only destroy the child service' do |
| 37 | + expect { service_link1.destroy }.to_not raise_error |
| 38 | + expect { service_link1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 39 | + expect { child_service1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 40 | + expect { parent_service1.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 41 | + end |
| 42 | + |
| 43 | + context 'with multiple children' do |
| 44 | + let(:child_service2) { FactoryBot.create(:mdm_service, name: 'child_service2') } |
| 45 | + let!(:service_link2) { FactoryBot.create(:mdm_service_link, parent: parent_service1, child: child_service2) } |
| 46 | + |
| 47 | + it 'should only destroy the child service related to this service link' do |
| 48 | + expect { service_link1.destroy }.to_not raise_error |
| 49 | + expect { service_link1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 50 | + expect { service_link2.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 51 | + expect { child_service1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 52 | + expect { child_service2.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 53 | + expect { parent_service1.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'with multiple nested children' do |
| 58 | + let(:child_service2) { FactoryBot.create(:mdm_service, name: 'child_service2') } |
| 59 | + let!(:service_link2) { FactoryBot.create(:mdm_service_link, parent: child_service1, child: child_service2) } |
| 60 | + |
| 61 | + it 'should only destroy the nested child services and service links' do |
| 62 | + expect { service_link1.destroy }.to_not raise_error |
| 63 | + expect { service_link1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 64 | + expect { service_link2.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 65 | + expect { child_service1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 66 | + expect { child_service2.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 67 | + expect { parent_service1.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context 'with a child that has another parent' do |
| 72 | + let(:parent_service2) { FactoryBot.create(:mdm_service, name: 'parent_service2') } |
| 73 | + let!(:service_link2) { FactoryBot.create(:mdm_service_link, parent: parent_service2, child: child_service1) } |
| 74 | + |
| 75 | + it 'should not destroy the child' do |
| 76 | + expect { service_link1.destroy }.to_not raise_error |
| 77 | + expect { service_link1.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 78 | + expect { service_link2.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 79 | + expect { child_service1.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 80 | + expect { parent_service1.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 81 | + expect { parent_service2.reload }.to_not raise_error(ActiveRecord::RecordNotFound) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context "Associations" do |
| 88 | + it { is_expected.to belong_to(:parent).class_name('Mdm::Service') } |
| 89 | + it { is_expected.to belong_to(:child).class_name('Mdm::Service') } |
| 90 | + end |
| 91 | + |
| 92 | + context "validations" do |
| 93 | + it "should not allow duplicate associations" do |
| 94 | + parent_service = FactoryBot.build(:mdm_service) |
| 95 | + child_service = FactoryBot.build(:mdm_service) |
| 96 | + FactoryBot.create(:mdm_service_link, :parent => parent_service, :child => child_service) |
| 97 | + service_link2 = FactoryBot.build(:mdm_service_link, :parent => parent_service, :child => child_service) |
| 98 | + expect(service_link2).not_to be_valid |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'callbacks' do |
| 103 | + context 'before_destroy' do |
| 104 | + it 'should call #destroy_orphan_child' do |
| 105 | + service_link = FactoryBot.create(:mdm_service_link) |
| 106 | + expect(service_link).to receive(:destroy_orphan_child) |
| 107 | + service_link.destroy |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | +end |
0 commit comments