Commit b62e5009 authored by Philip Cunningham's avatar Philip Cunningham

Filter by most recent in DastSiteValidationsFinder

Returns only the most recent validation by url_base.
parent 2b2f29a8
...@@ -10,6 +10,7 @@ class DastSiteValidationsFinder ...@@ -10,6 +10,7 @@ class DastSiteValidationsFinder
def execute def execute
relation = DastSiteValidation.all relation = DastSiteValidation.all
relation = by_most_recent(relation)
relation = by_project(relation) relation = by_project(relation)
relation = by_url_base(relation) relation = by_url_base(relation)
relation = by_state(relation) relation = by_state(relation)
...@@ -21,6 +22,12 @@ class DastSiteValidationsFinder ...@@ -21,6 +22,12 @@ class DastSiteValidationsFinder
attr_reader :params attr_reader :params
def by_most_recent(relation)
return relation unless params[:most_recent]
relation.by_most_recent
end
def by_project(relation) def by_project(relation)
return relation if params[:project_id].nil? return relation if params[:project_id].nil?
......
...@@ -12,7 +12,7 @@ module Resolvers ...@@ -12,7 +12,7 @@ module Resolvers
def resolve(**args) def resolve(**args)
return DastSiteValidation.none unless allowed? return DastSiteValidation.none unless allowed?
DastSiteValidationsFinder.new(project_id: project.id, url_base: args[:normalized_target_urls]).execute DastSiteValidationsFinder.new(project_id: project.id, url_base: args[:normalized_target_urls], most_recent: true).execute
end end
private private
......
...@@ -17,6 +17,10 @@ class DastSiteValidation < ApplicationRecord ...@@ -17,6 +17,10 @@ class DastSiteValidation < ApplicationRecord
where(url_base: url_base) where(url_base: url_base)
end end
scope :by_most_recent, -> do
where(id: select('MAX(id) AS id').group(:url_base))
end
before_create :set_normalized_url_base before_create :set_normalized_url_base
enum validation_strategy: { text_file: 0, header: 1 } enum validation_strategy: { text_file: 0, header: 1 }
......
...@@ -18,11 +18,17 @@ RSpec.describe DastSiteValidationsFinder do ...@@ -18,11 +18,17 @@ RSpec.describe DastSiteValidationsFinder do
expect(subject).to eq([dast_site_validation_3, dast_site_validation_2, dast_site_validation_1]) expect(subject).to eq([dast_site_validation_3, dast_site_validation_2, dast_site_validation_1])
end end
context 'filtering by url_base' do context 'filtering by most_recent' do
let(:params) { { url_base: dast_site_validation_1.url_base } } let(:params) { { most_recent: true } }
it 'returns the matching dast_site_validations' do it 'returns the matching dast_site_validations' do
expect(subject).to eq([dast_site_validation_3, dast_site_validation_1]) expect(subject).to eq([dast_site_validation_3, dast_site_validation_2])
end
it 'allows the results to be easily re-ordered' do
relation = subject.reorder(id: :asc)
expect(relation).to eq([dast_site_validation_2, dast_site_validation_3])
end end
end end
...@@ -32,14 +38,30 @@ RSpec.describe DastSiteValidationsFinder do ...@@ -32,14 +38,30 @@ RSpec.describe DastSiteValidationsFinder do
it 'returns the matching dast_site_validations' do it 'returns the matching dast_site_validations' do
expect(subject).to eq([dast_site_validation_2]) expect(subject).to eq([dast_site_validation_2])
end end
end
context 'when url_base is for a different project' do context 'when url_base is for a different project' do
let(:params) { { url_base: dast_site_validation_1.url_base, project_id: dast_site_validation_2.project.id } } let(:params) { { project_id: dast_site_validation_2.project.id, url_base: dast_site_validation_1.url_base } }
it 'returns an empty relation' do it 'returns an empty relation' do
expect(subject).to be_empty expect(subject).to be_empty
end end
end end
end end
context 'filtering by url_base' do
let(:params) { { url_base: dast_site_validation_1.url_base } }
it 'returns the matching dast_site_validations' do
expect(subject).to eq([dast_site_validation_3, dast_site_validation_1])
end
end
context 'filtering by *' do
let(:params) { { project_id: dast_site_validation_1.project.id, url_base: dast_site_validation_1.url_base, most_recent: true } }
it 'returns the matching dast_site_validations' do
expect(subject).to eq([dast_site_validation_3])
end
end
end
end end
...@@ -37,7 +37,7 @@ RSpec.describe Resolvers::DastSiteValidationResolver do ...@@ -37,7 +37,7 @@ RSpec.describe Resolvers::DastSiteValidationResolver do
it { is_expected.to contain_exactly(dast_site_validation3, dast_site_validation1) } it { is_expected.to contain_exactly(dast_site_validation3, dast_site_validation1) }
end end
context 'when one normalized_target_urls is specified' do context 'when one normalized_target_url is specified' do
let(:args) { { normalized_target_urls: [dast_site_validation2.url_base] } } let(:args) { { normalized_target_urls: [dast_site_validation2.url_base] } }
it { is_expected.to contain_exactly(dast_site_validation2) } it { is_expected.to contain_exactly(dast_site_validation2) }
......
...@@ -50,7 +50,7 @@ RSpec.describe DastSiteValidation, type: :model do ...@@ -50,7 +50,7 @@ RSpec.describe DastSiteValidation, type: :model do
describe 'by_url_base' do describe 'by_url_base' do
let(:more_dast_site_validations) do let(:more_dast_site_validations) do
create_list(:dast_site_validation, 5, dast_site_token: subject.dast_site_token).prepend(subject) create_list(:dast_site_validation, 5, dast_site_token: subject.dast_site_token)
end end
it 'includes the correct records' do it 'includes the correct records' do
...@@ -59,6 +59,18 @@ RSpec.describe DastSiteValidation, type: :model do ...@@ -59,6 +59,18 @@ RSpec.describe DastSiteValidation, type: :model do
expect(result).not_to include(another_dast_site_validation) expect(result).not_to include(another_dast_site_validation)
end end
end end
describe 'by_most_recent' do
let(:more_dast_site_validations) do
create_list(:dast_site_validation, 5, dast_site_token: subject.dast_site_token)
end
it 'includes the correct records' do
result = described_class.by_most_recent
expect(result).to contain_exactly(another_dast_site_validation, more_dast_site_validations.last)
end
end
end end
describe 'enums' do describe 'enums' do
......
...@@ -6,11 +6,14 @@ RSpec.describe 'Query.project(fullPath).dastSiteValidations' do ...@@ -6,11 +6,14 @@ RSpec.describe 'Query.project(fullPath).dastSiteValidations' do
include GraphqlHelpers include GraphqlHelpers
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:dast_site_token) { create(:dast_site_token, project: project, url: generate(:url)) } let_it_be(:dast_site_token1) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_validation1) { create(:dast_site_validation, dast_site_token: dast_site_token) } let_it_be(:dast_site_token2) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_validation2) { create(:dast_site_validation, dast_site_token: dast_site_token) } let_it_be(:dast_site_token3) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_validation3) { create(:dast_site_validation, dast_site_token: dast_site_token) } let_it_be(:dast_site_token4) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_validation4) { create(:dast_site_validation, dast_site_token: dast_site_token) } let_it_be(:dast_site_validation1) { create(:dast_site_validation, dast_site_token: dast_site_token1) }
let_it_be(:dast_site_validation2) { create(:dast_site_validation, dast_site_token: dast_site_token2) }
let_it_be(:dast_site_validation3) { create(:dast_site_validation, dast_site_token: dast_site_token3) }
let_it_be(:dast_site_validation4) { create(:dast_site_validation, dast_site_token: dast_site_token4) }
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
subject do subject 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