RSpec.describe Inquisition::Bundler::Audit do
  describe Inquisition::Bundler::Audit::Vulnerability do
    describe '#to_h' do
      subject(:vulnerability) { described_class.new }

      it { expect(vulnerability.to_h).to eq({}) }
    end
  end

  describe Inquisition::Bundler::Audit::UnpatchedGem do
    describe '#to_h' do
      subject(:unpatched_gem) do
        described_class.new(
          instance_double(Bundler::Audit::Scanner::UnpatchedGem, gem: gem, advisory: advisory)
        )
      end

      let(:gem) { instance_double(Bundler::LazySpecification) }

      context 'when advisory criticality is :high' do
        let(:advisory) do
          instance_double(Bundler::Audit::Advisory, criticality: :high,
                                                    title: 'Cocaine Gem for Ruby contains a flaw')
        end
        let(:options) do
          {
            severity: Inquisition::Severity::HIGH,
            message: 'Cocaine Gem for Ruby contains a flaw',
            context: gem
          }
        end

        it { expect(unpatched_gem.to_h).to eq(options) }
      end

      context 'when advisory criticality is :medium' do
        let(:advisory) do
          instance_double(Bundler::Audit::Advisory, criticality: :medium, title: 'XSS vulnerability in bootstrap')
        end
        let(:options) do
          {
            severity: Inquisition::Severity::MEDIUM,
            message: 'XSS vulnerability in bootstrap',
            context: gem
          }
        end

        it { expect(unpatched_gem.to_h).to eq(options) }
      end

      context 'when advisory criticality is :low' do
        let(:advisory) do
          instance_double(Bundler::Audit::Advisory, criticality: :low,
                                                    title: 'Multiple persistent XSS vulnerabilities in Radiant CMS')
        end
        let(:options) do
          {
            severity: Inquisition::Severity::LOW,
            message: 'Multiple persistent XSS vulnerabilities in Radiant CMS',
            context: gem
          }
        end

        it { expect(unpatched_gem.to_h).to eq(options) }
      end

      context 'when advisory criticality is absent' do
        let(:advisory) do
          instance_double(Bundler::Audit::Advisory, criticality: nil,
                                                    title: "Remote code execution in bootstrap-sass\n")
        end
        let(:options) do
          {
            severity: Inquisition::Severity::LOW,
            message: 'Remote code execution in bootstrap-sass',
            context: gem
          }
        end

        it { expect(unpatched_gem.to_h).to eq(options) }
      end
    end
  end

  describe Inquisition::Bundler::Audit::InsecureSource do
    describe '#to_h' do
      subject(:insecure_source) { described_class.new(source) }

      let(:source) { 'http://rubygems.org/' }
      let(:options) do
        {
          severity: Inquisition::Severity::MEDIUM,
          message: 'Insecure Source URI found: http://rubygems.org/'
        }
      end

      it { expect(insecure_source.to_h).to eq(options) }
    end
  end
end
