Skip to content
Snippets Groups Projects
Commit 5907081a authored by Gregory Havenga's avatar Gregory Havenga :two: Committed by Gregory Havenga
Browse files

Enable non-default pagination for vulnerability feedback index

To prevent affecting the interface prior to the enablement of this,
the pagination only engages when a pagination parameter has been
passed to the api.

Changelog: added
EE: true
parent b4970e05
No related branches found
No related tags found
1 merge request!92339Implement Non-Default Pagination on the Projects::VulnerabilityFeedbackController#index
......@@ -15,9 +15,11 @@ class Projects::VulnerabilityFeedbackController < Projects::ApplicationControlle
urgency :default, [:create, :update, :destroy]
urgency :low, [:index]
PER_PAGE = 50
def index
# TODO: Move to finder or list service
@vulnerability_feedback = @project.vulnerability_feedback.with_associations
@vulnerability_feedback = optionally_paginate(@project.vulnerability_feedback.with_associations)
if params[:category].present?
@vulnerability_feedback = @vulnerability_feedback
......@@ -63,6 +65,16 @@ def destroy
private
def optionally_paginate(relation)
if params[:page] || params[:per_page]
page = !(params[:per_page].to_i < 0) ? params[:page].to_i : 0
per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : PER_PAGE
relation.paginate(page, per_page)
else
relation
end
end
def authorize_create_vulnerability_feedback!
type = vulnerability_feedback_params[:feedback_type]
......
......@@ -43,6 +43,7 @@ class Feedback < ApplicationRecord
scope :by_finding_uuid, -> (uuids) { where(finding_uuid: uuids) }
scope :by_project, -> (project) { where(project: project) }
scope :by_project_fingerprints, -> (project_fingerprints) { where(project_fingerprint: project_fingerprints) }
scope :paginate, -> (page, per_page) { offset(page).limit(per_page) }
scope :all_preloaded, -> do
preload(:author, :comment_author, :project, :issue, :merge_request, :pipeline)
......
......@@ -29,6 +29,54 @@
sign_in(user)
end
context 'when pagination paramters are given' do
let(:page) { 0 }
let(:per_page) { 10 }
let!(:additional_vulns) { create_list(:vulnerability_feedback, 60, :merge_request, :dependency_scanning, project: project, author: user, pipeline: pipeline_1, merge_request: merge_request) }
before do
list_feedbacks(page: page, per_page: per_page)
end
context 'when page and per page are given' do
it 'returns the desired quantity of vulnerability_feedbacks' do
expect(json_response.length).to eq 10
end
end
context 'when just page is given' do
let(:per_page) { nil }
it 'returns the default quantity of vulnerability_feedbacks' do
expect(json_response.length).to eq 50
end
end
context 'when just per_page is given' do
let(:page) { nil }
it 'returns the first page of the quantity desired of vulnerability_feedbacks' do
expect(json_response.length).to eq 10
end
end
context 'when an invalid page is given' do
let(:page) { "abc" }
it 'returns the first page of the default quantity of vulnerability_feedbacks' do
expect(json_response.length).to eq 10
end
end
context 'when an invalid per_page is given' do
let(:per_page) { "abc" }
it 'returns the first page of the default quantity of vulnerability_feedbacks' do
expect(json_response.length).to eq 50
end
end
end
it 'returns a successful 200 response' do
list_feedbacks
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment