Skip to content
Snippets Groups Projects
Commit ec1bd88b authored by Jan Provaznik's avatar Jan Provaznik
Browse files

Validate project namespace parent

Validate the project namespace can be added only
to user/group namespaces and project namespace must
always have a parent.
parent 74729c56
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -531,7 +531,17 @@ def nesting_level_allowed
end
 
def validate_parent_type
return unless has_parent?
unless has_parent?
if project?
errors.add(:parent_id, 'a project namespace must have a parent')
end
return
end
if parent.project?
errors.add(:parent_id, 'a project namespace cannot be the parent of another namespace')
end
 
if user?
errors.add(:parent_id, 'a user namespace cannot have a parent')
Loading
Loading
Loading
Loading
@@ -36,27 +36,34 @@
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' | 'a project namespace must have a parent'
'Project' | 'User' | 'a project namespace cannot be the parent of another namespace'
'Project' | 'Group' | 'a project namespace cannot be the parent of another namespace'
'Project' | 'Project' | 'a project namespace cannot be the parent of another namespace'
'Group' | 'User' | 'a user namespace cannot have a parent'
'Group' | 'Group' | nil
'Group' | 'Project' | nil
'User' | 'User' | 'a user namespace cannot have a parent'
'User' | 'Group' | 'a group cannot have a user namespace as its parent'
'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
 
Loading
Loading
@@ -159,7 +166,8 @@
 
describe 'handling STI', :aggregate_failures do
let(:namespace_type) { nil }
let(:namespace) { Namespace.find(create(:namespace, type: namespace_type).id) }
let(:parent) { nil }
let(:namespace) { Namespace.find(create(:namespace, type: namespace_type, parent: parent).id) }
 
context 'creating a Group' do
let(:namespace_type) { 'Group' }
Loading
Loading
@@ -173,6 +181,7 @@
 
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)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment