Commit f3fc5ac9 authored by Tan Le's avatar Tan Le

Record audit event when hard-deleting user

Add tests to cover all available user actions
parent a2e4362e
...@@ -120,6 +120,10 @@ module EE ...@@ -120,6 +120,10 @@ module EE
) )
end end
def for_user
for_custom_model('user', @entity.full_path)
end
def for_project def for_project
for_custom_model('project', @entity.full_path) for_custom_model('project', @entity.full_path)
end end
......
...@@ -10,6 +10,8 @@ module EE ...@@ -10,6 +10,8 @@ module EE
super(user, options) do |delete_user| super(user, options) do |delete_user|
mirror_cleanup(delete_user) mirror_cleanup(delete_user)
end end
log_audit_event(user) if options[:hard_delete]
end end
def mirror_cleanup(user) def mirror_cleanup(user)
...@@ -31,6 +33,14 @@ module EE ...@@ -31,6 +33,14 @@ module EE
mirror_owners.first mirror_owners.first
end end
def log_audit_event(user)
::AuditEventService.new(
current_user,
user,
action: :destroy
).for_user.security_event
end
end end
end end
end end
...@@ -214,6 +214,61 @@ describe AuditEventService do ...@@ -214,6 +214,61 @@ describe AuditEventService do
end end
end end
describe '#for_user' do
let(:author_name) { 'Administrator' }
let(:current_user) { instance_spy(User, name: author_name) }
let(:target_user_full_path) { 'ejohn' }
let(:user) { instance_spy(User, full_path: target_user_full_path) }
let(:custom_message) { 'Some strange event has occurred' }
let(:ip_address) { '127.0.0.1' }
let(:options) { { action: action, custom_message: custom_message, ip_address: ip_address } }
subject(:service) { described_class.new(current_user, user, options).for_user }
context 'with destroy action' do
let(:action) { :destroy }
it 'sets the details attribute' do
expect(service.instance_variable_get(:@details)).to eq(
remove: 'user',
author_name: author_name,
target_id: target_user_full_path,
target_type: 'User',
target_details: target_user_full_path
)
end
end
context 'with create action' do
let(:action) { :create }
it 'sets the details attribute' do
expect(service.instance_variable_get(:@details)).to eq(
add: 'user',
author_name: author_name,
target_id: target_user_full_path,
target_type: 'User',
target_details: target_user_full_path
)
end
end
context 'with custom action' do
let(:action) { :custom }
it 'sets the details attribute' do
expect(service.instance_variable_get(:@details)).to eq(
custom_message: custom_message,
author_name: author_name,
target_id: target_user_full_path,
target_type: 'User',
target_details: target_user_full_path,
ip_address: ip_address
)
end
end
end
describe 'license' do describe 'license' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
......
# frozen_string_literal: true
require 'spec_helper'
describe Users::DestroyService do
let(:current_user) { create(:admin) }
subject(:service) { described_class.new(current_user) }
describe '#execute' do
let(:user) { create(:user) }
describe 'audit events' do
before do
stub_licensed_features(admin_audit_log: true)
end
context 'when hard_delete' do
let(:hard_delete) { true }
it 'logs audit event' do
expected_message = "Removed user"
expect do
service.execute(user, hard_delete: hard_delete)
end.to change { AuditEvent.count }.by(1)
expect(AuditEvent.last.present.action).to eq(expected_message)
end
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