2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe DynamicPathValidator do
|
|
|
|
let(:validator) { described_class.new(attributes: [:path]) }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def expect_handles_invalid_utf8
|
|
|
|
expect { yield('\255invalid') }.to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.valid_user_path' do
|
|
|
|
it 'handles invalid utf8' do
|
|
|
|
expect(described_class.valid_user_path?("a\0weird\255path")).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.valid_group_path' do
|
|
|
|
it 'handles invalid utf8' do
|
|
|
|
expect(described_class.valid_group_path?("a\0weird\255path")).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.valid_project_path' do
|
|
|
|
it 'handles invalid utf8' do
|
|
|
|
expect(described_class.valid_project_path?("a\0weird\255path")).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#path_valid_for_record?' do
|
|
|
|
context 'for project' do
|
|
|
|
it 'calls valid_project_path?' do
|
|
|
|
project = build(:project, path: 'activity')
|
|
|
|
|
|
|
|
expect(described_class).to receive(:valid_project_path?).with(project.full_path).and_call_original
|
|
|
|
|
|
|
|
expect(validator.path_valid_for_record?(project, 'activity')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for group' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'calls valid_group_path?' do
|
2017-08-17 22:00:37 +05:30
|
|
|
group = build(:group, :nested, path: 'activity')
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(described_class).to receive(:valid_group_path?).with(group.full_path).and_call_original
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(validator.path_valid_for_record?(group, 'activity')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for user' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'calls valid_user_path?' do
|
2017-08-17 22:00:37 +05:30
|
|
|
user = build(:user, username: 'activity')
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(described_class).to receive(:valid_user_path?).with(user.full_path).and_call_original
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(validator.path_valid_for_record?(user, 'activity')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for user namespace' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'calls valid_user_path?' do
|
2017-08-17 22:00:37 +05:30
|
|
|
user = create(:user, username: 'activity')
|
|
|
|
namespace = user.namespace
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(described_class).to receive(:valid_user_path?).with(namespace.full_path).and_call_original
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(validator.path_valid_for_record?(namespace, 'activity')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validates_each' do
|
|
|
|
it 'adds a message when the path is not in the correct format' do
|
|
|
|
group = build(:group)
|
|
|
|
|
|
|
|
validator.validate_each(group, :path, "Path with spaces, and comma's!")
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(group.errors[:path]).to include(Gitlab::PathRegex.namespace_format_message)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a message when the path is not in the correct format' do
|
|
|
|
group = build(:group, path: 'users')
|
|
|
|
|
|
|
|
validator.validate_each(group, :path, 'users')
|
|
|
|
|
|
|
|
expect(group.errors[:path]).to include('users is a reserved name')
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'updating to an invalid path is not allowed' do
|
|
|
|
project = create(:project)
|
|
|
|
project.path = 'update'
|
|
|
|
|
|
|
|
validator.validate_each(project, :path, 'update')
|
|
|
|
|
|
|
|
expect(project.errors[:path]).to include('update is a reserved name')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|