summaryrefslogtreecommitdiff
path: root/spec/unit/spdx/catalogue_spec.rb
blob: eeab29a821aae4407a0a31c4e4cf68d351d8f3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

RSpec.describe Spandx::Spdx::Catalogue do
  subject { described_class.new(catalogue_hash) }

  let(:spdx_file) { fixture_file('spdx/json/licenses.json') }
  let(:catalogue_hash) { JSON.parse(spdx_file.read, symbolize_names: true) }

  describe '#version' do
    let(:version) { SecureRandom.uuid }

    it { expect(described_class.new(licenseListVersion: version).version).to eql(version) }
  end

  describe '#[]' do
    context 'when fetcing a license by a known id' do
      let(:result) { subject['MIT'] }

      it { expect(result.id).to eql('MIT') }
      it { expect(result.name).to eql('MIT License') }
    end

    context 'when the id is not known' do
      it { expect(subject['unknown']).to be_nil }
    end
  end

  describe '#each' do
    let(:licenses) { catalogue_hash[:licenses] }

    it { expect(subject.count).to eql(licenses.count) }
    it { expect(subject.map(&:id)).to match_array(licenses.map { |x| x[:licenseId] }) }
    it { expect(subject.map(&:name)).to match_array(licenses.map { |x| x[:name] }) }

    context 'when some of the licenses are missing an identifier' do
      let(:catalogue_hash) do
        {
          licenseListVersion: '3.6',
          licenses: [
            { licenseId: nil, name: 'nil' },
            { licenseId: '', name: 'blank' },
            { licenseId: 'valid', name: 'valid' }
          ]
        }
      end

      it { expect(subject.count).to eq(1) }
      it { expect(subject.map(&:id)).to contain_exactly('valid') }
    end

    context 'when the schema of each license changes' do
      let(:catalogue_hash) do
        {
          licenseListVersion: '3.6',
          licenses: [
            {
              "license-ID": 'MIT',
              name: 'MIT License'
            }
          ]
        }
      end

      it { expect(subject.count).to be_zero }
    end

    context 'when the schema of the catalogue changes' do
      let(:catalogue_hash) { { SecureRandom.uuid.to_sym => [{ id: 'MIT', name: 'MIT License' }] } }

      it { expect(subject.count).to be_zero }
    end
  end

  describe '.latest' do
    subject { described_class.latest }

    context 'when the licenses.json endpoint is healthy' do
      let(:gateway) { instance_double(Spandx::Spdx::Gateway, fetch: catalogue) }
      let(:catalogue) { { licenses: [{ licenseId: 'example' }] } }

      before do
        allow(Spandx::Spdx::Gateway).to receive(:new).and_return(gateway)
      end

      it { expect(subject.count).to be(1) }
    end
  end

  describe '.from_file' do
    subject { described_class.from_file(spdx_file) }

    it { expect(subject.count).to eql(catalogue_hash[:licenses].count) }
  end

  describe '.from_git' do
    subject { described_class.from_git }

    it { expect(subject.count).to be > 400 }
  end

  describe '.default' do
    subject { described_class.default }

    context 'when the SPDX catalogue has not been cloned' do
      let(:gateway) { instance_double(Spandx::Spdx::Gateway, fetch: catalogue) }
      let(:catalogue) { { licenses: [{ licenseId: 'example' }] } }

      before do
        allow(Spandx::Spdx::Gateway).to receive(:new).and_return(gateway)
        allow(Spandx.git[:spdx]).to receive(:read).and_return(nil)
      end

      it { expect { subject }.not_to raise_error }
      it { expect(subject.count).to be(1) }
    end

    context 'when the SPDX catalogue has been cloned' do
      it { expect(subject.count).to be > 400 }
    end
  end
end