Welcome to mirror list, hosted at ThFree Co, Russian Federation.

set_correct_vulnerability_state_spec.rb « background_migration « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5b98e49a3135770d1045c0e2822b2bee6975de7 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::SetCorrectVulnerabilityState do
  let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
  let(:users) { table(:users) }
  let(:user) { create_user! }
  let(:project) do
    table(:projects).create!(
      namespace_id: namespace.id,
      project_namespace_id: namespace.id,
      packages_enabled: false)
  end

  let(:vulnerabilities) { table(:vulnerabilities) }

  let!(:vulnerability_with_dismissed_at) do
    create_vulnerability!(
      project_id: project.id,
      author_id: user.id,
      dismissed_at: Time.current
    )
  end

  let!(:vulnerability_without_dismissed_at) do
    create_vulnerability!(
      project_id: project.id,
      author_id: user.id,
      dismissed_at: nil
    )
  end

  let(:detected_state) { 1 }
  let(:dismissed_state) { 2 }

  subject(:perform_migration) do
    described_class.new(start_id: vulnerability_with_dismissed_at.id,
                        end_id: vulnerability_without_dismissed_at.id,
                        batch_table: :vulnerabilities,
                        batch_column: :id,
                        sub_batch_size: 1,
                        pause_ms: 0,
                        connection: ActiveRecord::Base.connection)
                   .perform
  end

  it 'changes vulnerability state to `dismissed` when dismissed_at is not nil' do
    expect { perform_migration }.to change { vulnerability_with_dismissed_at.reload.state }.to(dismissed_state)
  end

  it 'does not change the state when dismissed_at is nil' do
    expect { perform_migration }.not_to change { vulnerability_without_dismissed_at.reload.state }
  end

  private

  def create_vulnerability!(
    project_id:, author_id:, title: 'test', severity: 7, confidence: 7, report_type: 0, state: 1, dismissed_at: nil
  )
    vulnerabilities.create!(
      project_id: project_id,
      author_id: author_id,
      title: title,
      severity: severity,
      confidence: confidence,
      report_type: report_type,
      state: state,
      dismissed_at: dismissed_at
    )
  end

  def create_user!(name: "Example User", email: "user@example.com", user_type: nil)
    users.create!(
      name: name,
      email: email,
      username: name,
      projects_limit: 10
    )
  end
end