Welcome to mirror list, hosted at ThFree Co, Russian Federation.

symbol_spec.rb « nuget « packages « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bae8f90c7d50b060ea403848d8d9de7a81de4625 (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
122
123
124
125
126
127
128
129
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Nuget::Symbol, type: :model, feature_category: :package_registry do
  subject(:symbol) { create(:nuget_symbol) }

  it { is_expected.to be_a FileStoreMounter }
  it { is_expected.to be_a ShaAttribute }
  it { is_expected.to be_a Packages::Destructible }

  describe 'relationships' do
    it { is_expected.to belong_to(:package).inverse_of(:nuget_symbols) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:file) }
    it { is_expected.to validate_presence_of(:file_path) }
    it { is_expected.to validate_presence_of(:signature) }
    it { is_expected.to validate_presence_of(:object_storage_key) }
    it { is_expected.to validate_presence_of(:size) }
    it { is_expected.to validate_uniqueness_of(:signature).scoped_to(:file_path) }
    it { is_expected.to validate_uniqueness_of(:object_storage_key).case_insensitive }
  end

  describe 'delegations' do
    it { is_expected.to delegate_method(:project_id).to(:package) }
  end

  describe 'scopes' do
    describe '.stale' do
      subject { described_class.stale }

      let_it_be(:symbol) { create(:nuget_symbol) }
      let_it_be(:stale_symbol) { create(:nuget_symbol, :stale) }

      it { is_expected.to contain_exactly(stale_symbol) }
    end

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

      let_it_be(:symbol) { create(:nuget_symbol, :stale, :processing) }
      let_it_be(:stale_symbol) { create(:nuget_symbol, :stale) }

      it { is_expected.to contain_exactly(stale_symbol) }
    end

    describe '.with_signature' do
      subject(:with_signature) { described_class.with_signature(signature) }

      let_it_be(:signature) { 'signature' }
      let_it_be(:symbol) { create(:nuget_symbol, signature: signature) }

      it 'returns symbols with the given signature' do
        expect(with_signature).to eq([symbol])
      end
    end

    describe '.with_file_name' do
      subject(:with_file_name) { described_class.with_file_name(file_name) }

      let_it_be(:file_name) { 'file_name' }
      let_it_be(:symbol) { create(:nuget_symbol) }

      before do
        symbol.update_column(:file, file_name)
      end

      it 'returns symbols with the given file_name' do
        expect(with_file_name).to eq([symbol])
      end
    end

    describe '.with_file_sha256' do
      subject(:with_file_sha256) { described_class.with_file_sha256(checksums) }

      let_it_be(:checksums) { OpenSSL::Digest.hexdigest('SHA256', 'checksums') }
      let_it_be(:symbol) { create(:nuget_symbol, file_sha256: checksums) }

      it 'returns symbols with the given checksums' do
        expect(with_file_sha256).to eq([symbol])
      end
    end
  end

  describe 'callbacks' do
    describe 'before_validation' do
      describe '#set_object_storage_key' do
        context 'when signature and project_id are present' do
          it 'sets the object_storage_key' do
            expected_key = Gitlab::HashedPath.new(
              'packages', 'nuget', symbol.package_id, 'symbols', OpenSSL::Digest::SHA256.hexdigest(symbol.signature),
              root_hash: symbol.project_id
            ).to_s

            symbol.valid?

            expect(symbol.object_storage_key).to eq(expected_key)
          end
        end

        context 'when signature is not present' do
          subject(:symbol) { build(:nuget_symbol, signature: nil) }

          it 'does not set the object_storage_key' do
            symbol.valid?

            expect(symbol.object_storage_key).to be_nil
          end
        end

        context 'when project_id is not present' do
          subject(:symbol) { build(:nuget_symbol) }

          before do
            allow(symbol).to receive(:project_id).and_return(nil)
          end

          it 'does not set the object_storage_key' do
            symbol.valid?

            expect(symbol.object_storage_key).to be_nil
          end
        end
      end
    end
  end
end