debian-mirror-gitlab/spec/services/packages/nuget/metadata_extraction_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.6 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Nuget::MetadataExtractionService do
2021-09-04 01:27:46 +05:30
let_it_be(:package_file) { create(:nuget_package).package_files.first }
2020-07-28 23:09:34 +05:30
let(:service) { described_class.new(package_file.id) }
describe '#execute' do
subject { service.execute }
context 'with valid package file id' do
expected_metadata = {
package_name: 'DummyProject.DummyPackage',
package_version: '1.0.0',
package_dependencies: [
{
name: 'Newtonsoft.Json',
target_framework: '.NETCoreApp3.0',
version: '12.0.3'
}
],
2021-09-30 23:02:18 +05:30
package_tags: [],
package_types: []
2020-07-28 23:09:34 +05:30
}
it { is_expected.to eq(expected_metadata) }
end
context 'with nuspec file' do
before do
2021-09-04 01:27:46 +05:30
allow(service).to receive(:nuspec_file_content).and_return(fixture_file(nuspec_filepath))
2020-07-28 23:09:34 +05:30
end
context 'with dependencies' do
let(:nuspec_filepath) { 'packages/nuget/with_dependencies.nuspec' }
it { is_expected.to have_key(:package_dependencies) }
it 'extracts dependencies' do
dependencies = subject[:package_dependencies]
expect(dependencies).to include(name: 'Moqi', version: '2.5.6')
expect(dependencies).to include(name: 'Castle.Core')
expect(dependencies).to include(name: 'Test.Dependency', version: '2.3.7', target_framework: '.NETStandard2.0')
expect(dependencies).to include(name: 'Newtonsoft.Json', version: '12.0.3', target_framework: '.NETStandard2.0')
end
end
2021-09-30 23:02:18 +05:30
context 'with package types' do
let(:nuspec_filepath) { 'packages/nuget/with_package_types.nuspec' }
it { is_expected.to have_key(:package_types) }
it 'extracts package types' do
expect(subject[:package_types]).to include('SymbolsPackage')
end
end
2020-07-28 23:09:34 +05:30
context 'with a nuspec file with metadata' do
let(:nuspec_filepath) { 'packages/nuget/with_metadata.nuspec' }
it { expect(subject[:package_tags].sort).to eq(%w(foo bar test tag1 tag2 tag3 tag4 tag5).sort) }
end
end
context 'with a nuspec file with metadata' do
let_it_be(:nuspec_filepath) { 'packages/nuget/with_metadata.nuspec' }
before do
2021-09-04 01:27:46 +05:30
allow(service).to receive(:nuspec_file_content).and_return(fixture_file(nuspec_filepath))
2020-07-28 23:09:34 +05:30
end
it { expect(subject[:license_url]).to eq('https://opensource.org/licenses/MIT') }
it { expect(subject[:project_url]).to eq('https://gitlab.com/gitlab-org/gitlab') }
it { expect(subject[:icon_url]).to eq('https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png') }
end
context 'with invalid package file id' do
2022-04-04 11:22:00 +05:30
let(:package_file) { double('file', id: 555) }
2020-07-28 23:09:34 +05:30
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, 'invalid package file') }
end
context 'linked to a non nuget package' do
before do
package_file.package.maven!
end
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, 'invalid package file') }
end
context 'with a 0 byte package file id' do
before do
allow_any_instance_of(Packages::PackageFileUploader).to receive(:size).and_return(0)
end
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, 'invalid package file') }
end
context 'without the nuspec file' do
before do
allow_any_instance_of(Zip::File).to receive(:glob).and_return([])
end
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, 'nuspec file not found') }
end
context 'with a too big nuspec file' do
before do
2022-04-04 11:22:00 +05:30
allow_any_instance_of(Zip::File).to receive(:glob).and_return([double('file', size: 6.megabytes)])
2020-07-28 23:09:34 +05:30
end
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, 'nuspec file too big') }
end
2023-01-10 11:22:00 +05:30
context 'with a corrupted nupkg file with a wrong entry size' do
let(:nupkg_fixture_path) { expand_fixture_path('packages/nuget/corrupted_package.nupkg') }
let(:expected_error) { "nuspec file has the wrong entry size: entry 'DummyProject.DummyPackage.nuspec' should be 255B, but is larger when inflated." }
before do
allow(Zip::File).to receive(:new).and_return(Zip::File.new(nupkg_fixture_path, false, false))
end
it { expect { subject }.to raise_error(::Packages::Nuget::MetadataExtractionService::ExtractionError, expected_error) }
end
2020-07-28 23:09:34 +05:30
end
end