Commit 3375f1dd authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'philipcunningham-add-ability-to-show-most-recent-validations' into 'master'

Filter by most recent in DastSiteValidationsFinder

See merge request gitlab-org/gitlab!49663
parents 585f88b4 b62e5009
......@@ -10,6 +10,7 @@ class DastSiteValidationsFinder
def execute
relation = DastSiteValidation.all
relation = by_most_recent(relation)
relation = by_project(relation)
relation = by_url_base(relation)
relation = by_state(relation)
......@@ -21,6 +22,12 @@ class DastSiteValidationsFinder
attr_reader :params
def by_most_recent(relation)
return relation unless params[:most_recent]
relation.by_most_recent
end
def by_project(relation)
return relation if params[:project_id].nil?
......
......@@ -12,7 +12,7 @@ module Resolvers
def resolve(**args)
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
private
......
......@@ -17,6 +17,10 @@ class DastSiteValidation < ApplicationRecord
where(url_base: url_base)
end
scope :by_most_recent, -> do
where(id: select('MAX(id) AS id').group(:url_base))
end
before_create :set_normalized_url_base
enum validation_strategy: { text_file: 0, header: 1 }
......
......@@ -18,11 +18,17 @@ RSpec.describe DastSiteValidationsFinder do
expect(subject).to eq([dast_site_validation_3, dast_site_validation_2, dast_site_validation_1])
end
context 'filtering by url_base' do
let(:params) { { url_base: dast_site_validation_1.url_base } }
context 'filtering by most_recent' do
let(:params) { { most_recent: true } }
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
......@@ -32,13 +38,29 @@ RSpec.describe DastSiteValidationsFinder do
it 'returns the matching dast_site_validations' do
expect(subject).to eq([dast_site_validation_2])
end
context 'when url_base is for a different project' do
let(:params) { { project_id: dast_site_validation_2.project.id, url_base: dast_site_validation_1.url_base } }
it 'returns an empty relation' do
expect(subject).to be_empty
end
end
end
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 } }
context 'filtering by url_base' do
let(:params) { { url_base: dast_site_validation_1.url_base } }
it 'returns an empty relation' do
expect(subject).to be_empty
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
......
......@@ -37,7 +37,7 @@ RSpec.describe Resolvers::DastSiteValidationResolver do
it { is_expected.to contain_exactly(dast_site_validation3, dast_site_validation1) }
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] } }
it { is_expected.to contain_exactly(dast_site_validation2) }
......
......@@ -50,7 +50,7 @@ RSpec.describe DastSiteValidation, type: :model do
describe 'by_url_base' 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
it 'includes the correct records' do
......@@ -59,6 +59,18 @@ RSpec.describe DastSiteValidation, type: :model do
expect(result).not_to include(another_dast_site_validation)
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
describe 'enums' do
......
......@@ -6,11 +6,14 @@ RSpec.describe 'Query.project(fullPath).dastSiteValidations' do
include GraphqlHelpers
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_validation1) { create(:dast_site_validation, dast_site_token: dast_site_token) }
let_it_be(:dast_site_validation2) { create(:dast_site_validation, dast_site_token: dast_site_token) }
let_it_be(:dast_site_validation3) { create(:dast_site_validation, dast_site_token: dast_site_token) }
let_it_be(:dast_site_validation4) { create(:dast_site_validation, dast_site_token: dast_site_token) }
let_it_be(:dast_site_token1) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_token2) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_token3) { create(:dast_site_token, project: project, url: generate(:url)) }
let_it_be(:dast_site_token4) { create(:dast_site_token, project: project, url: generate(:url)) }
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) }
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