# frozen_string_literal: true

require 'test_helper'

describe Vulnerability do
  describe '.details' do
    let(:release) { create(:release) }
    let(:low_vulnerability) { create(:vulnerability, severity: 0) }
    let(:medium_vulnerability) { create(:vulnerability, severity: 1) }
    let(:high_vulnerability) { create(:vulnerability, severity: 2) }
    let(:unknown_severity) { create(:vulnerability, severity: nil) }

    before do
      release.vulnerabilities << low_vulnerability
      release.vulnerabilities << medium_vulnerability
      release.vulnerabilities << high_vulnerability
      release.vulnerabilities << unknown_severity
    end

    it 'should return all the vulnerabilities for a release' do
      _(Vulnerability.details(release.id)).must_include unknown_severity
      _(Vulnerability.details(release.id)).must_include low_vulnerability
      _(Vulnerability.details(release.id)).must_include medium_vulnerability
      _(Vulnerability.details(release.id)).must_include high_vulnerability
    end

    it 'should return the vulnerabilities with low severity for a release' do
      _(Vulnerability.details(release.id, filter: { severity: 'low' })).must_equal [low_vulnerability]
    end

    it 'should return the vulnerabilities with medium severity for a release' do
      _(Vulnerability.details(release.id, filter: { severity: 'medium' })).must_equal [medium_vulnerability]
    end

    it 'should return the vulnerabilities with high severity for a release' do
      _(Vulnerability.details(release.id, filter: { severity: 'high' })).must_equal [high_vulnerability]
    end

    it 'should return the vulnerabilities with null severity for a release' do
      _(Vulnerability.details(release.id, filter: { severity: 'unknown_severity' })).must_equal [unknown_severity]
    end

    it 'ransackable_associations delegates to authorizable_ransackable_associations' do
      expected = %w[releases other_association]
      Vulnerability.expects(:authorizable_ransackable_associations).returns(expected)
      assert_equal expected, Vulnerability.ransackable_associations
    end

    it 'ransackable_associations passes auth_object parameter' do
      Vulnerability.expects(:authorizable_ransackable_associations).returns(['releases'])
      assert_equal ['releases'], Vulnerability.ransackable_associations('admin')
    end

    it 'ransackable_associations handles nil return' do
      Vulnerability.stubs(:authorizable_ransackable_associations).returns(nil)
      assert_nil Vulnerability.ransackable_associations
    end
  end
end
