summaryrefslogtreecommitdiff
path: root/spec/models/packages/package_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
commitb76ae638462ab0f673e5915986070518dd3f9ad3 (patch)
treebdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/models/packages/package_spec.rb
parent434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff)
downloadgitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/models/packages/package_spec.rb')
-rw-r--r--spec/models/packages/package_spec.rb161
1 files changed, 160 insertions, 1 deletions
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