Commit 4cf93146 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '220523-refactor-audit-controllers' into 'master'

Consolidate audit event variables in rendered views

Closes #220523

See merge request gitlab-org/gitlab!39600
parents 599b747f 342ca9b3
# frozen_string_literal: true # frozen_string_literal: true
class Admin::AuditLogsController < Admin::ApplicationController class Admin::AuditLogsController < Admin::ApplicationController
include Gitlab::Utils::StrongMemoize
include AuditEvents::EnforcesValidDateParams include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams include AuditEvents::AuditLogsParams
include AuditEvents::Sortable include AuditEvents::Sortable
...@@ -13,8 +14,8 @@ class Admin::AuditLogsController < Admin::ApplicationController ...@@ -13,8 +14,8 @@ class Admin::AuditLogsController < Admin::ApplicationController
PER_PAGE = 25 PER_PAGE = 25
def index def index
@events = audit_log_events @is_last_page = events.last_page?
@table_events = AuditEventSerializer.new.represent(@events) @events = AuditEventSerializer.new.represent(events)
@entity = case audit_logs_params[:entity_type] @entity = case audit_logs_params[:entity_type]
when 'User' when 'User'
...@@ -30,12 +31,18 @@ class Admin::AuditLogsController < Admin::ApplicationController ...@@ -30,12 +31,18 @@ class Admin::AuditLogsController < Admin::ApplicationController
private private
def audit_log_events def events
level = Gitlab::Audit::Levels::Instance.new strong_memoize(:events) do
events = AuditLogFinder.new(level: level, params: audit_logs_params).execute level = Gitlab::Audit::Levels::Instance.new
events = events.page(params[:page]).per(PER_PAGE).without_count events = AuditLogFinder
.new(level: level, params: audit_logs_params)
Gitlab::Audit::Events::Preloader.preload!(events) .execute
.page(params[:page])
.per(PER_PAGE)
.without_count
Gitlab::Audit::Events::Preloader.preload!(events)
end
end end
def check_license_admin_audit_log_available! def check_license_admin_audit_log_available!
......
# frozen_string_literal: true # frozen_string_literal: true
class Groups::AuditEventsController < Groups::ApplicationController class Groups::AuditEventsController < Groups::ApplicationController
include Gitlab::Utils::StrongMemoize
include AuditEvents::EnforcesValidDateParams include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams include AuditEvents::AuditLogsParams
include AuditEvents::Sortable include AuditEvents::Sortable
...@@ -14,21 +15,29 @@ class Groups::AuditEventsController < Groups::ApplicationController ...@@ -14,21 +15,29 @@ class Groups::AuditEventsController < Groups::ApplicationController
layout 'group_settings' layout 'group_settings'
def index def index
level = Gitlab::Audit::Levels::Group.new(group: group) @is_last_page = events.last_page?
# This is an interim change until we have proper API support within Audit Events @events = AuditEventSerializer.new.represent(events)
audit_params = transform_author_entity_type(audit_logs_params) end
events = AuditLogFinder private
.new(level: level, params: audit_params)
.execute
.page(params[:page])
.without_count
@events = Gitlab::Audit::Events::Preloader.preload!(events) def events
@table_events = AuditEventSerializer.new.represent(@events) strong_memoize(:events) do
level = Gitlab::Audit::Levels::Group.new(group: group)
events = AuditLogFinder
.new(level: level, params: audit_params)
.execute
.page(params[:page])
.without_count
Gitlab::Audit::Events::Preloader.preload!(events)
end
end end
private def audit_params
# This is an interim change until we have proper API support within Audit Events
transform_author_entity_type(audit_logs_params)
end
def transform_author_entity_type(params) def transform_author_entity_type(params)
return params unless params[:entity_type] == 'Author' return params unless params[:entity_type] == 'Author'
......
# frozen_string_literal: true # frozen_string_literal: true
class Projects::AuditEventsController < Projects::ApplicationController class Projects::AuditEventsController < Projects::ApplicationController
include Gitlab::Utils::StrongMemoize
include LicenseHelper include LicenseHelper
include AuditEvents::EnforcesValidDateParams include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams include AuditEvents::AuditLogsParams
...@@ -12,18 +13,8 @@ class Projects::AuditEventsController < Projects::ApplicationController ...@@ -12,18 +13,8 @@ class Projects::AuditEventsController < Projects::ApplicationController
layout 'project_settings' layout 'project_settings'
def index def index
level = Gitlab::Audit::Levels::Project.new(project: project) @is_last_page = events.last_page?
# This is an interim change until we have proper API support within Audit Events @events = AuditEventSerializer.new.represent(events)
audit_params = transform_author_entity_type(audit_logs_params)
events = AuditLogFinder
.new(level: level, params: audit_params)
.execute
.page(params[:page])
.without_count
@events = Gitlab::Audit::Events::Preloader.preload!(events)
@table_events = AuditEventSerializer.new.represent(@events)
end end
private private
...@@ -32,6 +23,24 @@ class Projects::AuditEventsController < Projects::ApplicationController ...@@ -32,6 +23,24 @@ class Projects::AuditEventsController < Projects::ApplicationController
render_404 unless @project.feature_available?(:audit_events) || LicenseHelper.show_promotions?(current_user) render_404 unless @project.feature_available?(:audit_events) || LicenseHelper.show_promotions?(current_user)
end end
def events
strong_memoize(:events) do
level = Gitlab::Audit::Levels::Project.new(project: project)
events = AuditLogFinder
.new(level: level, params: audit_params)
.execute
.page(params[:page])
.without_count
Gitlab::Audit::Events::Preloader.preload!(events)
end
end
def audit_params
# This is an interim change until we have proper API support within Audit Events
transform_author_entity_type(audit_logs_params)
end
def transform_author_entity_type(params) def transform_author_entity_type(params)
return params unless params[:entity_type] == 'Author' return params unless params[:entity_type] == 'Author'
......
- page_title _('Audit Log') - page_title _('Audit Log')
#js-audit-log-app{ data: { form_path: admin_audit_logs_path, #js-audit-log-app{ data: { form_path: admin_audit_logs_path,
events: @table_events.to_json, events: @events.to_json,
is_last_page: @events.last_page?.to_json, is_last_page: @is_last_page.to_json,
filter_qa_selector: 'admin_audit_log_filter', filter_qa_selector: 'admin_audit_log_filter',
table_qa_selector: 'admin_audit_log_table', table_qa_selector: 'admin_audit_log_table',
filter_token_options: admin_audit_event_tokens.to_json } } filter_token_options: admin_audit_event_tokens.to_json } }
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
%p.light= _('Group-level events in %{group_name} (no project-level events)') % { group_name: @group.name } %p.light= _('Group-level events in %{group_name} (no project-level events)') % { group_name: @group.name }
#js-group-audit-events-app{ data: { form_path: group_audit_events_path(@group), #js-group-audit-events-app{ data: { form_path: group_audit_events_path(@group),
events: @table_events.to_json, events: @events.to_json,
is_last_page: @events.last_page?.to_json, is_last_page: @is_last_page.to_json,
filter_qa_selector: 'group_audit_log_filter', filter_qa_selector: 'group_audit_log_filter',
table_qa_selector: 'group_audit_log_table', table_qa_selector: 'group_audit_log_table',
filter_token_options: group_audit_event_tokens(@group.id).to_json } } filter_token_options: group_audit_event_tokens(@group.id).to_json } }
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
= _('Events in %{project_path}') % { project_path: @project.full_path } = _('Events in %{project_path}') % { project_path: @project.full_path }
#js-project-audit-events-app{ data: { form_path: project_audit_events_path(@project), #js-project-audit-events-app{ data: { form_path: project_audit_events_path(@project),
events: @table_events.to_json, events: @events.to_json,
is_last_page: @events.last_page?.to_json, is_last_page: @is_last_page.to_json,
filter_qa_selector: 'project_audit_log_filter', filter_qa_selector: 'project_audit_log_filter',
table_qa_selector: 'project_audit_log_table', table_qa_selector: 'project_audit_log_table',
filter_token_options: project_audit_event_tokens(@project.full_path).to_json } } filter_token_options: project_audit_event_tokens(@project.full_path).to_json } }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Admin::AuditLogsController do RSpec.describe Admin::AuditLogsController do
let(:admin) { create(:admin) } let_it_be(:admin) { create(:admin) }
describe 'GET #index' do describe 'GET #index' do
before do before do
...@@ -19,9 +19,12 @@ RSpec.describe Admin::AuditLogsController do ...@@ -19,9 +19,12 @@ RSpec.describe Admin::AuditLogsController do
it 'paginates audit events, without casting a count query' do it 'paginates audit events, without casting a count query' do
create(:user_audit_event, created_at: 5.days.ago) create(:user_audit_event, created_at: 5.days.ago)
serializer = instance_spy(AuditEventSerializer)
allow(AuditEventSerializer).to receive(:new).and_return(serializer)
get :index, params: { 'entity_type': 'User' } get :index, params: { 'entity_type': 'User' }
expect(assigns(:events)).to be_kind_of(Kaminari::PaginatableWithoutCount) expect(serializer).to have_received(:represent).with(kind_of(Kaminari::PaginatableWithoutCount))
end end
end end
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Groups::AuditEventsController do RSpec.describe Groups::AuditEventsController do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:owner) { create(:user) } let_it_be(:owner) { create(:user) }
let(:group) { create(:group, :private) } let_it_be(:group) { create(:group, :private) }
let_it_be(:events) { create_list(:group_audit_event, 5, entity_id: group.id) }
describe 'GET #index' do describe 'GET #index' do
let(:sort) { nil } let(:sort) { nil }
...@@ -74,12 +75,11 @@ RSpec.describe Groups::AuditEventsController do ...@@ -74,12 +75,11 @@ RSpec.describe Groups::AuditEventsController do
it 'orders by id descending' do it 'orders by id descending' do
request request
expect(assigns(:events)).to eq(group.audit_events.order(id: :desc)) actual_event_ids = assigns(:events).map { |event| event[:id] }
end expected_event_ids = events.map(&:id).reverse
end
before do expect(actual_event_ids).to eq(expected_event_ids)
create_list(:group_audit_event, 5, entity_id: group.id) end
end end
context 'when no sort order is specified' do context 'when no sort order is specified' do
...@@ -98,7 +98,10 @@ RSpec.describe Groups::AuditEventsController do ...@@ -98,7 +98,10 @@ RSpec.describe Groups::AuditEventsController do
it 'orders by id ascending' do it 'orders by id ascending' do
request request
expect(assigns(:events)).to eq(group.audit_events.order(id: :asc)) actual_event_ids = assigns(:events).map { |event| event[:id] }
expected_event_ids = events.map(&:id)
expect(actual_event_ids).to eq(expected_event_ids)
end end
end end
...@@ -110,10 +113,19 @@ RSpec.describe Groups::AuditEventsController do ...@@ -110,10 +113,19 @@ RSpec.describe Groups::AuditEventsController do
end end
context 'pagination' do context 'pagination' do
it 'sets instance variables' do
request
expect(assigns(:is_last_page)).to be(true)
end
it 'paginates audit events, without casting a count query' do it 'paginates audit events, without casting a count query' do
serializer = instance_spy(AuditEventSerializer)
allow(AuditEventSerializer).to receive(:new).and_return(serializer)
request request
expect(assigns(:events)).to be_kind_of(Kaminari::PaginatableWithoutCount) expect(serializer).to have_received(:represent).with(kind_of(Kaminari::PaginatableWithoutCount))
end end
end end
end end
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Projects::AuditEventsController do RSpec.describe Projects::AuditEventsController do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:maintainer) { create(:user) } let_it_be(:maintainer) { create(:user) }
let(:project) { create(:project, :private) } let_it_be(:project) { create(:project, :private) }
let_it_be(:events) { create_list(:project_audit_event, 5, entity_id: project.id) }
describe 'GET #index' do describe 'GET #index' do
let(:sort) { nil } let(:sort) { nil }
...@@ -73,12 +74,11 @@ RSpec.describe Projects::AuditEventsController do ...@@ -73,12 +74,11 @@ RSpec.describe Projects::AuditEventsController do
it 'orders by id descending' do it 'orders by id descending' do
request request
expect(assigns(:events)).to eq(project.audit_events.order(id: :desc)) actual_event_ids = assigns(:events).map { |event| event[:id] }
end expected_event_ids = events.map(&:id).reverse
end
before do expect(actual_event_ids).to eq(expected_event_ids)
create_list(:project_audit_event, 5, entity_id: project.id) end
end end
context 'when no sort order is specified' do context 'when no sort order is specified' do
...@@ -97,7 +97,10 @@ RSpec.describe Projects::AuditEventsController do ...@@ -97,7 +97,10 @@ RSpec.describe Projects::AuditEventsController do
it 'orders by id ascending' do it 'orders by id ascending' do
request request
expect(assigns(:events)).to eq(project.audit_events.order(id: :asc)) actual_event_ids = assigns(:events).map { |event| event[:id] }
expected_event_ids = events.map(&:id)
expect(actual_event_ids).to eq(expected_event_ids)
end end
end end
...@@ -110,10 +113,19 @@ RSpec.describe Projects::AuditEventsController do ...@@ -110,10 +113,19 @@ RSpec.describe Projects::AuditEventsController do
end end
context 'pagination' do context 'pagination' do
it 'sets instance variables' do
request
expect(assigns(:is_last_page)).to be(true)
end
it 'paginates audit events, without casting a count query' do it 'paginates audit events, without casting a count query' do
serializer = instance_spy(AuditEventSerializer)
allow(AuditEventSerializer).to receive(:new).and_return(serializer)
request request
expect(assigns(:events)).to be_kind_of(Kaminari::PaginatableWithoutCount) expect(serializer).to have_received(:represent).with(kind_of(Kaminari::PaginatableWithoutCount))
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