Commit f83a51fc authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '341823-cablett-namespace-scope-validations' into 'master'

Do not validate ProjectNamespaces for hierarchy

See merge request gitlab-org/gitlab!72758
parents 046f6c4f bf4e1ff7
......@@ -93,9 +93,11 @@ class Namespace < ApplicationRecord
validates :max_artifacts_size, numericality: { only_integer: true, greater_than: 0, allow_nil: true }
validate :validate_parent_type, if: -> { Feature.enabled?(:validate_namespace_parent_type, default_enabled: :yaml) }
validate :nesting_level_allowed
validate :changing_shared_runners_enabled_is_allowed
validate :changing_allow_descendants_override_disabled_shared_runners_is_allowed
# ProjectNamespaces excluded as they are not meant to appear in the group hierarchy at the moment.
validate :nesting_level_allowed, unless: -> { project_namespace? }
validate :changing_shared_runners_enabled_is_allowed, unless: -> { project_namespace? }
validate :changing_allow_descendants_override_disabled_shared_runners_is_allowed, unless: -> { project_namespace? }
delegate :name, to: :owner, allow_nil: true, prefix: true
delegate :avatar_url, to: :owner, allow_nil: true
......
......@@ -40,6 +40,30 @@ RSpec.describe Namespace do
end
end
shared_examples 'validations called by different namespace types' do |method|
using RSpec::Parameterized::TableSyntax
where(:namespace_type, :call_validation) do
:namespace | true
:group | true
:user_namespace | true
:project_namespace | false
end
with_them do
it 'conditionally runs given validation' do
namespace = build(namespace_type)
if call_validation
expect(namespace).to receive(method)
else
expect(namespace).not_to receive(method)
end
namespace.valid?
end
end
end
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
......@@ -112,6 +136,8 @@ RSpec.describe Namespace do
end
end
describe '#nesting_level_allowed' do
context 'for a group' do
it 'does not allow too deep nesting' do
ancestors = (1..21).to_a
group = build(:group)
......@@ -121,6 +147,10 @@ RSpec.describe Namespace do
expect(group).not_to be_valid
expect(group.errors[:parent_id].first).to eq('has too deep level of nesting')
end
end
it_behaves_like 'validations called by different namespace types', :nesting_level_allowed
end
describe 'reserved path validation' do
context 'nested group' do
......@@ -1855,6 +1885,7 @@ RSpec.describe Namespace do
end
context 'with a parent' do
context 'when namespace is a group' do
context 'when parent has shared runners disabled' do
let(:parent) { create(:group, :shared_runners_disabled) }
let(:group) { build(:group, shared_runners_enabled: true, parent_id: parent.id) }
......@@ -1885,10 +1916,14 @@ RSpec.describe Namespace do
end
end
it_behaves_like 'validations called by different namespace types', :changing_shared_runners_enabled_is_allowed
end
describe 'validation #changing_allow_descendants_override_disabled_shared_runners_is_allowed' do
context 'when namespace is a group' do
context 'without a parent' do
context 'with shared runners disabled' do
let(:namespace) { build(:namespace, :allow_descendants_override_disabled_shared_runners, :shared_runners_disabled) }
let(:namespace) { build(:group, :allow_descendants_override_disabled_shared_runners, :shared_runners_disabled) }
it 'is valid' do
expect(namespace).to be_valid
......@@ -1938,6 +1973,9 @@ RSpec.describe Namespace do
end
end
it_behaves_like 'validations called by different namespace types', :changing_allow_descendants_override_disabled_shared_runners_is_allowed
end
describe '#root?' do
subject { namespace.root? }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment