Commit d72d9ab1 authored by manojmj's avatar manojmj

Specify sort order explicitly for Group and Project audit events

This change adds explicit sort order of `id: :desc`,
while displaying Project and Group Audit Events.
This is accomplished by using the `LogFinder` service
to get audit_event records.
parent ba44545a
---
title: Specify sort order explicitly for Group and Project audit events
merge_request: 17739
author:
type: fixed
......@@ -7,6 +7,6 @@ class Groups::AuditEventsController < Groups::ApplicationController
layout 'group_settings'
def index
@events = group.audit_events.page(params[:page])
@events = LogFinder.new(entity_type: group.class.name, entity_id: group.id).execute.page(params[:page])
end
end
......@@ -9,7 +9,7 @@ class Projects::AuditEventsController < Projects::ApplicationController
layout 'project_settings'
def index
@events = project.audit_events.page(params[:page])
@events = LogFinder.new(entity_type: project.class.name, entity_id: project.id).execute.page(params[:page])
end
def check_audit_events_available!
......
# frozen_string_literal: true
require 'spec_helper'
describe Groups::AuditEventsController do
let(:user) { create(:user) }
let(:owner) { create(:user) }
let(:group) { create(:group, :private) }
describe 'GET #index' do
let(:request) do
get :index, params: { group_id: group.to_param }
end
context 'authorized' do
before do
group.add_owner(owner)
sign_in(owner)
end
context 'when audit_events feature is available' do
before do
stub_licensed_features(audit_events: true)
end
it 'renders index with 200 status code' do
expect(LogFinder).to receive(:new).with(entity_type: ::Group.name, entity_id: group.id).and_call_original
request
expect(response).to have_gitlab_http_status(200)
expect(response).to render_template(:index)
end
context 'ordering' do
before do
create_list(:group_audit_event, 5, entity_id: group.id)
end
it 'orders by id descending' do
request
expect(assigns(:events)).to eq(group.audit_events.order(id: :desc))
end
end
end
context 'when audit_events feature is not available' do
before do
stub_licensed_features(audit_events: false)
end
it 'renders 404' do
request
expect(response).to have_gitlab_http_status(404)
end
end
end
context 'unauthorized' do
before do
stub_licensed_features(audit_events: true)
sign_in(user)
end
it 'renders 404' do
request
expect(response).to have_gitlab_http_status(404)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Projects::AuditEventsController do
let(:user) { create(:user) }
let(:maintainer) { create(:user) }
let(:project) { create(:project, :private) }
describe 'GET #index' do
let(:request) do
get :index, params: { project_id: project.to_param, namespace_id: project.namespace.to_param }
end
context 'authorized' do
before do
project.add_maintainer(maintainer)
sign_in(maintainer)
end
context 'when audit_events feature is available' do
before do
stub_licensed_features(audit_events: true)
end
it 'renders index with 200 status code' do
expect(LogFinder).to receive(:new).with(entity_type: ::Project.name, entity_id: project.id).and_call_original
request
expect(response).to have_gitlab_http_status(200)
expect(response).to render_template(:index)
end
context 'ordering' do
before do
create_list(:project_audit_event, 5, entity_id: project.id)
end
it 'orders by id descending' do
request
expect(assigns(:events)).to eq(project.audit_events.order(id: :desc))
end
end
end
context 'when audit_events feature is not available' do
before do
stub_licensed_features(audit_events: false)
end
it 'renders 404' do
request
expect(response).to have_gitlab_http_status(404)
end
end
end
context 'unauthorized' do
before do
stub_licensed_features(audit_events: true)
sign_in(user)
end
it 'renders 404' do
request
expect(response).to have_gitlab_http_status(404)
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