Commit 5b1a28d6 authored by Tan Le's avatar Tan Le

Ensure audit event is added when user is destroyed

Integration test is also added to extend coverage for both soft
delete (delete user, not contributions) and hard delete (delete
user only).
parent f3fc5ac9
......@@ -7,11 +7,11 @@ module EE
override :execute
def execute(user, options = {})
super(user, options) do |delete_user|
result = super(user, options) do |delete_user|
mirror_cleanup(delete_user)
end
log_audit_event(user) if options[:hard_delete]
log_audit_event(user) if result.destroyed?
end
def mirror_cleanup(user)
......
......@@ -15,17 +15,58 @@ describe Users::DestroyService do
stub_licensed_features(admin_audit_log: true)
end
context 'when hard_delete' do
let(:hard_delete) { true }
context 'soft delete' do
let(:hard_delete) { false }
context 'when user destroy operation succeeds' do
it 'logs audit events for ghost user migration and destroy operation' do
service.execute(user, hard_delete: hard_delete)
expect(AuditEvent.last(3)).to contain_exactly(
have_attributes(details: hash_including(change: 'email address')),
have_attributes(details: hash_including(change: 'username')),
have_attributes(details: hash_including(remove: 'user'))
)
end
end
it 'logs audit event' do
expected_message = "Removed user"
context 'when user destroy operation fails' do
before do
allow(user).to receive(:destroy).and_return(user)
end
expect do
it 'logs audit events for ghost user migration operation' do
service.execute(user, hard_delete: hard_delete)
end.to change { AuditEvent.count }.by(1)
expect(AuditEvent.last.present.action).to eq(expected_message)
expect(AuditEvent.last(2)).to contain_exactly(
have_attributes(details: hash_including(change: 'email address')),
have_attributes(details: hash_including(change: 'username'))
)
end
end
end
context 'hard delete' do
let(:hard_delete) { true }
context 'when user destroy operation succeeds' do
it 'logs audit events for destroy operation' do
service.execute(user, hard_delete: hard_delete)
expect(AuditEvent.last)
.to have_attributes(details: hash_including(remove: 'user'))
end
end
context 'when user destroy operation fails' do
before do
allow(user).to receive(:destroy).and_return(user)
end
it 'does not log any audit event' do
expect { service.execute(user, hard_delete: hard_delete) }
.not_to change { AuditEvent.count }
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment