summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/migrate_links_for_vulnerability_findings_spec.rb
blob: fd2e3ffb670c0722d6135b3fa4831e9a54723187 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::MigrateLinksForVulnerabilityFindings,
  feature_category: :vulnerability_management do
  let(:vulnerability_occurrences) { table(:vulnerability_occurrences) }
  let(:vulnerability_finding_links) { table(:vulnerability_finding_links) }
  let(:link_hash) { { url: 'http://test.com' } }
  let(:namespace1) { table(:namespaces).create!(name: 'namespace 1', path: 'namespace1') }
  let(:project1) { table(:projects).create!(namespace_id: namespace1.id, project_namespace_id: namespace1.id) }
  let(:user) { table(:users).create!(email: 'test1@example.com', projects_limit: 5) }

  let(:scanner1) do
    table(:vulnerability_scanners).create!(project_id: project1.id, external_id: 'test 1', name: 'test scanner 1')
  end

  let(:stating_id) { vulnerability_occurrences.pluck(:id).min }
  let(:end_id) { vulnerability_occurrences.pluck(:id).max }

  let(:migration) do
    described_class.new(
      start_id: stating_id,
      end_id: end_id,
      batch_table: :vulnerability_occurrences,
      batch_column: :id,
      sub_batch_size: 2,
      pause_ms: 2,
      connection: ApplicationRecord.connection
    )
  end

  subject(:perform_migration) { migration.perform }

  context 'without the presence of links key' do
    before do
      create_finding!(project1.id, scanner1.id, { other_keys: 'test' })
    end

    it 'does not create any link' do
      expect(Gitlab::AppLogger).not_to receive(:error)

      expect { perform_migration }.not_to change { vulnerability_finding_links.count }
    end
  end

  context 'with links equals to an array of nil element' do
    before do
      create_finding!(project1.id, scanner1.id, { links: [nil] })
    end

    it 'does not create any link' do
      expect(Gitlab::AppLogger).not_to receive(:error)

      expect { perform_migration }.not_to change { vulnerability_finding_links.count }
    end
  end

  context 'with links equals to an array of duplicated elements' do
    let!(:finding) do
      create_finding!(project1.id, scanner1.id, { links: [link_hash, link_hash] })
    end

    it 'creates one new link' do
      expect(Gitlab::AppLogger).not_to receive(:error)

      expect { perform_migration }.to change { vulnerability_finding_links.count }.by(1)
    end
  end

  context 'with existing links within raw_metadata' do
    let!(:finding1) { create_finding!(project1.id, scanner1.id, { links: [link_hash] }) }
    let!(:finding2) { create_finding!(project1.id, scanner1.id, { links: [link_hash] }) }

    it 'creates new link for each finding' do
      expect(Gitlab::AppLogger).not_to receive(:error)

      expect { perform_migration }.to change { vulnerability_finding_links.count }.by(2)
    end

    context 'when create throws exception ActiveRecord::RecordNotUnique' do
      before do
        allow(migration).to receive(:create_links).and_raise(ActiveRecord::RecordNotUnique)
      end

      it 'does not log this error nor create new records' do
        expect(Gitlab::AppLogger).not_to receive(:error)

        expect { perform_migration }.not_to change { vulnerability_finding_links.count }
      end
    end

    context 'when create throws exception StandardError' do
      before do
        allow(migration).to receive(:create_links).and_raise(StandardError)
      end

      it 'logs StandardError' do
        expect(Gitlab::AppLogger).to receive(:error).with({
          class: described_class.name, message: StandardError.to_s, model_id: finding1.id
        })
        expect(Gitlab::AppLogger).to receive(:error).with({
          class: described_class.name, message: StandardError.to_s, model_id: finding2.id
        })
        expect { perform_migration }.not_to change { vulnerability_finding_links.count }
      end
    end
  end

  context 'with existing link records' do
    let!(:finding) { create_finding!(project1.id, scanner1.id, { links: [link_hash] }) }

    before do
      vulnerability_finding_links.create!(vulnerability_occurrence_id: finding.id, url: link_hash[:url])
    end

    it 'does not create new link' do
      expect(Gitlab::AppLogger).not_to receive(:error)

      expect { perform_migration }.not_to change { vulnerability_finding_links.count }
    end
  end

  private

  def create_finding!(project_id, scanner_id, raw_metadata)
    vulnerability = table(:vulnerabilities).create!(project_id: project_id, author_id: user.id, title: 'test',
      severity: 4, confidence: 4, report_type: 0)

    identifier = table(:vulnerability_identifiers).create!(project_id: project_id, external_type: 'uuid-v5',
      external_id: 'uuid-v5', fingerprint: OpenSSL::Digest::SHA256.hexdigest(vulnerability.id.to_s),
      name: 'Identifier for UUIDv5 2 2')

    table(:vulnerability_occurrences).create!(
      vulnerability_id: vulnerability.id, project_id: project_id, scanner_id: scanner_id,
      primary_identifier_id: identifier.id, name: 'test', severity: 4, confidence: 4, report_type: 0,
      uuid: SecureRandom.uuid, project_fingerprint: '123qweasdzxc', location: { "image" => "alpine:3.4" },
      location_fingerprint: 'test', metadata_version: 'test',
      raw_metadata: raw_metadata.to_json)
  end
end