From 69f0420852da57346e9b7f70a337fd5f1a6cdd19 Mon Sep 17 00:00:00 2001 From: Belinda Liu Date: Mon, 10 May 2021 18:21:45 +0000 Subject: [PATCH] refactor dependent destroy tests for user.rb Co-authored-by: Belinda Liu Co-authored-by: Matthew Kocher --- spec/unit/actions/user_delete_spec.rb | 94 --------------------------- spec/unit/models/runtime/user_spec.rb | 23 +++++++ 2 files changed, 23 insertions(+), 94 deletions(-) diff --git a/spec/unit/actions/user_delete_spec.rb b/spec/unit/actions/user_delete_spec.rb index 12d4aa758fb..42667c91a95 100644 --- a/spec/unit/actions/user_delete_spec.rb +++ b/spec/unit/actions/user_delete_spec.rb @@ -14,100 +14,6 @@ module VCAP::CloudController }.to change { User.count }.by(-1) expect { user.refresh }.to raise_error Sequel::Error, 'Record not found' end - - describe 'recursive deletion' do - let(:user) { User.make } - let(:space) { Space.make } - let(:org) { space.organization } - - describe 'roles' do - it 'deletes associated space auditor roles' do - set_current_user_as_role(role: 'space_auditor', org: org, space: space, user: user) - role = SpaceAuditor.find(user_id: user.id, space_id: space.id) - - expect { - user_delete.delete([user]) - }.to change { user.audited_spaces.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated space application supporter roles' do - set_current_user_as_role(role: 'spaces_application_supporters', org: org, space: space, user: user) - role = SpaceApplicationSupporter.find(user_id: user.id, space_id: space.id) - - expect { - user_delete.delete([user]) - }.to change { user.application_supported_spaces.count }.by(-1) - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated space developer roles' do - set_current_user_as_role(role: 'space_developer', org: org, space: space, user: user) - role = SpaceDeveloper.find(user_id: user.id, space_id: space.id) - - expect { - user_delete.delete([user]) - }.to change { user.spaces.count }.by(-1) - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated space manager roles' do - set_current_user_as_role(role: 'space_manager', org: org, space: space, user: user) - role = SpaceManager.find(user_id: user.id, space_id: space.id) - - expect { - user_delete.delete([user]) - }.to change { user.managed_spaces.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated org user roles' do - set_current_user_as_role(role: 'org_user', org: org, user: user) - role = OrganizationUser.find(user_id: user.id, organization_id: org.id) - - expect { - user_delete.delete([user]) - }.to change { user.organizations.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated org auditor roles' do - set_current_user_as_role(role: 'org_auditor', org: org, user: user) - role = OrganizationAuditor.find(user_id: user.id, organization_id: org.id) - - expect { - user_delete.delete([user]) - }.to change { user.audited_organizations.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated org manager roles' do - set_current_user_as_role(role: 'org_manager', org: org, user: user) - role = OrganizationManager.find(user_id: user.id, organization_id: org.id) - - expect { - user_delete.delete([user]) - }.to change { user.managed_organizations.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - - it 'deletes associated org billing manager roles' do - set_current_user_as_role(role: 'org_billing_manager', org: org, user: user) - role = OrganizationBillingManager.find(user_id: user.id, organization_id: org.id) - - expect { - user_delete.delete([user]) - }.to change { user.billing_managed_organizations.count }.by(-1) - - expect { role.reload }.to raise_error Sequel::NoExistingObject - end - end - end end end end diff --git a/spec/unit/models/runtime/user_spec.rb b/spec/unit/models/runtime/user_spec.rb index 55947843dca..532497efa25 100644 --- a/spec/unit/models/runtime/user_spec.rb +++ b/spec/unit/models/runtime/user_spec.rb @@ -32,6 +32,29 @@ module VCAP::CloudController it { is_expected.to have_associated :managed_spaces, class: Space } it { is_expected.to have_associated :audited_spaces, class: Space } it { is_expected.to have_associated :application_supported_spaces, class: Space } + + describe 'destroy dependent associations' do + let(:user) { User.make } + + [ + SpaceApplicationSupporter, + SpaceAuditor, + SpaceDeveloper, + SpaceManager, + OrganizationUser, + OrganizationBillingManager, + OrganizationAuditor, + OrganizationManager + ].each do |role_class| + context role_class.to_s do + it 'deletes the association' do + role_class.make(user: user) + + expect { user.destroy }.to change { role_class.count }.by(-1) + end + end + end + end end describe 'Validations' do