Commit ab47a478 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '220322-refactor-target-type-3' into 'master'

Add parallel persistance to AuditEvent.target_type

See merge request gitlab-org/gitlab!39462
parents 962e30dc c283cd71
......@@ -5,7 +5,7 @@ class AuditEvent < ApplicationRecord
include IgnorableColumns
include BulkInsertSafe
PARALLEL_PERSISTENCE_COLUMNS = [:author_name, :entity_path, :target_details].freeze
PARALLEL_PERSISTENCE_COLUMNS = [:author_name, :entity_path, :target_details, :target_type].freeze
ignore_column :type, remove_with: '13.6', remove_after: '2020-11-22'
ignore_column :updated_at, remove_with: '13.4', remove_after: '2020-09-22'
......
......@@ -15,6 +15,7 @@ module EE
old_access_level = @details[:old_access_level]
user_id = member.id
user_name = member.user ? member.user.name : 'Deleted User'
target_type = 'User'
@details =
case action
......@@ -23,7 +24,7 @@ module EE
remove: "user_access",
author_name: @author.name,
target_id: user_id,
target_type: "User",
target_type: target_type,
target_details: user_name
}
when :expired
......@@ -31,7 +32,7 @@ module EE
remove: "user_access",
author_name: member.created_by ? member.created_by.name : 'Deleted User',
target_id: user_id,
target_type: "User",
target_type: target_type,
target_details: user_name,
system_event: true,
reason: "access expired on #{member.expires_at}"
......@@ -42,7 +43,7 @@ module EE
as: ::Gitlab::Access.options_with_owner.key(member.access_level.to_i),
author_name: @author.name,
target_id: user_id,
target_type: "User",
target_type: target_type,
target_details: user_name
}
when :update, :override
......@@ -54,7 +55,7 @@ module EE
expiry_to: member.expires_at,
author_name: @author.name,
target_id: user_id,
target_type: "User",
target_type: target_type,
target_details: user_name
}
end
......
......@@ -16,7 +16,7 @@ RSpec.describe AuditEvent, type: :model do
describe 'callbacks' do
context 'parallel_persist' do
let_it_be(:details) do
{ author_name: 'Kungfu Panda', entity_path: 'gitlab-org/gitlab', target_details: 'Project X' }
{ author_name: 'Kungfu Panda', entity_path: 'gitlab-org/gitlab', target_details: 'Project X', target_type: 'User' }
end
let_it_be(:event) { create(:project_audit_event, details: details, entity_path: nil, target_details: nil) }
......
......@@ -42,6 +42,11 @@ RSpec.describe AuditEventService do
end
end
it 'generates a system event' do
expect(event[:details][:target_type]).to eq('User')
expect(event.target_type).to eq('User')
end
context 'updating membership' do
let(:service) do
described_class.new(user, project, {
......@@ -277,6 +282,22 @@ RSpec.describe AuditEventService do
end
end
describe '#for_project_group_link' do
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:group) { create(:group) }
let_it_be(:link) { create(:project_group_link, group: group, project: project) }
let(:options) { { action: :create } }
subject(:event) { described_class.new(current_user, project, options).for_project_group_link(link).security_event }
it 'sets the target_type attribute' do
expect(event.details[:target_type]).to eq('Project')
expect(event.target_type).to eq('Project')
end
end
describe '#for_user' do
let(:author_name) { 'Administrator' }
let(:current_user) { User.new(name: author_name) }
......@@ -351,6 +372,53 @@ RSpec.describe AuditEventService do
end
end
describe '#for_project' do
let_it_be(:current_user) { create(:user, name: 'Test User') }
let_it_be(:project) { create(:project) }
let(:action) { :destroy }
let(:options) { { action: action } }
subject(:event) { described_class.new(current_user, project, options).for_project.security_event }
it 'sets the details attribute' do
expect(event.details).to eq(
remove: 'project',
author_name: 'Test User',
target_id: project.full_path,
target_type: 'Project',
target_details: project.full_path
)
end
it 'sets the target_type column' do
expect(event.target_type).to eq('Project')
end
end
describe '#for_group' do
let_it_be(:user) { create(:user, name: 'Test User') }
let_it_be(:group) { create(:group) }
let(:action) { :destroy }
let(:options) { { action: action } }
let(:service) { described_class.new(user, group, options).for_group }
subject(:event) { service.security_event }
it 'sets the details attribute' do
expect(event.details).to eq(
remove: 'group',
author_name: 'Test User',
target_id: group.full_path,
target_type: 'Group',
target_details: group.full_path
)
end
it 'stores target_type in a database column' do
expect(event.target_type).to eq('Group')
end
end
describe 'license' do
let(:event) { service.for_project.security_event }
......
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