Commit 8601c224 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '299215-add-ability-to-find-dast-profiles-with-names' into 'master'

Add methods to find DAST profiles with given name

See merge request gitlab-org/gitlab!54235
parents 49ea048e 3a61a9d7
...@@ -9,6 +9,7 @@ class DastScannerProfilesFinder ...@@ -9,6 +9,7 @@ class DastScannerProfilesFinder
relation = DastScannerProfile.all relation = DastScannerProfile.all
relation = by_id(relation) relation = by_id(relation)
relation = by_project(relation) relation = by_project(relation)
relation = by_name(relation)
relation relation
end end
...@@ -27,4 +28,10 @@ class DastScannerProfilesFinder ...@@ -27,4 +28,10 @@ class DastScannerProfilesFinder
relation.project_id_in(params[:project_ids]) relation.project_id_in(params[:project_ids])
end end
def by_name(relation)
return relation unless params[:name]
relation.with_name(params[:name])
end
end end
...@@ -9,6 +9,7 @@ class DastSiteProfilesFinder ...@@ -9,6 +9,7 @@ class DastSiteProfilesFinder
relation = DastSiteProfile.with_dast_site_and_validation relation = DastSiteProfile.with_dast_site_and_validation
relation = by_id(relation) relation = by_id(relation)
relation = by_project(relation) relation = by_project(relation)
relation = by_name(relation)
relation relation
end end
...@@ -31,4 +32,10 @@ class DastSiteProfilesFinder ...@@ -31,4 +32,10 @@ class DastSiteProfilesFinder
relation.where(project_id: params[:project_id]) relation.where(project_id: params[:project_id])
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def by_name(relation)
return relation unless params[:name]
relation.with_name(params[:name])
end
end end
...@@ -7,6 +7,7 @@ class DastScannerProfile < ApplicationRecord ...@@ -7,6 +7,7 @@ class DastScannerProfile < ApplicationRecord
validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id }, presence: true validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id }, presence: true
scope :project_id_in, -> (project_ids) { where(project_id: project_ids) } scope :project_id_in, -> (project_ids) { where(project_id: project_ids) }
scope :with_name, -> (name) { where(name: name) }
enum scan_type: { enum scan_type: {
passive: 1, passive: 1,
......
...@@ -9,6 +9,7 @@ class DastSiteProfile < ApplicationRecord ...@@ -9,6 +9,7 @@ class DastSiteProfile < ApplicationRecord
validate :dast_site_project_id_fk validate :dast_site_project_id_fk
scope :with_dast_site_and_validation, -> { includes(dast_site: :dast_site_validation) } scope :with_dast_site_and_validation, -> { includes(dast_site: :dast_site_validation) }
scope :with_name, -> (name) { where(name: name) }
after_destroy :cleanup_dast_site after_destroy :cleanup_dast_site
......
...@@ -35,6 +35,14 @@ RSpec.describe DastScannerProfilesFinder do ...@@ -35,6 +35,14 @@ RSpec.describe DastScannerProfilesFinder do
end end
end end
context 'filter by name' do
let(:params) { { name: dast_scanner_profile1.name } }
it 'returns the matching dast_scanner_profiles' do
expect(subject).to contain_exactly(dast_scanner_profile1)
end
end
context 'when DastScannerProfile id is for a different project' do context 'when DastScannerProfile id is for a different project' do
let(:params) { { ids: [dast_scanner_profile1.id], project_ids: [dast_scanner_profile2.project.id] } } let(:params) { { ids: [dast_scanner_profile1.id], project_ids: [dast_scanner_profile2.project.id] } }
......
...@@ -49,6 +49,14 @@ RSpec.describe DastSiteProfilesFinder do ...@@ -49,6 +49,14 @@ RSpec.describe DastSiteProfilesFinder do
end end
end end
context 'filtering by name' do
let(:params) { { name: dast_site_profile1.name } }
it 'returns a single dast_site_profile' do
expect(subject).to contain_exactly(dast_site_profile1)
end
end
context 'when the dast_site_profile1 does not exist' do context 'when the dast_site_profile1 does not exist' do
let(:params) { { id: 0 } } let(:params) { { id: 0 } }
......
...@@ -24,6 +24,13 @@ RSpec.describe DastScannerProfile, type: :model do ...@@ -24,6 +24,13 @@ RSpec.describe DastScannerProfile, type: :model do
expect(result).to eq([subject]) expect(result).to eq([subject])
end end
end end
describe '.with_name' do
it 'returns the dast_scanner_profiles with given name' do
result = DastScannerProfile.with_name(subject.name)
expect(result).to eq([subject])
end
end
end end
describe 'full_scan_enabled?' do describe 'full_scan_enabled?' do
......
...@@ -51,6 +51,13 @@ RSpec.describe DastSiteProfile, type: :model do ...@@ -51,6 +51,13 @@ RSpec.describe DastSiteProfile, type: :model do
end end
end end
end end
describe '.with_name' do
it 'returns the dast_site_profiles with given name' do
result = DastSiteProfile.with_name(subject.name)
expect(result).to eq([subject])
end
end
end end
describe '#destroy!' do describe '#destroy!' do
......
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