Commit b0a03d62 authored by Philip Cunningham's avatar Philip Cunningham Committed by Igor Drozdov

Set dast_site_validation_id validation created

This ensures that associated DastSiteValidations can be
retreived via DastSiteProfile.
parent 5b89a956
...@@ -6,4 +6,8 @@ class DastSiteToken < ApplicationRecord ...@@ -6,4 +6,8 @@ class DastSiteToken < ApplicationRecord
validates :project_id, presence: true validates :project_id, presence: true
validates :token, length: { maximum: 255 }, presence: true validates :token, length: { maximum: 255 }, presence: true
validates :url, length: { maximum: 255 }, presence: true, public_url: true validates :url, length: { maximum: 255 }, presence: true, public_url: true
def dast_site
@dast_site ||= DastSite.find_by(project_id: project.id, url: url)
end
end end
...@@ -25,7 +25,7 @@ class DastSiteValidation < ApplicationRecord ...@@ -25,7 +25,7 @@ class DastSiteValidation < ApplicationRecord
enum validation_strategy: { text_file: 0, header: 1 } enum validation_strategy: { text_file: 0, header: 1 }
delegate :project, to: :dast_site_token, allow_nil: true delegate :project, :dast_site, to: :dast_site_token, allow_nil: true
def validation_url def validation_url
"#{url_base}/#{url_path}" "#{url_base}/#{url_path}"
......
...@@ -8,6 +8,10 @@ module DastSiteValidations ...@@ -8,6 +8,10 @@ module DastSiteValidations
dast_site_validation = create_validation! dast_site_validation = create_validation!
return ServiceResponse.error(message: 'Site does not exist for profile') unless dast_site_validation.dast_site
associate_dast_site!(dast_site_validation)
perform_async_validation(dast_site_validation) perform_async_validation(dast_site_validation)
rescue ActiveRecord::RecordInvalid => err rescue ActiveRecord::RecordInvalid => err
ServiceResponse.error(message: err.record.errors.full_messages) ServiceResponse.error(message: err.record.errors.full_messages)
...@@ -43,6 +47,10 @@ module DastSiteValidations ...@@ -43,6 +47,10 @@ module DastSiteValidations
@url_base ||= DastSiteValidation.get_normalized_url_base(dast_site_token.url) @url_base ||= DastSiteValidation.get_normalized_url_base(dast_site_token.url)
end end
def associate_dast_site!(dast_site_validation)
dast_site_validation.dast_site.update!(dast_site_validation_id: dast_site_validation.id)
end
def find_latest_successful_dast_site_validation def find_latest_successful_dast_site_validation
DastSiteValidationsFinder.new( DastSiteValidationsFinder.new(
project_id: container.id, project_id: container.id,
......
...@@ -2,13 +2,16 @@ ...@@ -2,13 +2,16 @@
FactoryBot.define do FactoryBot.define do
factory :dast_site_profile do factory :dast_site_profile do
project
dast_site { association :dast_site, project: project }
sequence :name do |i| sequence :name do |i|
"#{FFaker::Product.product_name.truncate(200)} - #{i}" "#{FFaker::Product.product_name.truncate(200)} - #{i}"
end end
before(:create) do |dast_site_profile| trait :with_dast_site_validation do
dast_site_profile.project ||= FactoryBot.create(:project) dast_site { association :dast_site, :with_dast_site_validation, project: project }
dast_site_profile.dast_site ||= FactoryBot.create(:dast_site, project: dast_site_profile.project)
end end
end end
end end
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
FactoryBot.define do FactoryBot.define do
factory :dast_site_token do factory :dast_site_token do
project
token { SecureRandom.uuid } token { SecureRandom.uuid }
url { generate(:url) }
before(:create) do |dast_site_token| url { generate(:url) }
dast_site_token.project ||= FactoryBot.create(:project)
end
end end
end end
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
FactoryBot.define do FactoryBot.define do
factory :dast_site_validation do factory :dast_site_validation do
dast_site_token
validation_strategy { DastSiteValidation.validation_strategies[:text_file] } validation_strategy { DastSiteValidation.validation_strategies[:text_file] }
url_path { 'some/path/GitLab-DAST-Site-Validation.txt' }
before(:create) do |dast_site_validation| url_path { 'some/path/GitLab-DAST-Site-Validation.txt' }
dast_site_validation.dast_site_token ||= FactoryBot.create(:dast_site_token)
end
end end
end end
...@@ -2,17 +2,14 @@ ...@@ -2,17 +2,14 @@
FactoryBot.define do FactoryBot.define do
factory :dast_site do factory :dast_site do
project
url { generate(:url) } url { generate(:url) }
before(:create) do |dast_site| trait :with_dast_site_validation do
dast_site.project ||= FactoryBot.create(:project) dast_site_validation do
dast_site.dast_site_validation ||= FactoryBot.create( association(:dast_site_validation, dast_site_token: association(:dast_site_token, project: project))
:dast_site_validation, end
dast_site_token: FactoryBot.create(
:dast_site_token,
project: dast_site.project
)
)
end end
end end
end end
...@@ -7,7 +7,8 @@ RSpec.describe Mutations::DastSiteValidations::Create do ...@@ -7,7 +7,8 @@ RSpec.describe Mutations::DastSiteValidations::Create do
let(:project) { dast_site_token.project } let(:project) { dast_site_token.project }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:full_path) { project.full_path } let(:full_path) { project.full_path }
let(:dast_site_token) { create(:dast_site_token, project: create(:project, group: group)) } let(:dast_site) { create(:dast_site, project: create(:project, group: group)) }
let(:dast_site_token) { create(:dast_site_token, project: dast_site.project, url: dast_site.url) }
let(:dast_site_validation) { DastSiteValidation.find_by!(url_path: validation_path) } let(:dast_site_validation) { DastSiteValidation.find_by!(url_path: validation_path) }
let(:validation_path) { SecureRandom.hex } let(:validation_path) { SecureRandom.hex }
......
...@@ -86,7 +86,7 @@ RSpec.describe GitlabSchema.types['DastSiteProfile'] do ...@@ -86,7 +86,7 @@ RSpec.describe GitlabSchema.types['DastSiteProfile'] do
describe 'validation_status field' do describe 'validation_status field' do
it 'is the validation status' do it 'is the validation status' do
expect(first_dast_site_profile['validationStatus']).to eq('PENDING_VALIDATION') expect(first_dast_site_profile['validationStatus']).to eq('NONE')
end end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe DastSiteProfile, type: :model do RSpec.describe DastSiteProfile, type: :model do
subject { create(:dast_site_profile) } subject { create(:dast_site_profile, :with_dast_site_validation) }
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
......
...@@ -28,4 +28,20 @@ RSpec.describe DastSiteToken, type: :model do ...@@ -28,4 +28,20 @@ RSpec.describe DastSiteToken, type: :model do
end end
end end
end end
describe '#dast_site' do
context 'when dast_site exists' do
it 'finds the associated dast_site' do
dast_site = create(:dast_site, project_id: subject.project_id, url: subject.url)
expect(subject.dast_site).to eq(dast_site)
end
end
context 'when dast_site does not exist' do
it 'returns nil' do
expect(subject.dast_site).to be_nil
end
end
end
end end
...@@ -87,6 +87,12 @@ RSpec.describe DastSiteValidation, type: :model do ...@@ -87,6 +87,12 @@ RSpec.describe DastSiteValidation, type: :model do
end end
end end
describe '#dast_site' do
it 'returns dast_site through dast_site_token' do
expect(subject.dast_site).to eq(subject.dast_site_token.dast_site)
end
end
describe '#validation_url' do describe '#validation_url' do
it 'formats the url correctly' do it 'formats the url correctly' do
expect(subject.validation_url).to eq("#{subject.url_base}/#{subject.url_path}") expect(subject.validation_url).to eq("#{subject.url_base}/#{subject.url_path}")
......
...@@ -5,9 +5,10 @@ require 'spec_helper' ...@@ -5,9 +5,10 @@ require 'spec_helper'
RSpec.describe 'Creating a DAST Site Token' do RSpec.describe 'Creating a DAST Site Token' do
include GraphqlHelpers include GraphqlHelpers
let(:dast_site_token) { create(:dast_site_token, project: project) } let(:dast_site) { create(:dast_site, project: project) }
let(:dast_site_validation) { DastSiteValidation.find_by!(url_path: validation_path) } let(:dast_site_token) { create(:dast_site_token, project: project, url: dast_site.url) }
let(:validation_path) { SecureRandom.hex } let(:validation_path) { SecureRandom.hex }
let(:dast_site_validation) { DastSiteValidation.find_by!(url_path: validation_path) }
let(:mutation_name) { :dast_site_validation_create } let(:mutation_name) { :dast_site_validation_create }
let(:mutation) do let(:mutation) do
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe 'Query.project(fullPath).dastSiteProfile' do RSpec.describe 'Query.project(fullPath).dastSiteProfile' do
include GraphqlHelpers include GraphqlHelpers
let_it_be(:dast_site_profile) { create(:dast_site_profile) } let_it_be(:dast_site_profile) { create(:dast_site_profile, :with_dast_site_validation) }
let_it_be(:project) { dast_site_profile.project } let_it_be(:project) { dast_site_profile.project }
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
......
...@@ -3,14 +3,19 @@ ...@@ -3,14 +3,19 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe DastSiteValidations::CreateService do RSpec.describe DastSiteValidations::CreateService do
let(:project) { dast_site_token.project } let_it_be(:project) { create(:project) }
let(:dast_site_token) { create(:dast_site_token, project: create(:project)) } let_it_be(:dast_site) { create(:dast_site, project: project) }
let(:url_path) { SecureRandom.hex } let_it_be(:dast_site_token) { create(:dast_site_token, project: project, url: dast_site.url) }
let(:params) { { dast_site_token: dast_site_token, url_path: url_path, validation_strategy: :text_file } }
subject { described_class.new(container: project, params: params).execute } let(:params) { { dast_site_token: dast_site_token, url_path: SecureRandom.hex, validation_strategy: :text_file } }
subject { described_class.new(container: dast_site.project, params: params).execute }
describe 'execute', :clean_gitlab_redis_shared_state do
before do
project.clear_memoization(:licensed_feature_available)
end
describe 'execute' do
context 'when on demand scan feature is disabled' do context 'when on demand scan feature is disabled' do
it 'communicates failure' do it 'communicates failure' do
stub_licensed_features(security_on_demand_scans: true) stub_licensed_features(security_on_demand_scans: true)
...@@ -45,6 +50,10 @@ RSpec.describe DastSiteValidations::CreateService do ...@@ -45,6 +50,10 @@ RSpec.describe DastSiteValidations::CreateService do
expect(subject.status).to eq(:success) expect(subject.status).to eq(:success)
end end
it 'associates the dast_site_validation with the dast_site' do
expect(subject.payload).to eq(dast_site.reload.dast_site_validation)
end
it 'attempts to validate' do it 'attempts to validate' do
aggregate_failures do aggregate_failures do
expect(DastSiteValidationWorker).to receive(:perform_async) expect(DastSiteValidationWorker).to receive(:perform_async)
...@@ -83,7 +92,7 @@ RSpec.describe DastSiteValidations::CreateService do ...@@ -83,7 +92,7 @@ RSpec.describe DastSiteValidations::CreateService do
end end
context 'when the dast_site_token.project and container do not match' do context 'when the dast_site_token.project and container do not match' do
let(:project) { create(:project) } let_it_be(:dast_site_token) { create(:dast_site_token, project: create(:project), url: dast_site.url) }
it 'communicates failure' do it 'communicates failure' do
aggregate_failures do aggregate_failures do
...@@ -99,7 +108,7 @@ RSpec.describe DastSiteValidations::CreateService do ...@@ -99,7 +108,7 @@ RSpec.describe DastSiteValidations::CreateService do
end end
let(:dast_site_validation) do let(:dast_site_validation) do
DastSiteValidation.find_by!(dast_site_token: dast_site_token, url_path: url_path) DastSiteValidation.find_by!(dast_site_token: dast_site_token, url_path: params[:url_path])
end end
it 'communicates failure' do it 'communicates failure' do
...@@ -123,6 +132,17 @@ RSpec.describe DastSiteValidations::CreateService do ...@@ -123,6 +132,17 @@ RSpec.describe DastSiteValidations::CreateService do
expect(Gitlab::AppLogger).to have_received(:error).with(message: 'Unable to validate dast_site_validation', dast_site_validation_id: dast_site_validation.id) expect(Gitlab::AppLogger).to have_received(:error).with(message: 'Unable to validate dast_site_validation', dast_site_validation_id: dast_site_validation.id)
end end
end end
context 'when the dast_site_token does not have a related dast_site via its url' do
let_it_be(:dast_site_token) { create(:dast_site_token, project: project, url: generate(:url)) }
it 'communicates failure' do
aggregate_failures do
expect(subject.status).to eq(:error)
expect(subject.message).to eq('Site does not exist for profile')
end
end
end
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