Commit 3cfa60c6 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Encapsulate the validity logic of Finding in entity itself

There is no better place than the entity itself to calculate if it is
valid or not.
parent 7e0e3508
......@@ -42,7 +42,7 @@ module Security
end
def create_vulnerability_finding(finding)
return if finding.scanner.blank? || finding.primary_identifier.blank?
return unless finding.valid?
vulnerability_params = finding.to_hash.except(:compare_key, :identifiers, :location, :scanner)
vulnerability_finding = create_or_find_vulnerability_finding(finding, vulnerability_params)
......
......@@ -81,6 +81,10 @@ module Gitlab
report_type.hash ^ location.fingerprint.hash ^ primary_identifier.fingerprint.hash
end
def valid?
scanner.present? && primary_identifier.present? && location.present?
end
private
def generate_project_fingerprint
......
......@@ -228,4 +228,42 @@ RSpec.describe Gitlab::Ci::Reports::Security::Finding do
end
end
end
describe '#valid?' do
let(:scanner) { build(:ci_reports_security_scanner) }
let(:identifiers) { [build(:ci_reports_security_identifier)] }
let(:location) { build(:ci_reports_security_locations_sast) }
let(:finding) do
build(:ci_reports_security_finding,
scanner: scanner,
identifiers: identifiers,
location: location,
compare_key: '')
end
subject { finding.valid? }
context 'when the scanner is missing' do
let(:scanner) { nil }
it { is_expected.to be_falsey }
end
context 'when there is no identifier' do
let(:identifiers) { [] }
it { is_expected.to be_falsey }
end
context 'when the location is missing' do
let(:location) { nil }
it { is_expected.to be_falsey }
end
context 'when all required attributes present' do
it { is_expected.to be_truthy }
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