# frozen_string_literal: true

require 'spec_helper'

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

  it 'updates undefined severity level to unknown' do
    projects.create!(id: 123, namespace_id: 12, 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(severity: 2))

    expect(vulnerabilities.where(severity: 2).count). to eq(1)
    expect(vulnerabilities.where(severity: 0).count). to eq(2)

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

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

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