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

extraction_worker_spec.rb « nuget « packages « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1d42d556c2172294b179a1f204facaf6ed95269 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Nuget::ExtractionWorker, type: :worker, feature_category: :package_registry do
  describe '#perform' do
    let!(:package) { create(:nuget_package) }
    let(:package_file) { package.package_files.first }
    let(:package_file_id) { package_file.id }

    let_it_be(:package_name) { 'DummyProject.DummyPackage' }
    let_it_be(:package_version) { '1.0.0' }

    subject { described_class.new.perform(package_file_id) }

    shared_examples 'handling the metadata error' do |exception_class: ::Packages::Nuget::UpdatePackageFromMetadataService::InvalidMetadataError|
      it 'updates package status to error', :aggregate_failures do
        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          instance_of(exception_class),
          project_id: package.project_id
        )

        subject

        expect(package.reload).to be_error
      end
    end

    context 'with valid package file' do
      it 'updates package and package file' do
        expect { subject }
          .to not_change { Packages::Package.count }
          .and not_change { Packages::PackageFile.count }
      end

      context 'with exisiting package' do
        let!(:existing_package) { create(:nuget_package, project: package.project, name: package_name, version: package_version) }

        it 'reuses existing package and updates package file' do
          expect { subject }
            .to change { Packages::Package.count }.by(-1)
            .and change { existing_package.reload.package_files.count }.by(1)
            .and not_change { Packages::PackageFile.count }
        end
      end
    end

    context 'with invalid package file id' do
      let(:package_file_id) { 5555 }

      it "doesn't update package and package file" do
        expect { subject }
          .to not_change { package.reload.name }
          .and not_change { package.version }
          .and not_change { package_file.reload.file_name }
      end
    end

    context 'with package file not containing a nuspec file' do
      before do
        allow_any_instance_of(Zip::File).to receive(:glob).and_return([])
      end

      it_behaves_like 'handling the metadata error', exception_class: ::Packages::Nuget::MetadataExtractionService::ExtractionError
    end

    context 'with package with an invalid package name' do
      invalid_names = [
        '',
        'My/package',
        '../../../my_package',
        '%2e%2e%2fmy_package'
      ]

      invalid_names.each do |invalid_name|
        context "with #{invalid_name}" do
          before do
            allow_next_instance_of(::Packages::Nuget::UpdatePackageFromMetadataService) do |service|
              allow(service).to receive(:package_name).and_return(invalid_name)
            end
          end

          it_behaves_like 'handling the metadata error'
        end
      end
    end

    context 'with package with an invalid package version' do
      invalid_versions = [
        '',
        '555',
        '1./2.3',
        '../../../../../1.2.3',
        '%2e%2e%2f1.2.3'
      ]

      invalid_versions.each do |invalid_version|
        context "with #{invalid_version}" do
          before do
            allow_next_instance_of(::Packages::Nuget::UpdatePackageFromMetadataService) do |service|
              allow(service).to receive(:package_version).and_return(invalid_version)
            end
          end

          it_behaves_like 'handling the metadata error'
        end
      end
    end

    context 'handles a processing an unaccounted for error' do
      before do
        expect(::Packages::Nuget::UpdatePackageFromMetadataService).to receive(:new)
          .and_raise(Zip::Error)
      end

      it_behaves_like 'handling the metadata error', exception_class: Zip::Error
    end
  end
end