debian-mirror-gitlab/spec/services/packages/pypi/create_package_service_spec.rb

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

145 lines
4.6 KiB
Ruby
Raw Permalink Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Packages::Pypi::CreatePackageService, :aggregate_failures, feature_category: :package_registry do
2020-07-28 23:09:34 +05:30
include PackagesManagerApiSpecHelpers
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
2020-11-24 15:15:51 +05:30
2022-05-03 16:02:30 +05:30
let(:sha256) { '1' * 64 }
let(:md5) { '567' }
2020-11-24 15:15:51 +05:30
let(:requires_python) { '>=2.7' }
let(:params) do
2020-07-28 23:09:34 +05:30
{
name: 'foo',
version: '1.0',
content: temp_file('foo.tgz'),
2020-11-24 15:15:51 +05:30
requires_python: requires_python,
2022-05-03 16:02:30 +05:30
sha256_digest: sha256,
md5_digest: md5
2020-07-28 23:09:34 +05:30
}
end
describe '#execute' do
subject { described_class.new(project, user, params).execute }
let(:created_package) { Packages::Package.pypi.last }
context 'without an existing package' do
it 'creates the package' do
expect { subject }.to change { Packages::Package.pypi.count }.by(1)
expect(created_package.name).to eq 'foo'
expect(created_package.version).to eq '1.0'
expect(created_package.pypi_metadatum.required_python).to eq '>=2.7'
expect(created_package.package_files.size).to eq 1
expect(created_package.package_files.first.file_name).to eq 'foo.tgz'
2022-05-03 16:02:30 +05:30
expect(created_package.package_files.first.file_sha256).to eq sha256
expect(created_package.package_files.first.file_md5).to eq md5
2020-07-28 23:09:34 +05:30
end
end
2022-08-13 15:12:31 +05:30
context 'with FIPS mode', :fips_mode do
it 'does not generate file_md5' do
expect { subject }.to change { Packages::Package.pypi.count }.by(1)
expect(created_package.name).to eq 'foo'
expect(created_package.version).to eq '1.0'
expect(created_package.pypi_metadatum.required_python).to eq '>=2.7'
expect(created_package.package_files.size).to eq 1
expect(created_package.package_files.first.file_name).to eq 'foo.tgz'
expect(created_package.package_files.first.file_sha256).to eq sha256
expect(created_package.package_files.first.file_md5).to be_nil
end
end
2022-05-07 20:08:51 +05:30
context 'without required_python' do
before do
params.delete(:requires_python)
end
it 'creates the package' do
expect { subject }.to change { Packages::Package.pypi.count }.by(1)
expect(created_package.pypi_metadatum.required_python).to eq ''
end
end
2020-11-24 15:15:51 +05:30
context 'with an invalid metadata' do
let(:requires_python) { 'x' * 256 }
it 'raises an error' do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
end
end
it_behaves_like 'assigns the package creator' do
let(:package) { created_package }
end
2021-02-22 17:27:13 +05:30
it_behaves_like 'assigns build to package'
2021-03-11 19:13:27 +05:30
it_behaves_like 'assigns status to package'
2021-02-22 17:27:13 +05:30
2020-07-28 23:09:34 +05:30
context 'with an existing package' do
before do
described_class.new(project, user, params).execute
end
context 'with an existing file' do
before do
params[:content] = temp_file('foo.tgz')
2022-05-03 16:02:30 +05:30
params[:sha256_digest] = sha256
params[:md5_digest] = md5
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
it 'throws an error' do
2020-07-28 23:09:34 +05:30
expect { subject }
.to change { Packages::Package.pypi.count }.by(0)
2020-10-24 23:57:45 +05:30
.and change { Packages::PackageFile.count }.by(0)
.and raise_error(/File name has already been taken/)
2020-07-28 23:09:34 +05:30
end
2022-03-02 08:16:31 +05:30
2022-05-07 20:08:51 +05:30
context 'with a pending_destruction package' do
2022-03-02 08:16:31 +05:30
before do
Packages::Package.pypi.last.pending_destruction!
end
it 'creates a new package' do
expect { subject }
.to change { Packages::Package.pypi.count }.by(1)
.and change { Packages::PackageFile.count }.by(1)
expect(created_package.name).to eq 'foo'
expect(created_package.version).to eq '1.0'
expect(created_package.pypi_metadatum.required_python).to eq '>=2.7'
expect(created_package.package_files.size).to eq 1
expect(created_package.package_files.first.file_name).to eq 'foo.tgz'
2022-05-03 16:02:30 +05:30
expect(created_package.package_files.first.file_sha256).to eq sha256
expect(created_package.package_files.first.file_md5).to eq md5
2022-03-02 08:16:31 +05:30
end
end
2020-07-28 23:09:34 +05:30
end
context 'without an existing file' do
before do
params[:content] = temp_file('another.tgz')
end
it 'adds the file' do
expect { subject }
.to change { Packages::Package.pypi.count }.by(0)
.and change { Packages::PackageFile.count }.by(1)
expect(created_package.package_files.size).to eq 2
expect(created_package.package_files.map(&:file_name).sort).to eq ['another.tgz', 'foo.tgz']
end
end
end
end
end