summaryrefslogtreecommitdiff
path: root/spec/lib/container_registry/path_spec.rb
blob: 278b1fc1b5525ad7cb9af8dfc9bae24ea094ec69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'spec_helper'

describe ContainerRegistry::Path do
  let(:path) { described_class.new(name) }

  describe '#components' do
    context 'when repository path is valid' do
      let(:name) { 'path/to/some/project' }

      it 'return all project-like components in reverse order' do
        expect(path.components).to eq %w[path/to/some/project
                                         path/to/some
                                         path/to]
      end
    end

    context 'when repository path is invalid' do
      let(:name) { '' }

      it 'rasises en error' do
        expect { path.components }
          .to raise_error described_class::InvalidRegistryPathError
      end
    end
  end

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

      it 'is not valid' do
        expect(path).not_to be_valid
      end
    end

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

      it 'is not valid' do
        expect(path).not_to be_valid
      end
    end

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

      it 'is valid' do
        expect(path).to be_valid
      end
    end
  end

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

    context 'when project for given path exists' do
      let(:name) { 'some_group/some_project' }

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

      it 'returns a correct project' do
        expect(path.repository_project.group).to eq group
      end
    end

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

      it 'returns nil' do
        expect(path.repository_project).to be_nil
      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
        let(:name) { project.full_path }

        it 'supports zero-level path' do
          expect(path.repository_project).to eq project
        end
      end

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

        it 'supports first-level path' do
          expect(path.repository_project).to eq project
        end
      end

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

        it 'supports second-level path' do
          expect(path.repository_project).to eq project
        end
      end

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

        it 'does not support three-levels of nesting' do
          expect(path.repository_project).to be_nil
        end
      end
    end
  end

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

      it 'returns nil' do
        expect(path.repository_name).to be_nil
      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
        let(:name) { 'some_group/some_project' }

        it 'returns an empty string' do
          expect(path.repository_name).to eq ''
        end
      end

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

        it 'returns a correct repository name' do
          expect(path.repository_name).to eq 'repository'
        end
      end

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

        it 'returns a correct repository name' do
          expect(path.repository_name).to eq 'repository/image'
        end
      end
    end
  end
end