-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_service_spec.rb
More file actions
64 lines (52 loc) · 2.11 KB
/
Copy pathdelete_service_spec.rb
File metadata and controls
64 lines (52 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Users::DeleteService do
subject(:service_response) do
described_class.new(create_authentication(current_user), user).execute
end
let(:user) { create(:user) }
let(:current_user) { create(:user, :admin) }
it 'deletes the user successfully' do
namespace_id = user.ensure_namespace.id
expect { service_response }.to change { User.exists?(user.id) }.from(true).to(false)
.and change {
Namespace.exists?(namespace_id)
}.from(true).to(false)
expect(service_response).to be_success
expect(service_response.payload).to eq(user)
is_expected.to create_audit_event(
:user_deleted,
author_id: current_user.id,
entity_type: 'User',
entity_id: user.id,
details: {},
target_type: 'global',
target_id: 0
)
end
context 'when the user is the last administrator' do
let(:user) { current_user }
it 'does not delete the user' do
expect(service_response).not_to be_success
expect(service_response.payload[:error_code]).to eq(:cannot_delete_last_administrator)
expect(User.exists?(user.id)).to be true
is_expected.not_to create_audit_event(:user_deleted)
end
end
context 'when the user is an administrator and another administrator exists' do
let(:user) { create(:user, :admin) }
it 'deletes the user successfully' do
expect { service_response }.to change { User.exists?(user.id) }.from(true).to(false)
expect(service_response).to be_success
end
end
context 'when current user lacks permission' do
let(:current_user) { create(:user) }
it 'returns a missing permission error' do
expect(service_response).not_to be_success
expect(service_response.payload[:error_code]).to eq(:missing_permission)
expect(User.exists?(user.id)).to be true
is_expected.not_to create_audit_event(:user_deleted)
end
end
end