path_spec.rb 5.97 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 192 193 194 195 196 197 198 199 200 201 202 203
      end
    end

    context 'when project exists' do
      let(:group) { create(:group, path: 'some_group') }

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

      before do
        allow(path).to receive(:repository_project)
          .and_return(project)
      end

      context 'when project path equal repository path' do
204
        let(:path) { 'some_group/some_project' }
205 206

        it 'returns an empty string' do
207
          expect(subject.repository_name).to eq ''
208 209 210 211
        end
      end

      context 'when repository path has one additional level' do
212
        let(:path) { 'some_group/some_project/repository' }
213 214

        it 'returns a correct repository name' do
215
          expect(subject.repository_name).to eq 'repository'
216 217 218 219
        end
      end

      context 'when repository path has two additional levels' do
220
        let(:path) { 'some_group/some_project/repository/image' }
221 222

        it 'returns a correct repository name' do
223
          expect(subject.repository_name).to eq 'repository/image'
224 225 226
        end
      end
    end
227 228
  end
end