Commit 92120a0f authored by Philip Cunningham's avatar Philip Cunningham Committed by Matthias Käppler

Add Dast::ProfilesFinder to retreive dast_profiles

Adds new finder for DAST saved scans feature.
parent 30cf2c96
# frozen_string_literal: true
module Dast
class ProfilesFinder
DEFAULT_SORT = { id: :asc }.freeze
def initialize(params = {})
@params = params
end
def execute
relation = default_relation
relation = by_id(relation)
relation = by_project(relation)
sort(relation)
end
private
attr_reader :params
# rubocop: disable CodeReuse/ActiveRecord
def default_relation
Dast::Profile.limit(100)
end
# rubocop: enable CodeReuse/ActiveRecord
def by_id(relation)
return relation if params[:id].nil?
relation.id_in(params[:id])
end
def by_project(relation)
return relation if params[:project_id].nil?
relation.by_project_id(params[:project_id])
end
# rubocop: disable CodeReuse/ActiveRecord
def sort(relation)
relation.order(DEFAULT_SORT)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
...@@ -14,6 +14,10 @@ module Dast ...@@ -14,6 +14,10 @@ module Dast
validate :project_ids_match validate :project_ids_match
scope :by_project_id, -> (project_id) do
where(project_id: project_id)
end
private private
def project_ids_match def project_ids_match
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
FactoryBot.define do FactoryBot.define do
factory :dast_scanner_profile do factory :dast_scanner_profile do
name { FFaker::Product.product_name } sequence :name do |i|
"#{FFaker::Product.product_name.truncate(200)} - #{i}"
end
before(:create) do |dast_scanner_profile| before(:create) do |dast_scanner_profile|
dast_scanner_profile.project ||= FactoryBot.create(:project) dast_scanner_profile.project ||= FactoryBot.create(:project)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Dast::ProfilesFinder do
let_it_be(:project1) { create(:project) }
let_it_be(:project2) { create(:project) }
let_it_be(:dast_profile1) { create(:dast_profile, project: project1) }
let_it_be(:dast_profile2) { create(:dast_profile, project: project2) }
let_it_be(:dast_profile3) { create(:dast_profile, project: project1) }
let(:params) { {} }
subject do
described_class.new(params).execute
end
describe '#execute' do
it 'returns dast_profiles limited to 100 records' do
aggregate_failures do
expect(Dast::Profile).to receive(:limit).with(100).and_call_original
expect(subject).to contain_exactly(dast_profile1, dast_profile2, dast_profile3)
end
end
context 'filtering by id' do
let(:params) { { id: dast_profile1.id } }
it 'returns the matching dast_profile' do
expect(subject).to contain_exactly(dast_profile1)
end
end
context 'filtering by project_id' do
let(:params) { { project_id: project1.id } }
it 'returns the matching dast_profiles' do
expect(subject).to contain_exactly(dast_profile1, dast_profile3)
end
end
context 'when the dast_profile does not exist' do
let(:params) { { project_id: 0 } }
it 'returns an empty relation' do
expect(subject).to be_empty
end
end
context 'sorting' do
it 'orders by id asc by default' do
expect(subject).to be_sorted(:id, :asc)
end
end
end
end
...@@ -48,4 +48,19 @@ RSpec.describe Dast::Profile, type: :model do ...@@ -48,4 +48,19 @@ RSpec.describe Dast::Profile, type: :model do
end end
end end
end end
describe 'scopes' do
describe 'by_project_id' do
it 'includes the correct records' do
another_dast_profile = create(:dast_profile)
result = described_class.by_project_id(subject.project_id)
aggregate_failures do
expect(result).to include(subject)
expect(result).not_to include(another_dast_profile)
end
end
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