# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe UpdateCsVulnerabilityConfidenceColumn do
  include MigrationHelpers::VulnerabilitiesFindingsHelper

  let(:vulnerabilities) { table(:vulnerability_occurrences) }
  let(:identifiers) { table(:vulnerability_identifiers) }
  let(:scanners) { table(:vulnerability_scanners) }
  let(:projects) { table(:projects) }
  let(:finding1_attributes) { attributes_for_vulnerabilities_finding }
  let(:finding2_attributes) { attributes_for_vulnerabilities_finding }

  before do
    stub_const("#{described_class}::BATCH_SIZE", 2)
  end

  it 'updates confidence levels for container scanning reports', :sidekiq_might_not_need_inline do
    projects.create!(id: 123, namespace_id: 12, name: 'gitlab', path: 'gitlab')

    identifiers.create!(id: 1,
                        project_id: 123,
                        fingerprint: 'd432c2ad2953e8bd587a3a43b3ce309b5b0154c2',
                        external_type: 'SECURITY_ID',
                        external_id: 'SECURITY_0',
                        name: 'SECURITY_IDENTIFIER 0')

    identifiers.create!(id: 2,
                        project_id: 123,
                        fingerprint: 'd432c2ad2953e8bd587a3a43b3ce309b5b0154c3',
                        external_type: 'SECURITY_ID',
                        external_id: 'SECURITY_0',
                        name: 'SECURITY_IDENTIFIER 0')

    scanners.create!(id: 6, project_id: 123, external_id: 'clair', name: 'Security Scanner')

    vulnerabilities.create!(id: 1,
                            severity: 2,
                            confidence: 5,
                            report_type: 2,
                            project_id: 123,
                            scanner_id: 6,
                            primary_identifier_id: 1,
                            project_fingerprint: finding1_attributes[:project_fingerprint],
                            location_fingerprint: finding1_attributes[:location_fingerprint],
                            uuid: finding1_attributes[:uuid],
                            name: finding1_attributes[:name],
                            metadata_version: finding1_attributes[:metadata_version],
                            raw_metadata: finding1_attributes[:raw_metadata])

    vulnerabilities.create!(id: 2,
                            severity: 2,
                            confidence: 5,
                            report_type: 2,
                            project_id: 123,
                            scanner_id: 6,
                            primary_identifier_id: 2,
                            project_fingerprint: finding2_attributes[:project_fingerprint],
                            location_fingerprint: finding2_attributes[:location_fingerprint],
                            uuid: finding2_attributes[:uuid],
                            name: finding2_attributes[:name],
                            metadata_version: finding2_attributes[:metadata_version],
                            raw_metadata: finding2_attributes[:raw_metadata])

    expect(vulnerabilities.where(report_type: 2, confidence: 2).count). to eq(0)
    expect(vulnerabilities.exists?(report_type: 2, confidence: 5)).to be_truthy

    migrate!

    expect(vulnerabilities.exists?(report_type: 2, confidence: 5)).to be_falsy
    expect(vulnerabilities.where(report_type: 2, confidence: 2).count). to eq(2)
  end
end
