# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::RemoveUndefinedVulnerabilityConfidenceLevel, :migration, schema: 20200511092714 do
  let(:vulnerabilities) { table(:vulnerabilities) }
  let(:identifiers) { table(:vulnerability_identifiers) }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:users) { table(:users) }

  it 'updates undefined confidence level to unknown' do
    namespace = namespaces.create!(name: 'gitlab-org', path: 'gitlab-org')
    projects.create!(id: 123, namespace_id: namespace.id, name: 'gitlab', path: 'gitlab')
    users.create!(id: 13, email: 'author@example.com', notification_email: 'author@example.com', name: 'author', username: 'author', projects_limit: 10, state: 'active')

    vul1 = vulnerabilities.create!(vuln_params)
    vulnerabilities.create!(vuln_params)
    vul3 = vulnerabilities.create!(vuln_params.merge(confidence: 2))

    expect(vulnerabilities.where(confidence: 2).count). to eq(1)
    expect(vulnerabilities.where(severity: 5).count). to eq(3)

    described_class.new.perform(vul1.id, vul3.id)

    expect(vulnerabilities.where(confidence: 2).count).to eq(3)
  end

  def vuln_params
    {
      title: 'title',
      state: 1,
      severity: 5,
      confidence: 0,
      report_type: 2,
      project_id: 123,
      author_id: 13
    }
  end
end
