diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 13:18:24 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 13:18:24 +0000 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/models/namespace_spec.rb | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) | |
download | gitlab-ce-0653e08efd039a5905f3fa4f6e9cef9f5d2f799c.tar.gz |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/models/namespace_spec.rb')
-rw-r--r-- | spec/models/namespace_spec.rb | 117 |
1 files changed, 98 insertions, 19 deletions
diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index e2700378f5f..51a26d82daa 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -36,27 +36,34 @@ RSpec.describe Namespace do it { is_expected.to validate_numericality_of(:max_artifacts_size).only_integer.is_greater_than(0) } context 'validating the parent of a namespace' do - context 'when the namespace has no parent' do - it 'allows a namespace to have no parent associated with it' do - namespace = build(:namespace) - - expect(namespace).to be_valid - end + using RSpec::Parameterized::TableSyntax + + where(:parent_type, :child_type, :error) do + nil | 'User' | nil + nil | 'Group' | nil + nil | 'Project' | 'must be set for a project namespace' + 'Project' | 'User' | 'project namespace cannot be the parent of another namespace' + 'Project' | 'Group' | 'project namespace cannot be the parent of another namespace' + 'Project' | 'Project' | 'project namespace cannot be the parent of another namespace' + 'Group' | 'User' | 'cannot not be used for user namespace' + 'Group' | 'Group' | nil + 'Group' | 'Project' | nil + 'User' | 'User' | 'cannot not be used for user namespace' + 'User' | 'Group' | 'user namespace cannot be the parent of another namespace' + 'User' | 'Project' | nil end - context 'when the namespace has a parent' do - it 'does not allow a namespace to have a group as its parent' do - namespace = build(:namespace, parent: build(:group)) - - expect(namespace).not_to be_valid - expect(namespace.errors[:parent_id].first).to eq('a user namespace cannot have a parent') - end - - it 'does not allow a namespace to have another namespace as its parent' do - namespace = build(:namespace, parent: build(:namespace)) - - expect(namespace).not_to be_valid - expect(namespace.errors[:parent_id].first).to eq('a user namespace cannot have a parent') + with_them do + it 'validates namespace parent' do + parent = build(:namespace, type: parent_type) if parent_type + namespace = build(:namespace, type: child_type, parent: parent) + + if error + expect(namespace).not_to be_valid + expect(namespace.errors[:parent_id].first).to eq(error) + else + expect(namespace).to be_valid + end end end @@ -157,6 +164,65 @@ RSpec.describe Namespace do end end + describe 'handling STI', :aggregate_failures do + let(:namespace_type) { nil } + let(:parent) { nil } + let(:namespace) { Namespace.find(create(:namespace, type: namespace_type, parent: parent).id) } + + context 'creating a Group' do + let(:namespace_type) { 'Group' } + + it 'is valid' do + expect(namespace).to be_a(Group) + expect(namespace.kind).to eq('group') + expect(namespace.group?).to be_truthy + end + end + + context 'creating a ProjectNamespace' do + let(:namespace_type) { 'Project' } + let(:parent) { create(:group) } + + it 'is valid' do + expect(Namespace.find(namespace.id)).to be_a(Namespaces::ProjectNamespace) + expect(namespace.kind).to eq('project') + expect(namespace.project?).to be_truthy + end + end + + context 'creating a UserNamespace' do + let(:namespace_type) { 'User' } + + it 'is valid' do + # TODO: We create a normal Namespace until + # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68894 is ready + expect(Namespace.find(namespace.id)).to be_a(Namespace) + expect(namespace.kind).to eq('user') + expect(namespace.user?).to be_truthy + end + end + + context 'creating a default Namespace' do + let(:namespace_type) { nil } + + it 'is valid' do + expect(Namespace.find(namespace.id)).to be_a(Namespace) + expect(namespace.kind).to eq('user') + expect(namespace.user?).to be_truthy + end + end + + context 'creating an unknown Namespace type' do + let(:namespace_type) { 'One' } + + it 'defaults to a Namespace' do + expect(Namespace.find(namespace.id)).to be_a(Namespace) + expect(namespace.kind).to eq('user') + expect(namespace.user?).to be_truthy + end + end + end + describe 'scopes', :aggregate_failures do let_it_be(:namespace1) { create(:group, name: 'Namespace 1', path: 'namespace-1') } let_it_be(:namespace2) { create(:group, name: 'Namespace 2', path: 'namespace-2') } @@ -287,6 +353,12 @@ RSpec.describe Namespace do end end + describe '#owner_required?' do + specify { expect(build(:project_namespace).owner_required?).to be_falsey } + specify { expect(build(:group).owner_required?).to be_falsey } + specify { expect(build(:namespace).owner_required?).to be_truthy } + end + describe '#visibility_level_field' do it { expect(namespace.visibility_level_field).to eq(:visibility_level) } end @@ -1377,6 +1449,13 @@ RSpec.describe Namespace do expect { root_group.root_ancestor }.not_to exceed_query_limit(0) end + it 'returns root_ancestor for nested group with a single query' do + nested_group = create(:group, parent: root_group) + nested_group.reload + + expect { nested_group.root_ancestor }.not_to exceed_query_limit(1) + end + it 'returns the top most ancestor' do nested_group = create(:group, parent: root_group) deep_nested_group = create(:group, parent: nested_group) |