Commit b71fedce authored by Philip Cunningham's avatar Philip Cunningham

Use calculated status for DastSiteProfile query

Replace stubbed field with validation status from the database.
parent 479c7bf2
......@@ -6,7 +6,7 @@ class DastSiteProfilesFinder
end
def execute
relation = DastSiteProfile.with_dast_site
relation = DastSiteProfile.with_dast_site_and_validation
relation = by_id(relation)
relation = by_project(relation)
relation
......@@ -20,7 +20,7 @@ class DastSiteProfilesFinder
def by_id(relation)
return relation if params[:id].nil?
relation.where(id: params[:id])
relation.where(id: params[:id]).limit(1)
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -100,7 +100,7 @@ module EE
::Types::DastSiteProfileType.connection_type,
null: true,
description: 'DAST Site Profiles associated with the project',
resolve: -> (obj, _args, _ctx) { obj.dast_site_profiles.with_dast_site }
resolve: -> (obj, _args, _ctx) { DastSiteProfilesFinder.new(project_id: obj.id).execute }
field :cluster_agent,
::Types::Clusters::AgentType,
......
......@@ -28,6 +28,6 @@ module Types
field :validation_status, Types::DastSiteProfileValidationStatusEnum, null: true,
description: 'The current validation status of the site profile',
resolve: -> (_obj, _args, _ctx) { Types::DastSiteProfileValidationStatusEnum.enum['pending_validation'] }
resolve: -> (obj, _args, _ctx) { "#{obj.status.upcase}_VALIDATION" }
end
end
......@@ -8,10 +8,18 @@ class DastSiteProfile < ApplicationRecord
validates :project_id, :dast_site_id, presence: true
validate :dast_site_project_id_fk
scope :with_dast_site, -> { includes(:dast_site) }
scope :with_dast_site_and_validation, -> { includes(dast_site: :dast_site_validation) }
after_destroy :cleanup_dast_site
delegate :dast_site_validation, to: :dast_site, allow_nil: true
def status
return DastSiteValidation::INITIAL_STATE unless dast_site_validation
dast_site_validation.state
end
private
def cleanup_dast_site
......
......@@ -21,7 +21,9 @@ class DastSiteValidation < ApplicationRecord
"#{url_base}/#{url_path}"
end
state_machine :state, initial: :pending do
INITIAL_STATE = :pending
state_machine :state, initial: INITIAL_STATE do
event :start do
transition pending: :inprogress
end
......
---
title: Change stubbed DastSiteProfile#status for calculated status
merge_request: 44133
author:
type: changed
......@@ -2,7 +2,17 @@
FactoryBot.define do
factory :dast_site do
project
url { generate(:url) }
before(:create) do |dast_site|
dast_site.project ||= FactoryBot.create(:project)
dast_site.dast_site_validation ||= FactoryBot.create(
:dast_site_validation,
dast_site_token: FactoryBot.create(
:dast_site_token,
project: dast_site.project
)
)
end
end
end
......@@ -31,6 +31,16 @@ RSpec.describe DastSiteProfilesFinder do
expect(recorder.count).to be_zero
end
it 'eager loads the dast_site_validation association' do
dast_site_profile1 = subject.first!
recorder = ActiveRecord::QueryRecorder.new do
dast_site_profile1.dast_site_validation
end
expect(recorder.count).to be_zero
end
context 'filtering by id' do
let(:params) { { id: dast_site_profile1.id } }
......
......@@ -84,7 +84,7 @@ RSpec.describe GitlabSchema.types['DastSiteProfile'] do
end
describe 'validation_status field' do
it 'is a placeholder validation status' do
it 'is the validation status' do
expect(first_dast_site_profile['validationStatus']).to eq('PENDING_VALIDATION')
end
end
......
......@@ -31,15 +31,23 @@ RSpec.describe DastSiteProfile, type: :model do
end
describe 'scopes' do
describe '.with_dast_site' do
describe '.with_dast_site_and_validation' do
before do
subject.dast_site_validation.update!(state: :failed)
end
it 'eager loads the association' do
subject
recorder = ActiveRecord::QueryRecorder.new do
subject.dast_site
subject.dast_site_validation
end
expect(recorder.count).to be_zero
aggregate_failures do
expect(subject.status).to eq('failed') # ensures guard passed
expect(recorder.count).to be_zero
end
end
end
end
......@@ -63,4 +71,23 @@ RSpec.describe DastSiteProfile, type: :model do
end
end
end
describe '#status' do
context 'when dast_site_validation association does not exist' do
it 'is pending' do
subject.dast_site.update!(dast_site_validation_id: nil)
aggregate_failures do
expect(subject.dast_site_validation).to be_nil
expect(subject.status).to eq(:pending)
end
end
end
context 'when dast_site_validation association does exist' do
it 'is dast_site_validation#state' do
expect(subject.status).to eq(subject.dast_site_validation.state)
end
end
end
end
......@@ -5,8 +5,8 @@ require 'spec_helper'
RSpec.describe 'Query.project(fullPath).dastSiteProfiles' do
include GraphqlHelpers
let_it_be(:dast_site_profile) { create(:dast_site_profile) }
let_it_be(:project) { dast_site_profile.project }
let_it_be(:project) { create(:project) }
let_it_be(:dast_site_profile) { create(:dast_site_profile, project: project) }
let_it_be(:current_user) { create(:user) }
let(:query) do
......@@ -76,7 +76,7 @@ RSpec.describe 'Query.project(fullPath).dastSiteProfiles' do
expect(first_dast_site_profile_response['id']).to eq(dast_site_profile.to_global_id.to_s)
end
it 'eager loads the dast site' do
it 'eager loads the dast site and dast site validation' do
control = ActiveRecord::QueryRecorder.new do
post_graphql(
query,
......
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