|
9 | 9 |
|
10 | 10 | let(:user) { create(:user) } |
11 | 11 | let(:current_user) { create(:user, :admin) } |
| 12 | + let!(:ghost_user) do |
| 13 | + create(:user, username: User::GHOST_USERNAME, email: User::GHOST_EMAIL) |
| 14 | + end |
12 | 15 |
|
13 | 16 | it 'deletes the user successfully' do |
14 | 17 | namespace_id = user.ensure_namespace.id |
|
31 | 34 | ) |
32 | 35 | end |
33 | 36 |
|
| 37 | + context 'when the user authored audit events' do |
| 38 | + let!(:audit_event) do |
| 39 | + create(:audit_event, |
| 40 | + author: user, |
| 41 | + action_type: :user_logged_in, |
| 42 | + entity: user, |
| 43 | + target: AuditEvent::GLOBAL_TARGET) |
| 44 | + end |
| 45 | + |
| 46 | + it 'reassigns authored audit events to the ghost user' do |
| 47 | + expect { service_response }.to change { User.exists?(user.id) }.from(true).to(false) |
| 48 | + expect(service_response).to be_success |
| 49 | + expect(audit_event.reload.author).to eq(ghost_user) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context 'when the user owns a namespace' do |
| 54 | + let!(:namespace) { user.ensure_namespace } |
| 55 | + let!(:project) { create(:namespace_project, namespace: namespace) } |
| 56 | + let!(:flow) { create(:flow, project: project) } |
| 57 | + |
| 58 | + it 'deletes the owned namespace and associated projects and flows' do |
| 59 | + expect { service_response } |
| 60 | + .to change { Namespace.exists?(namespace.id) }.from(true).to(false) |
| 61 | + .and change { NamespaceProject.exists?(project.id) }.from(true).to(false) |
| 62 | + .and change { Flow.exists?(flow.id) }.from(true).to(false) |
| 63 | + |
| 64 | + expect(service_response).to be_success |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when deleting the current user' do |
| 69 | + let(:user) { current_user } |
| 70 | + |
| 71 | + it 'creates the deletion audit event with the ghost user as author' do |
| 72 | + expect(service_response).to be_success |
| 73 | + |
| 74 | + is_expected.to create_audit_event( |
| 75 | + :user_deleted, |
| 76 | + author_id: ghost_user.id, |
| 77 | + entity_type: 'User', |
| 78 | + entity_id: user.id, |
| 79 | + details: {}, |
| 80 | + target_type: 'global', |
| 81 | + target_id: 0 |
| 82 | + ) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'when deleting the ghost user' do |
| 87 | + let(:user) { ghost_user } |
| 88 | + |
| 89 | + it 'returns an invalid user error' do |
| 90 | + expect(service_response).not_to be_success |
| 91 | + expect(service_response.payload[:error_code]).to eq(:invalid_user) |
| 92 | + expect(User.exists?(ghost_user.id)).to be true |
| 93 | + end |
| 94 | + end |
| 95 | + |
34 | 96 | context 'when current user lacks permission' do |
35 | 97 | let(:current_user) { create(:user) } |
36 | 98 |
|
|
0 commit comments