Commit 91604ec8 authored by Luke Duncalfe's avatar Luke Duncalfe

Merge branch 'optimising_rspec_using_let_it_be' into 'master'

Move from let! to let_it_be to optimize performance

See merge request gitlab-org/gitlab!63233
parents d014ba88 f9f7ab0b
......@@ -3,6 +3,11 @@
require 'spec_helper'
RSpec.describe Security::Finding do
let_it_be(:scan_1) { create(:security_scan, scan_type: :sast) }
let_it_be(:scan_2) { create(:security_scan, scan_type: :dast) }
let_it_be(:finding_1) { create(:security_finding, scan: scan_1) }
let_it_be(:finding_2) { create(:security_finding, scan: scan_2) }
describe 'associations' do
it { is_expected.to belong_to(:scan).required }
it { is_expected.to belong_to(:scanner).required }
......@@ -20,8 +25,6 @@ RSpec.describe Security::Finding do
end
describe '.by_uuid' do
let!(:finding_1) { create(:security_finding) }
let!(:finding_2) { create(:security_finding) }
let(:expected_findings) { [finding_1] }
subject { described_class.by_uuid(finding_1.uuid) }
......@@ -30,40 +33,39 @@ RSpec.describe Security::Finding do
end
describe '.by_build_ids' do
let!(:finding_1) { create(:security_finding) }
let!(:finding_2) { create(:security_finding) }
subject { described_class.by_build_ids(finding_1.scan.build_id) }
it { is_expected.to eq([finding_1]) }
end
describe '.by_severity_levels' do
let!(:critical_severity_finding) { create(:security_finding, severity: :critical) }
let!(:high_severity_finding) { create(:security_finding, severity: :high) }
let(:expected_findings) { [critical_severity_finding] }
let(:expected_findings) { [finding_2] }
subject { described_class.by_severity_levels(:critical) }
before do
finding_1.update! severity: :high
finding_2.update! severity: :critical
end
it { is_expected.to match_array(expected_findings) }
end
describe '.by_confidence_levels' do
let!(:high_confidence_finding) { create(:security_finding, confidence: :high) }
let!(:low_confidence_finding) { create(:security_finding, confidence: :low) }
let(:expected_findings) { [high_confidence_finding] }
let(:expected_findings) { [finding_2] }
subject { described_class.by_confidence_levels(:high) }
before do
finding_1.update! confidence: :low
finding_2.update! confidence: :high
end
it { is_expected.to match_array(expected_findings) }
end
describe '.by_report_types' do
let!(:sast_scan) { create(:security_scan, scan_type: :sast) }
let!(:dast_scan) { create(:security_scan, scan_type: :dast) }
let!(:sast_finding) { create(:security_finding, scan: sast_scan) }
let!(:dast_finding) { create(:security_finding, scan: dast_scan) }
let(:expected_findings) { [sast_finding] }
let(:expected_findings) { [finding_1] }
subject { described_class.by_report_types(:sast) }
......@@ -71,8 +73,6 @@ RSpec.describe Security::Finding do
end
describe '.by_project_fingerprints' do
let!(:finding_1) { create(:security_finding) }
let!(:finding_2) { create(:security_finding) }
let(:expected_findings) { [finding_1] }
subject { described_class.by_project_fingerprints(finding_1.project_fingerprint) }
......@@ -81,64 +81,63 @@ RSpec.describe Security::Finding do
end
describe '.undismissed' do
let(:scan_1) { create(:security_scan) }
let(:scan_2) { create(:security_scan) }
let!(:undismissed_finding) { create(:security_finding, scan: scan_1) }
let!(:dismissed_finding) { create(:security_finding, scan: scan_1) }
let(:expected_findings) { [undismissed_finding] }
let(:expected_findings) { [finding_2] }
subject { described_class.undismissed }
before do
finding_2.update! scan: scan_1
create(:vulnerability_feedback,
:dismissal,
project: scan_1.project,
category: scan_1.scan_type,
project_fingerprint: dismissed_finding.project_fingerprint)
project_fingerprint: finding_1.project_fingerprint)
create(:vulnerability_feedback,
:dismissal,
project: scan_2.project,
category: scan_2.scan_type,
project_fingerprint: undismissed_finding.project_fingerprint)
project_fingerprint: finding_2.project_fingerprint)
end
it { is_expected.to match_array(expected_findings) }
end
describe '.ordered' do
let!(:finding_1) { create(:security_finding, severity: :high, confidence: :unknown) }
let!(:finding_2) { create(:security_finding, severity: :low, confidence: :confirmed) }
let!(:finding_3) { create(:security_finding, severity: :critical, confidence: :confirmed) }
let!(:finding_4) { create(:security_finding, severity: :critical, confidence: :high) }
let_it_be(:finding_3) { create(:security_finding, severity: :critical, confidence: :confirmed) }
let_it_be(:finding_4) { create(:security_finding, severity: :critical, confidence: :high) }
let(:expected_findings) { [finding_3, finding_4, finding_1, finding_2] }
subject { described_class.ordered }
before do
finding_1.update!(severity: :high, confidence: :unknown)
finding_2.update!(severity: :low, confidence: :confirmed)
end
it { is_expected.to eq(expected_findings) }
end
describe '.deduplicated' do
let!(:finding_1) { create(:security_finding, deduplicated: true) }
let!(:finding_2) { create(:security_finding, deduplicated: false) }
let(:expected_findings) { [finding_1] }
subject { described_class.deduplicated }
before do
finding_1.update! deduplicated: true
finding_2.update! deduplicated: false
end
it { is_expected.to eq(expected_findings) }
end
describe '.count_by_scan_type' do
let!(:sast_scan) { create(:security_scan, scan_type: :sast) }
let!(:dast_scan) { create(:security_scan, scan_type: :dast) }
let!(:finding_1) { create(:security_finding, scan: sast_scan) }
let!(:finding_2) { create(:security_finding, scan: sast_scan) }
let!(:finding_3) { create(:security_finding, scan: dast_scan) }
subject { described_class.count_by_scan_type }
let_it_be(:finding_3) { create(:security_finding, scan: scan_1) }
it {
is_expected.to eq({
Security::Scan.scan_types['dast'] => 1,
......
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