Skip to content

Commit

Permalink
refactor dependent destroy tests for user.rb
Browse files Browse the repository at this point in the history
Co-authored-by: Belinda Liu <[email protected]>
Co-authored-by: Matthew Kocher <[email protected]>
  • Loading branch information
belinda-liu and mkocher committed May 10, 2021
1 parent 17120da commit 69f0420
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 94 deletions.
94 changes: 0 additions & 94 deletions spec/unit/actions/user_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions spec/unit/models/runtime/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69f0420

Please sign in to comment.