summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/drop_invalid_security_findings_spec.rb
blob: 7cc64889fc868f3c273d5e417266ad4db1b69cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::DropInvalidSecurityFindings, schema: 20211108211434 do
  let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user', type: Namespaces::UserNamespace.sti_name) }
  let(:project) { table(:projects).create!(namespace_id: namespace.id) }

  let(:pipelines) { table(:ci_pipelines) }
  let!(:pipeline) { pipelines.create!(project_id: project.id) }

  let(:ci_builds) { table(:ci_builds) }
  let!(:ci_build) { ci_builds.create! }

  let(:security_scans) { table(:security_scans) }
  let!(:security_scan) do
    security_scans.create!(
      scan_type: 1,
      status: 1,
      build_id: ci_build.id,
      project_id: project.id,
      pipeline_id: pipeline.id
    )
  end

  let(:vulnerability_scanners) { table(:vulnerability_scanners) }
  let!(:vulnerability_scanner) { vulnerability_scanners.create!(project_id: project.id, external_id: 'test 1', name: 'test scanner 1') }

  let(:security_findings) { table(:security_findings) }
  let!(:security_finding_without_uuid) do
    security_findings.create!(
      severity: 1,
      confidence: 1,
      scan_id: security_scan.id,
      scanner_id: vulnerability_scanner.id,
      uuid: nil
    )
  end

  let!(:security_finding_with_uuid) do
    security_findings.create!(
      severity: 1,
      confidence: 1,
      scan_id: security_scan.id,
      scanner_id: vulnerability_scanner.id,
      uuid: 'bd95c085-71aa-51d7-9bb6-08ae669c262e'
    )
  end

  let(:sub_batch_size) { 10_000 }

  subject { described_class.new.perform(security_finding_without_uuid.id, security_finding_with_uuid.id, sub_batch_size) }

  it 'drops Security::Finding objects with no UUID' do
    expect { subject }.to change(security_findings, :count).from(2).to(1)
  end
end