Commit a3a4846b authored by charlie ablett's avatar charlie ablett

Merge branch...

Merge branch 'philipcunningham-add-model-level-constraints-for-dast-profile-and-dast-scanner-profile' into 'master'

Add missing validations to DAST models

See merge request gitlab-org/gitlab!53434
parents e7ab053b 86147808
...@@ -9,10 +9,11 @@ module Dast ...@@ -9,10 +9,11 @@ module Dast
belongs_to :dast_scanner_profile belongs_to :dast_scanner_profile
validates :description, length: { maximum: 255 } validates :description, length: { maximum: 255 }
validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id } validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id }, presence: true
validates :project_id, :dast_site_profile_id, :dast_scanner_profile_id, presence: true validates :project_id, :dast_site_profile_id, :dast_scanner_profile_id, presence: true
validate :project_ids_match validate :project_ids_match
validate :description_not_nil
scope :by_project_id, -> (project_id) do scope :by_project_id, -> (project_id) do
where(project_id: project_id) where(project_id: project_id)
...@@ -25,6 +26,10 @@ module Dast ...@@ -25,6 +26,10 @@ module Dast
association_project_id_matches(dast_scanner_profile) association_project_id_matches(dast_scanner_profile)
end end
def description_not_nil
errors.add(:description, 'can\'t be nil') if description.nil?
end
def association_project_id_matches(association) def association_project_id_matches(association)
return if association.nil? return if association.nil?
......
...@@ -4,7 +4,7 @@ class DastScannerProfile < ApplicationRecord ...@@ -4,7 +4,7 @@ class DastScannerProfile < ApplicationRecord
belongs_to :project belongs_to :project
validates :project_id, presence: true validates :project_id, presence: true
validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id } 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) }
......
...@@ -4,7 +4,7 @@ class DastSiteProfile < ApplicationRecord ...@@ -4,7 +4,7 @@ class DastSiteProfile < ApplicationRecord
belongs_to :project belongs_to :project
belongs_to :dast_site belongs_to :dast_site
validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id } validates :name, length: { maximum: 255 }, uniqueness: { scope: :project_id }, presence: true
validates :project_id, :dast_site_id, presence: true validates :project_id, :dast_site_id, presence: true
validate :dast_site_project_id_fk validate :dast_site_project_id_fk
......
...@@ -19,6 +19,7 @@ RSpec.describe Dast::Profile, type: :model do ...@@ -19,6 +19,7 @@ RSpec.describe Dast::Profile, type: :model do
it { is_expected.to validate_presence_of(:project_id) } it { is_expected.to validate_presence_of(:project_id) }
it { is_expected.to validate_presence_of(:dast_site_profile_id) } it { is_expected.to validate_presence_of(:dast_site_profile_id) }
it { is_expected.to validate_presence_of(:dast_scanner_profile_id) } it { is_expected.to validate_presence_of(:dast_scanner_profile_id) }
it { is_expected.to validate_presence_of(:name) }
context 'when the project_id and dast_site_profile.project_id do not match' do context 'when the project_id and dast_site_profile.project_id do not match' do
let(:project) { create(:project) } let(:project) { create(:project) }
...@@ -47,6 +48,17 @@ RSpec.describe Dast::Profile, type: :model do ...@@ -47,6 +48,17 @@ RSpec.describe Dast::Profile, type: :model do
end end
end end
end end
context 'when the description is nil' do
subject { build(:dast_profile, description: nil) }
it 'is not valid' do
aggregate_failures do
expect(subject.valid?).to be_falsey
expect(subject.errors.full_messages).to include('Description can\'t be nil')
end
end
end
end end
describe 'scopes' do describe 'scopes' do
......
...@@ -14,6 +14,7 @@ RSpec.describe DastScannerProfile, type: :model do ...@@ -14,6 +14,7 @@ RSpec.describe DastScannerProfile, type: :model do
it { is_expected.to validate_length_of(:name).is_at_most(255) } it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) } it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_presence_of(:project_id) } it { is_expected.to validate_presence_of(:project_id) }
it { is_expected.to validate_presence_of(:name) }
end end
describe 'scopes' do describe 'scopes' do
......
...@@ -16,6 +16,7 @@ RSpec.describe DastSiteProfile, type: :model do ...@@ -16,6 +16,7 @@ RSpec.describe DastSiteProfile, type: :model do
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) } it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_presence_of(:project_id) } it { is_expected.to validate_presence_of(:project_id) }
it { is_expected.to validate_presence_of(:dast_site_id) } it { is_expected.to validate_presence_of(:dast_site_id) }
it { is_expected.to validate_presence_of(:name) }
context 'when the project_id and dast_site.project_id do not match' do context 'when the project_id and dast_site.project_id do not match' do
let(:project) { create(:project) } let(:project) { create(:project) }
......
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