BigW Consortium Gitlab

path_spec.rb 6.45 KB
Newer Older
1 2 3
require 'spec_helper'

describe ContainerRegistry::Path do
4
  subject { described_class.new(path) }
5

6
  describe '#components' do
7 8
    let(:path) { 'path/to/some/project' }

9 10
    it 'splits components by a forward slash' do
      expect(subject.components).to eq %w[path to some project]
11 12 13
    end
  end

14
  describe '#nodes' do
15
    context 'when repository path is valid' do
16
      let(:path) { 'path/to/some/project' }
17

18 19 20 21
      it 'return all project path like node in reverse order' do
        expect(subject.nodes).to eq %w[path/to/some/project
                                       path/to/some
                                       path/to]
22 23 24 25
      end
    end

    context 'when repository path is invalid' do
26
      let(:path) { '' }
27 28

      it 'rasises en error' do
29
        expect { subject.nodes }
30 31 32 33 34
          .to raise_error described_class::InvalidRegistryPathError
      end
    end
  end

35
  describe '#to_s' do
36 37
    context 'when path does not have uppercase characters' do
      let(:path) { 'some/image' }
38

39 40 41 42 43 44 45 46 47 48 49
      it 'return a string with a repository path' do
        expect(subject.to_s).to eq 'some/image'
      end
    end

    context 'when path has uppercase characters' do
      let(:path) { 'SoMe/ImAgE' }

      it 'return a string with a repository path' do
        expect(subject.to_s).to eq 'some/image'
      end
50 51 52
    end
  end

53 54
  describe '#valid?' do
    context 'when path has less than two components' do
55
      let(:path) { 'something/' }
56

57
      it { is_expected.not_to be_valid }
58
    end
59 60

    context 'when path has more than allowed number of components' do
61
      let(:path) { 'a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/r/s/t/u/w/y/z' }
62

63 64 65 66 67 68 69
      it { is_expected.not_to be_valid }
    end

    context 'when path has invalid characters' do
      let(:path) { 'some\path' }

      it { is_expected.not_to be_valid }
70 71 72
    end

    context 'when path has two or more components' do
73
      let(:path) { 'some/path' }
74

75 76 77 78 79 80 81
      it { is_expected.to be_valid }
    end

    context 'when path is related to multi-level image' do
      let(:path) { 'some/path/my/image' }

      it { is_expected.to be_valid }
82
    end
83 84 85 86 87 88

    context 'when path contains uppercase letters' do
      let(:path) { 'Some/Registry' }

      it { is_expected.to be_valid }
    end
89 90
  end

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  describe '#has_repository?' do
    context 'when project exists' do
      let(:project) { create(:empty_project) }
      let(:path) { "#{project.full_path}/my/image" }

      context 'when path already has matching repository' do
        before do
          create(:container_repository, project: project, name: 'my/image')
        end

        it { is_expected.to have_repository }
        it { is_expected.to have_project }
      end

      context 'when path does not have matching repository' do
        it { is_expected.not_to have_repository }
        it { is_expected.to have_project }
      end
    end

    context 'when project does not exist' do
      let(:path) { 'some/project/my/image' }

      it { is_expected.not_to have_repository }
      it { is_expected.not_to have_project }
    end
  end

119 120 121 122
  describe '#repository_project' do
    let(:group) { create(:group, path: 'some_group') }

    context 'when project for given path exists' do
123
      let(:path) { 'some_group/some_project' }
124 125 126 127 128 129 130

      before do
        create(:empty_project, group: group, name: 'some_project')
        create(:empty_project, name: 'some_project')
      end

      it 'returns a correct project' do
131
        expect(subject.repository_project.group).to eq group
132 133 134 135
      end
    end

    context 'when project for given path does not exist' do
136
      let(:path) { 'not/matching' }
137 138

      it 'returns nil' do
139
        expect(subject.repository_project).to be_nil
140 141 142 143 144 145 146 147 148
      end
    end

    context 'when matching multi-level path' do
      let(:project) do
        create(:empty_project, group: group, name: 'some_project')
      end

      context 'when using the zero-level path' do
149
        let(:path) { project.full_path }
150 151

        it 'supports zero-level path' do
152
          expect(subject.repository_project).to eq project
153 154 155 156
        end
      end

      context 'when using first-level path' do
157
        let(:path) { "#{project.full_path}/repository" }
158 159

        it 'supports first-level path' do
160
          expect(subject.repository_project).to eq project
161 162 163 164
        end
      end

      context 'when using second-level path' do
165
        let(:path) { "#{project.full_path}/repository/name" }
166 167

        it 'supports second-level path' do
168
          expect(subject.repository_project).to eq project
169 170 171 172
        end
      end

      context 'when using too deep nesting in the path' do
173
        let(:path) { "#{project.full_path}/repository/name/invalid" }
174 175

        it 'does not support three-levels of nesting' do
176
          expect(subject.repository_project).to be_nil
177 178 179 180 181 182
        end
      end
    end
  end

  describe '#repository_name' do
183
    context 'when project does not exist' do
184
      let(:path) { 'some/name' }
185 186

      it 'returns nil' do
187
        expect(subject.repository_name).to be_nil
188 189 190 191
      end
    end

    context 'when project exists' do
192
      let(:group) { create(:group, path: 'Some_Group') }
193 194

      before do
195
        create(:empty_project, group: group, name: 'some_project')
196 197 198
      end

      context 'when project path equal repository path' do
199
        let(:path) { 'some_group/some_project' }
200 201

        it 'returns an empty string' do
202
          expect(subject.repository_name).to eq ''
203 204 205 206
        end
      end

      context 'when repository path has one additional level' do
207
        let(:path) { 'some_group/some_project/repository' }
208 209

        it 'returns a correct repository name' do
210
          expect(subject.repository_name).to eq 'repository'
211 212 213 214
        end
      end

      context 'when repository path has two additional levels' do
215
        let(:path) { 'some_group/some_project/repository/image' }
216 217

        it 'returns a correct repository name' do
218
          expect(subject.repository_name).to eq 'repository/image'
219 220 221
        end
      end
    end
222
  end
223 224 225 226 227 228 229 230 231 232 233

  describe '#project_path' do
    context 'when project does not exist' do
      let(:path) { 'some/name' }

      it 'returns nil' do
        expect(subject.project_path).to be_nil
      end
    end

    context 'when project with uppercase characters in path exists' do
234
      let(:path) { 'somegroup/myproject/my/image' }
235 236 237
      let(:group) { create(:group, path: 'SomeGroup') }

      before do
238
        create(:empty_project, group: group, name: 'MyProject')
239 240 241
      end

      it 'returns downcased project path' do
242
        expect(subject.project_path).to eq 'somegroup/myproject'
243 244 245
      end
    end
  end
246
end