Read about our upcoming Code of Conduct on this issue

dynamic_path_validator_spec.rb 2.85 KB
Newer Older
1
2
require 'spec_helper'

3
describe DynamicPathValidator do
4
  let(:validator) { described_class.new(attributes: [:path]) }
5

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  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

28
29
30
  describe '#path_valid_for_record?' do
    context 'for project' do
      it 'calls valid_project_path?' do
31
        project = build(:project, path: 'activity')
32

33
        expect(described_class).to receive(:valid_project_path?).with(project.full_path).and_call_original
34

35
        expect(validator.path_valid_for_record?(project, 'activity')).to be_truthy
36
      end
37
38
    end

39
    context 'for group' do
40
      it 'calls valid_group_path?' do
41
        group = build(:group, :nested, path: 'activity')
42

43
        expect(described_class).to receive(:valid_group_path?).with(group.full_path).and_call_original
44

45
        expect(validator.path_valid_for_record?(group, 'activity')).to be_falsey
46
      end
47
    end
48

49
    context 'for user' do
50
      it 'calls valid_user_path?' do
51
        user = build(:user, username: 'activity')
52

53
        expect(described_class).to receive(:valid_user_path?).with(user.full_path).and_call_original
54

55
56
        expect(validator.path_valid_for_record?(user, 'activity')).to be_truthy
      end
57
    end
58

59
    context 'for user namespace' do
60
      it 'calls valid_user_path?' do
61
62
        user = create(:user, username: 'activity')
        namespace = user.namespace
63

64
        expect(described_class).to receive(:valid_user_path?).with(namespace.full_path).and_call_original
65

66
67
        expect(validator.path_valid_for_record?(namespace, 'activity')).to be_truthy
      end
68
69
70
71
72
73
74
75
76
    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!")

77
      expect(group.errors[:path]).to include(Gitlab::PathRegex.namespace_format_message)
78
79
80
81
82
83
84
85
86
    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
87
88

    it 'updating to an invalid path is not allowed' do
89
      project = create(:project)
90
91
92
93
94
95
      project.path = 'update'

      validator.validate_each(project, :path, 'update')

      expect(project.errors[:path]).to include('update is a reserved name')
    end
96
  end
97
end