Commit 10b7cc4d authored by Jiaan Louw's avatar Jiaan Louw Committed by Kerri Miller

Add default date range for audit events

This sets the default date range filter for audit events
to this month, startint at the 1st day of the month and
ending at the current date. It also sets the created_before
paramater time to the end_of_day time to include events
that occured on that date.
parent 3df1baec
......@@ -5,6 +5,7 @@ class Admin::AuditLogsController < Admin::ApplicationController
include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams
include AuditEvents::Sortable
include AuditEvents::DateRange
include Analytics::UniqueVisitsHelper
before_action :check_license_admin_audit_log_available!
......
# frozen_string_literal: true
module AuditEvents
module DateRange
extend ActiveSupport::Concern
included do
before_action :set_date_range, only: [:index]
end
private
def set_date_range
params[:created_before] = params[:created_before].nil? ? Date.current.end_of_day : Date.parse(params[:created_before]).end_of_day
params[:created_after] = Date.current.beginning_of_month unless params[:created_after]
end
end
end
......@@ -5,6 +5,7 @@ class Groups::AuditEventsController < Groups::ApplicationController
include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams
include AuditEvents::Sortable
include AuditEvents::DateRange
include Analytics::UniqueVisitsHelper
before_action :authorize_admin_group!
......
......@@ -6,6 +6,7 @@ class Projects::AuditEventsController < Projects::ApplicationController
include AuditEvents::EnforcesValidDateParams
include AuditEvents::AuditLogsParams
include AuditEvents::Sortable
include AuditEvents::DateRange
before_action :authorize_admin_project!
before_action :check_audit_events_available!
......
---
title: Add default date range for audit events
merge_request: 42986
author:
type: changed
......@@ -26,7 +26,7 @@ RSpec.describe Groups::AuditEventsController do
context 'when audit_events feature is available' do
let(:level) { Gitlab::Audit::Levels::Group.new(group: group) }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', entity_type: '', entity_id: '').permit! }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', entity_type: '', entity_id: '', created_after: Date.current.beginning_of_month, created_before: Date.current.end_of_day).permit! }
before do
stub_licensed_features(audit_events: true)
......@@ -64,7 +64,7 @@ RSpec.describe Groups::AuditEventsController do
context 'when the author entity type is specified' do
let(:entity_type) { 'Author' }
let(:entity_id) { 1 }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', author_id: '1').permit! }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', author_id: '1', created_after: Date.current.beginning_of_month, created_before: Date.current.end_of_day).permit! }
it_behaves_like 'AuditLogFinder params'
end
......
......@@ -25,7 +25,7 @@ RSpec.describe Projects::AuditEventsController do
context 'when audit_events feature is available' do
let(:level) { Gitlab::Audit::Levels::Project.new(project: project) }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', entity_type: '', entity_id: '').permit! }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', entity_type: '', entity_id: '', created_after: Date.current.beginning_of_month, created_before: Date.current.end_of_day).permit! }
before do
stub_licensed_features(audit_events: true)
......@@ -63,7 +63,7 @@ RSpec.describe Projects::AuditEventsController do
context 'when the author entity type is specified' do
let(:entity_type) { 'Author' }
let(:entity_id) { 1 }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', author_id: '1').permit! }
let(:audit_logs_params) { ActionController::Parameters.new(sort: '', author_id: '1', created_after: Date.current.beginning_of_month, created_before: Date.current.end_of_day).permit! }
it_behaves_like 'AuditLogFinder params'
end
......
......@@ -105,7 +105,7 @@ RSpec.describe 'Admin::AuditLogs', :js do
describe 'filter by date' do
let_it_be(:audit_event_1) { create(:user_audit_event, created_at: 5.days.ago) }
let_it_be(:audit_event_2) { create(:user_audit_event, created_at: 3.days.ago) }
let_it_be(:audit_event_3) { create(:user_audit_event, created_at: 1.day.ago) }
let_it_be(:audit_event_3) { create(:user_audit_event, created_at: Date.current) }
let_it_be(:events_path) { :admin_audit_logs_path }
let_it_be(:entity) { nil }
......
......@@ -69,7 +69,7 @@ RSpec.describe 'Groups > Audit Events', :js do
describe 'filter by date' do
let!(:audit_event_1) { create(:group_audit_event, entity_type: 'Group', entity_id: group.id, created_at: 5.days.ago) }
let!(:audit_event_2) { create(:group_audit_event, entity_type: 'Group', entity_id: group.id, created_at: 3.days.ago) }
let!(:audit_event_3) { create(:group_audit_event, entity_type: 'Group', entity_id: group.id, created_at: 1.day.ago) }
let!(:audit_event_3) { create(:group_audit_event, entity_type: 'Group', entity_id: group.id, created_at: Date.current) }
let!(:events_path) { :group_audit_events_path }
let!(:entity) { group }
......
......@@ -166,7 +166,7 @@ RSpec.describe 'Projects > Audit Events', :js do
describe 'filter by date' do
let!(:audit_event_1) { create(:project_audit_event, entity_type: 'Project', entity_id: project.id, created_at: 5.days.ago) }
let!(:audit_event_2) { create(:project_audit_event, entity_type: 'Project', entity_id: project.id, created_at: 3.days.ago) }
let!(:audit_event_3) { create(:project_audit_event, entity_type: 'Project', entity_id: project.id, created_at: 1.day.ago) }
let!(:audit_event_3) { create(:project_audit_event, entity_type: 'Project', entity_id: project.id, created_at: Date.current) }
let!(:events_path) { :project_audit_events_path }
let!(:entity) { project }
......
......@@ -11,8 +11,8 @@ RSpec.shared_examples_for 'audit events date filter' do
expect(page).not_to have_content(audit_event_3.present.date)
end
it 'shows only yesterday events' do
visit method(events_path).call(entity, created_after: 2.days.ago.to_date)
it 'shows only today\'s event' do
visit method(events_path).call(entity, created_after: 1.day.ago.to_date, created_before: Date.current.to_date)
find('.audit-log-table td', match: :first)
......
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