diff options
Diffstat (limited to 'spec/services/packages')
6 files changed, 267 insertions, 20 deletions
diff --git a/spec/services/packages/debian/create_package_file_service_spec.rb b/spec/services/packages/debian/create_package_file_service_spec.rb index c8292b2d5c2..291f6df991c 100644 --- a/spec/services/packages/debian/create_package_file_service_spec.rb +++ b/spec/services/packages/debian/create_package_file_service_spec.rb @@ -6,6 +6,7 @@ RSpec.describe Packages::Debian::CreatePackageFileService do include WorkhorseHelpers let_it_be(:package) { create(:debian_incoming, without_package_files: true) } + let_it_be(:current_user) { create(:user) } describe '#execute' do let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' } @@ -20,12 +21,13 @@ RSpec.describe Packages::Debian::CreatePackageFileService do }.with_indifferent_access end - let(:service) { described_class.new(package, params) } + let(:service) { described_class.new(package: package, current_user: current_user, params: params) } subject(:package_file) { service.execute } shared_examples 'a valid deb' do it 'creates a new package file', :aggregate_failures do + expect(::Packages::Debian::ProcessChangesWorker).not_to receive(:perform_async) expect(package_file).to be_valid expect(package_file.file.read).to start_with('!<arch>') expect(package_file.size).to eq(1124) @@ -40,6 +42,24 @@ RSpec.describe Packages::Debian::CreatePackageFileService do end end + shared_examples 'a valid changes' do + it 'creates a new package file', :aggregate_failures do + expect(::Packages::Debian::ProcessChangesWorker).to receive(:perform_async) + + expect(package_file).to be_valid + expect(package_file.file.read).to start_with('Format: 1.8') + expect(package_file.size).to eq(2143) + expect(package_file.file_name).to eq(file_name) + expect(package_file.file_sha1).to eq('54321') + expect(package_file.file_sha256).to eq('543212345') + expect(package_file.file_md5).to eq('12345') + expect(package_file.debian_file_metadatum).to be_valid + expect(package_file.debian_file_metadatum.file_type).to eq('unknown') + expect(package_file.debian_file_metadatum.architecture).to be_nil + expect(package_file.debian_file_metadatum.fields).to be_nil + end + end + context 'with temp file' do let!(:file) do upload_path = ::Packages::PackageFileUploader.workhorse_local_upload_path @@ -52,6 +72,21 @@ RSpec.describe Packages::Debian::CreatePackageFileService do end it_behaves_like 'a valid deb' + + context 'with a .changes file' do + let(:file_name) { 'sample_1.2.3~alpha2_amd64.changes' } + let(:fixture_path) { "spec/fixtures/packages/debian/#{file_name}" } + + it_behaves_like 'a valid changes' + end + + context 'when current_user is missing' do + let(:current_user) { nil } + + it 'raises an error' do + expect { package_file }.to raise_error(ArgumentError, 'Invalid user') + end + end end context 'with remote file' do @@ -77,37 +112,37 @@ RSpec.describe Packages::Debian::CreatePackageFileService do it_behaves_like 'a valid deb' end - context 'package is missing' do + context 'when package is missing' do let(:package) { nil } let(:params) { {} } it 'raises an error' do - expect { subject.execute }.to raise_error(ArgumentError, 'Invalid package') + expect { package_file }.to raise_error(ArgumentError, 'Invalid package') end end - context 'params is empty' do + context 'when params is empty' do let(:params) { {} } it 'raises an error' do - expect { subject.execute }.to raise_error(ActiveRecord::RecordInvalid) + expect { package_file }.to raise_error(ActiveRecord::RecordInvalid) end end - context 'file is missing' do + context 'when file is missing' do let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' } let(:file) { nil } it 'raises an error' do - expect { subject.execute }.to raise_error(ActiveRecord::RecordInvalid) + expect { package_file }.to raise_error(ActiveRecord::RecordInvalid) end end - context 'FIPS mode enabled', :fips_mode do + context 'when FIPS mode enabled', :fips_mode do let(:file) { nil } it 'raises an error' do - expect { subject.execute }.to raise_error(::Packages::FIPS::DisabledError) + expect { package_file }.to raise_error(::Packages::FIPS::DisabledError) end end end diff --git a/spec/services/packages/mark_packages_for_destruction_service_spec.rb b/spec/services/packages/mark_packages_for_destruction_service_spec.rb new file mode 100644 index 00000000000..5c043b89de8 --- /dev/null +++ b/spec/services/packages/mark_packages_for_destruction_service_spec.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::MarkPackagesForDestructionService, :sidekiq_inline do + let_it_be(:project) { create(:project) } + let_it_be_with_reload(:packages) { create_list(:npm_package, 3, project: project) } + + let(:user) { project.owner } + + # The service only accepts ActiveRecord relationships and not arrays. + let(:service) { described_class.new(packages: ::Packages::Package.id_in(package_ids), current_user: user) } + let(:package_ids) { packages.map(&:id) } + + describe '#execute' do + subject { service.execute } + + context 'when the user is authorized' do + before do + project.add_maintainer(user) + end + + context 'when it is successful' do + it 'marks the packages as pending destruction' do + expect(::Packages::Maven::Metadata::SyncService).not_to receive(:new) + + expect { subject }.to change { ::Packages::Package.pending_destruction.count }.from(0).to(3) + .and change { Packages::PackageFile.pending_destruction.count }.from(0).to(3) + packages.each { |pkg| expect(pkg.reload).to be_pending_destruction } + + expect(subject).to be_a(ServiceResponse) + expect(subject).to be_success + expect(subject.message).to eq('Packages were successfully marked as pending destruction') + end + + context 'with maven packages' do + let_it_be_with_reload(:packages) { create_list(:maven_package, 3, project: project) } + + it 'marks the packages as pending destruction' do + expect(::Packages::Maven::Metadata::SyncService).to receive(:new).once.and_call_original + + expect { subject }.to change { ::Packages::Package.pending_destruction.count }.from(0).to(3) + .and change { Packages::PackageFile.pending_destruction.count }.from(0).to(9) + packages.each { |pkg| expect(pkg.reload).to be_pending_destruction } + + expect(subject).to be_a(ServiceResponse) + expect(subject).to be_success + expect(subject.message).to eq('Packages were successfully marked as pending destruction') + end + + context 'without version' do + before do + ::Packages::Package.id_in(package_ids).update_all(version: nil) + end + + it 'marks the packages as pending destruction' do + expect(::Packages::Maven::Metadata::SyncService).not_to receive(:new) + + expect { subject }.to change { ::Packages::Package.pending_destruction.count }.from(0).to(3) + .and change { Packages::PackageFile.pending_destruction.count }.from(0).to(9) + packages.each { |pkg| expect(pkg.reload).to be_pending_destruction } + + expect(subject).to be_a(ServiceResponse) + expect(subject).to be_success + expect(subject.message).to eq('Packages were successfully marked as pending destruction') + end + end + end + end + + context 'when it is not successful' do + before do + allow(service).to receive(:can_destroy_packages?).and_raise(StandardError, 'test') + end + + it 'returns an error ServiceResponse' do + expect(::Packages::Maven::Metadata::SyncService).not_to receive(:new) + + expect { subject }.to not_change { ::Packages::Package.pending_destruction.count } + .and not_change { ::Packages::PackageFile.pending_destruction.count } + + expect(subject).to be_a(ServiceResponse) + expect(subject).to be_error + expect(subject.message).to eq("Failed to mark the packages as pending destruction") + expect(subject.status).to eq(:error) + end + end + end + + context 'when the user is not authorized' do + let(:user) { nil } + + it 'returns an error ServiceResponse' do + expect(::Packages::Maven::Metadata::SyncService).not_to receive(:new) + + expect { subject }.to not_change { ::Packages::Package.pending_destruction.count } + .and not_change { ::Packages::PackageFile.pending_destruction.count } + + expect(subject).to be_a(ServiceResponse) + expect(subject).to be_error + expect(subject.message).to eq("You don't have the permission to perform this action") + expect(subject.status).to eq(:error) + expect(subject.reason).to eq(:unauthorized) + end + end + end +end diff --git a/spec/services/packages/rpm/parse_package_service_spec.rb b/spec/services/packages/rpm/parse_package_service_spec.rb new file mode 100644 index 00000000000..f330587bfa0 --- /dev/null +++ b/spec/services/packages/rpm/parse_package_service_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Rpm::ParsePackageService do + let(:package_file) { File.open('spec/fixtures/packages/rpm/hello-0.0.1-1.fc29.x86_64.rpm') } + + describe 'dynamic private methods' do + described_class::BUILD_ATTRIBUTES_METHOD_NAMES.each do |attribute| + it 'define dynamic build attribute method' do + expect(described_class).to be_private_method_defined("build_#{attribute}") + end + end + end + + describe '#execute' do + subject { described_class.new(package_file).execute } + + shared_examples 'valid package parsing' do + it 'return hash' do + expect(subject).to be_a(Hash) + end + + it 'has all static attribute keys' do + expect(subject.keys).to include(*described_class::STATIC_ATTRIBUTES) + end + + it 'includes epoch attribute' do + expect(subject[:epoch]).not_to be_blank + end + + it 'has all built attributes with array values' do + result = subject + described_class::BUILD_ATTRIBUTES_METHOD_NAMES.each do |attribute| + expect(result).to have_key(attribute) + expect(result[attribute]).to be_a(Array) + end + end + end + + context 'when wrong format file received' do + let(:package_file) { File.open('spec/fixtures/rails_sample.jpg') } + + it 'raise error' do + expect { subject }.to raise_error(ArgumentError) + end + end + + context 'when valid file uploaded' do + context 'when .rpm file uploaded' do + it_behaves_like 'valid package parsing' + end + + context 'when .src.rpm file uploaded' do + let(:package_file) { File.open('spec/fixtures/packages/rpm/hello-0.0.1-1.fc29.src.rpm') } + + it_behaves_like 'valid package parsing' + end + end + end +end diff --git a/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb b/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb index 0fb58cc27d5..524c224177b 100644 --- a/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb +++ b/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb @@ -3,7 +3,10 @@ require 'spec_helper' RSpec.describe Packages::Rpm::RepositoryMetadata::BaseBuilder do describe '#execute' do - subject { described_class.new.execute } + subject { described_class.new(xml: xml, data: data).execute } + + let(:xml) { nil } + let(:data) { {} } before do stub_const("#{described_class}::ROOT_TAG", 'test') @@ -18,5 +21,13 @@ RSpec.describe Packages::Rpm::RepositoryMetadata::BaseBuilder do expect(result.children.first.attributes['foo1'].value).to eq('bar1') expect(result.children.first.attributes['foo2'].value).to eq('bar2') end + + context 'when call with parameters' do + let(:xml) { 'test' } + + it 'raise NotImplementedError' do + expect { subject }.to raise_error NotImplementedError + end + end end end diff --git a/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb index f5294d6f7f7..147d5862a71 100644 --- a/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb +++ b/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb @@ -3,18 +3,32 @@ require 'spec_helper' RSpec.describe Packages::Rpm::RepositoryMetadata::BuildPrimaryXml do describe '#execute' do - subject { described_class.new.execute } + subject { described_class.new(xml: xml, data: data).execute } - context "when generate empty xml" do - let(:expected_xml) do - <<~XML - <?xml version="1.0" encoding="UTF-8"?> - <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"/> - XML - end + let(:empty_xml) do + <<~XML + <?xml version="1.0" encoding="UTF-8"?> + <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"/> + XML + end + + it_behaves_like 'handling rpm xml file' + + context 'when updating existing xml' do + include_context 'with rpm package data' + + let(:xml) { empty_xml } + let(:data) { xml_update_params } + let(:required_text_only_attributes) { %i[description summary arch name] } + + it 'adds node with required_text_only_attributes' do + result = Nokogiri::XML::Document.parse(subject).remove_namespaces! - it 'generate expected xml' do - expect(subject).to eq(expected_xml) + required_text_only_attributes.each do |attribute| + expect( + result.at("//#{described_class::ROOT_TAG}/package/#{attribute}").text + ).to eq(data[attribute]) + end end end end diff --git a/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb index 29b0f73e3c1..0843a983b7e 100644 --- a/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb +++ b/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb @@ -62,5 +62,25 @@ RSpec.describe Packages::Rpm::RepositoryMetadata::BuildRepomdXml do end end end + + context 'when data values has unexpected keys' do + let(:data) do + { + filelists: described_class::ALLOWED_DATA_VALUE_KEYS.each_with_object({}) do |key, result| + result[:"#{key}-wrong"] = { value: 'value' } + end + } + end + + it 'ignores wrong keys' do + result = Nokogiri::XML::Document.parse(subject).remove_namespaces! + + data.each do |tag_name, tag_attributes| + tag_attributes.each_key do |key| + expect(result.at("//repomd/data[@type=\"#{tag_name}\"]/#{key}")).to be_nil + end + end + end + end end end |