Commit ad25c574 authored by Victor Zagorodny's avatar Victor Zagorodny

Move pagination to API level where it should be

parent d75b5723
......@@ -6,23 +6,17 @@
#
# Arguments:
# project: a Project to query for Vulnerabilities
# params:
# page: Integer
# per_page: Integer
module Security
class VulnerabilitiesFinder
attr_reader :project
attr_reader :page, :per_page
def initialize(project, params = {})
def initialize(project)
@project = project
@page = params[:page] || 1
@per_page = params[:per_page] || 20
end
def execute
project.vulnerabilities.page(page).per(per_page)
project.vulnerabilities
end
end
end
......@@ -7,8 +7,8 @@ module API
helpers ::API::Helpers::VulnerabilityFindingsHelpers
helpers do
def vulnerabilities_by(project, params)
Security::VulnerabilitiesFinder.new(project, params).execute
def vulnerabilities_by(project)
Security::VulnerabilitiesFinder.new(project).execute
end
end
......@@ -36,7 +36,7 @@ module API
authorize! :read_project_security_dashboard, user_project
vulnerabilities = paginate(
vulnerabilities_by(user_project, declared_params)
vulnerabilities_by(user_project)
)
present vulnerabilities, with: VulnerabilityEntity
......
......@@ -4,11 +4,10 @@ require 'spec_helper'
describe Security::VulnerabilitiesFinder do
let(:project) { create(:project, :with_vulnerabilities) }
let(:params) { { page: 2, per_page: 1 } }
subject { described_class.new(project, params).execute }
subject { described_class.new(project).execute }
it 'returns vulnerabilities of a project and respects pagination params' do
expect(subject).to contain_exactly(project.vulnerabilities.drop(1).take(1).first)
expect(subject).to match_array(project.vulnerabilities)
end
end
......@@ -26,6 +26,13 @@ describe API::Vulnerabilities do
expect(response).to match_response_schema('vulnerability_list', dir: 'ee')
expect(response.headers['X-Total']).to eq project.vulnerabilities.count.to_s
end
it 'paginates the vulnerabilities according to the pagination params' do
get api("#{project_vulnerabilities_path}?page=2&per_page=1", user)
expect(response).to have_gitlab_http_status(200)
expect(json_response.map { |v| v['id'] }).to contain_exactly(project.vulnerabilities.drop(1).take(1).first.id)
end
end
it_behaves_like 'forbids access to vulnerability-like endpoint in expected cases'
......
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