diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/models/packages | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) | |
download | gitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/models/packages')
-rw-r--r-- | spec/models/packages/npm_spec.rb | 24 | ||||
-rw-r--r-- | spec/models/packages/package_file_spec.rb | 67 | ||||
-rw-r--r-- | spec/models/packages/package_spec.rb | 161 |
3 files changed, 251 insertions, 1 deletions
diff --git a/spec/models/packages/npm_spec.rb b/spec/models/packages/npm_spec.rb new file mode 100644 index 00000000000..fa4adadfe06 --- /dev/null +++ b/spec/models/packages/npm_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Npm do + using RSpec::Parameterized::TableSyntax + + describe '.scope_of' do + subject { described_class.scope_of(package_name) } + + where(:package_name, :expected_result) do + nil | nil + 'test' | nil + '@test' | nil + 'test/package' | nil + '@/package' | nil + '@test/package' | 'test' + '@test/' | nil + end + + with_them do + it { is_expected.to eq(expected_result) } + end + end +end diff --git a/spec/models/packages/package_file_spec.rb b/spec/models/packages/package_file_spec.rb index ee0aeb26d50..90910fcb7ce 100644 --- a/spec/models/packages/package_file_spec.rb +++ b/spec/models/packages/package_file_spec.rb @@ -159,4 +159,71 @@ RSpec.describe Packages::PackageFile, type: :model do expect { subject }.to change { package_file.size }.from(nil).to(3513) end end + + context 'update callbacks' do + subject { package_file.save! } + + shared_examples 'executing the default callback' do + it 'executes the default callback' do + expect(package_file).to receive(:remove_previously_stored_file) + expect(package_file).not_to receive(:move_in_object_storage) + + subject + end + end + + context 'with object storage disabled' do + let(:package_file) { create(:package_file, file_name: 'file_name.txt') } + + before do + stub_package_file_object_storage(enabled: false) + end + + it_behaves_like 'executing the default callback' + + context 'with new_file_path set' do + before do + package_file.new_file_path = 'test' + end + + it_behaves_like 'executing the default callback' + end + end + + context 'with object storage enabled' do + let(:package_file) do + create( + :package_file, + file_name: 'file_name.txt', + file: CarrierWaveStringFile.new_file( + file_content: 'content', + filename: 'file_name.txt', + content_type: 'text/plain' + ), + file_store: ::Packages::PackageFileUploader::Store::REMOTE + ) + end + + before do + stub_package_file_object_storage(enabled: true) + end + + it_behaves_like 'executing the default callback' + + context 'with new_file_path set' do + before do + package_file.new_file_path = 'test' + end + + it 'executes the move_in_object_storage callback' do + expect(package_file).not_to receive(:remove_previously_stored_file) + expect(package_file).to receive(:move_in_object_storage).and_call_original + expect(package_file.file.file).to receive(:copy_to).and_call_original + expect(package_file.file.file).to receive(:delete).and_call_original + + subject + end + end + end + end end diff --git a/spec/models/packages/package_spec.rb b/spec/models/packages/package_spec.rb index 449e30f9fb7..4d4d4ad4fa9 100644 --- a/spec/models/packages/package_spec.rb +++ b/spec/models/packages/package_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' RSpec.describe Packages::Package, type: :model do include SortingHelper + using RSpec::Parameterized::TableSyntax it_behaves_like 'having unique enum values' @@ -418,7 +419,7 @@ RSpec.describe Packages::Package, type: :model do end end - describe '#package_already_taken' do + describe '#npm_package_already_taken' do context 'maven package' do let!(:package) { create(:maven_package) } @@ -428,6 +429,164 @@ RSpec.describe Packages::Package, type: :model do expect(new_package).to be_valid end end + + context 'npm package' do + let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project, namespace: group) } + let_it_be(:second_project) { create(:project, namespace: group)} + + let(:package) { build(:npm_package, project: project, name: name) } + + shared_examples 'validating the first package' do + it 'validates the first package' do + expect(package).to be_valid + end + end + + shared_examples 'validating the second package' do + it 'validates the second package' do + package.save! + + expect(second_package).to be_valid + end + end + + shared_examples 'not validating the second package' do |field_with_error:| + it 'does not validate the second package' do + package.save! + + expect(second_package).not_to be_valid + case field_with_error + when :base + expect(second_package.errors.messages[:base]).to eq ['Package already exists'] + when :name + expect(second_package.errors.messages[:name]).to eq ['has already been taken'] + else + raise ArgumentError, "field #{field_with_error} not expected" + end + end + end + + context 'following the naming convention' do + let(:name) { "@#{group.path}/test" } + + context 'with the second package in the project of the first package' do + let(:second_package) { build(:npm_package, project: project, name: second_package_name, version: second_package_version) } + + context 'with no duplicated name' do + let(:second_package_name) { "@#{group.path}/test2" } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicated name' do + let(:second_package_name) { package.name } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicate name and duplicated version' do + let(:second_package_name) { package.name } + let(:second_package_version) { package.version } + + it_behaves_like 'validating the first package' + it_behaves_like 'not validating the second package', field_with_error: :name + end + end + + context 'with the second package in a different project than the first package' do + let(:second_package) { build(:npm_package, project: second_project, name: second_package_name, version: second_package_version) } + + context 'with no duplicated name' do + let(:second_package_name) { "@#{group.path}/test2" } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicated name' do + let(:second_package_name) { package.name } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicate name and duplicated version' do + let(:second_package_name) { package.name } + let(:second_package_version) { package.version } + + it_behaves_like 'validating the first package' + it_behaves_like 'not validating the second package', field_with_error: :base + end + end + end + + context 'not following the naming convention' do + let(:name) { '@foobar/test' } + + context 'with the second package in the project of the first package' do + let(:second_package) { build(:npm_package, project: project, name: second_package_name, version: second_package_version) } + + context 'with no duplicated name' do + let(:second_package_name) { "@foobar/test2" } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicated name' do + let(:second_package_name) { package.name } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicate name and duplicated version' do + let(:second_package_name) { package.name } + let(:second_package_version) { package.version } + + it_behaves_like 'validating the first package' + it_behaves_like 'not validating the second package', field_with_error: :name + end + end + + context 'with the second package in a different project than the first package' do + let(:second_package) { build(:npm_package, project: second_project, name: second_package_name, version: second_package_version) } + + context 'with no duplicated name' do + let(:second_package_name) { "@foobar/test2" } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicated name' do + let(:second_package_name) { package.name } + let(:second_package_version) { '5.0.0' } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + + context 'with duplicate name and duplicated version' do + let(:second_package_name) { package.name } + let(:second_package_version) { package.version } + + it_behaves_like 'validating the first package' + it_behaves_like 'validating the second package' + end + end + end + end end context "recipe uniqueness for conan packages" do |