From f7511caa5f26c071c61908a48440a95c5f45eb69 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Tue, 11 Apr 2017 15:51:33 +0200 Subject: Split off validating full paths The first part of a full path needs to be validated as a `top_level` while the rest need to be validated as `wildcard` --- app/validators/namespace_validator.rb | 14 ++++++++------ spec/validators/namespace_validator_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/validators/namespace_validator.rb b/app/validators/namespace_validator.rb index 4d99b09e98f..8a0e18612ec 100644 --- a/app/validators/namespace_validator.rb +++ b/app/validators/namespace_validator.rb @@ -77,12 +77,14 @@ class NamespaceValidator < ActiveModel::EachValidator STRICT_RESERVED = (TOP_LEVEL_ROUTES | WILDCARD_ROUTES) def self.valid_full_path?(full_path) - pieces = full_path.split('/') - first_part = pieces.first - pieces.all? do |namespace| - type = first_part == namespace ? :top_level : :wildcard - valid?(namespace, type: type) - end + path_segments = full_path.split('/') + root_segment = path_segments.shift + + valid?(root_segment, type: :top_level) && valid_wildcard_segments?(path_segments) + end + + def self.valid_wildcard_segments?(segments) + segments.all? { |segment| valid?(segment, type: :wildcard) } end def self.valid?(value, type: :strict) diff --git a/spec/validators/namespace_validator_spec.rb b/spec/validators/namespace_validator_spec.rb index 7ddce74939d..589175a2ced 100644 --- a/spec/validators/namespace_validator_spec.rb +++ b/spec/validators/namespace_validator_spec.rb @@ -81,6 +81,26 @@ describe NamespaceValidator do end end + describe '#valid_full_path' do + it "isn't valid when the top level is reserved" do + test_path = 'u/should-be-a/reserved-word' + + expect(described_class.valid_full_path?(test_path)).to be(false) + end + + it "isn't valid if any of the path segments is reserved" do + test_path = 'the-wildcard/wikis/is-a-reserved-path' + + expect(described_class.valid_full_path?(test_path)).to be(false) + end + + it "is valid if the path doen't contain reserved words" do + test_path = 'there-are/no-wildcards/in-this-path' + + expect(described_class.valid_full_path?(test_path)).to be(true) + end + end + describe '#validation_type' do it 'uses top level validation for groups without parent' do group = build(:group) -- cgit v1.2.1