File: fetch_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (206 lines) | stat: -rw-r--r-- 6,412 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::Components::FetchService, feature_category: :pipeline_composition do
  let_it_be(:user) { create(:user) }
  let_it_be(:current_user) { user }
  let_it_be(:current_host) { Gitlab.config.gitlab.host }
  let_it_be(:content) do
    <<~COMPONENT
    job:
      script: echo
    COMPONENT
  end

  let(:service) do
    described_class.new(address: address, current_user: current_user)
  end

  let_it_be(:project) do
    project = create(
      :project, :custom_repo,
      files: {
        'templates/first-component.yml' => content,
        'templates/second-component/template.yml' => content
      }
    )

    project.repository.add_tag(project.creator, 'v0.1', project.repository.commit.sha)

    create(:release, project: project, tag: 'v0.1', sha: project.repository.commit.sha)
    create(:ci_catalog_resource, project: project)

    project
  end

  before do
    project.add_developer(user)
  end

  describe '#execute', :aggregate_failures do
    subject(:result) { service.execute }

    shared_examples 'an external component' do
      shared_examples 'component address' do
        context 'when content exists' do
          it 'returns the content' do
            expect(result).to be_success
            expect(result.payload[:content]).to eq(content)
          end
        end

        context 'when content does not exist' do
          let(:address) { "#{current_host}/#{component_path}@~version-does-not-exist" }

          it 'returns an error' do
            expect(result).to be_error
            expect(result.reason).to eq(:content_not_found)
          end
        end
      end

      context 'when user does not have permissions to read the code' do
        let(:version) { 'master' }
        let(:current_user) { create(:user) }

        it 'returns a generic error response' do
          expect(result).to be_error
          expect(result.reason).to eq(:not_allowed)
          expect(result.message)
            .to eq(
              "Component '#{address}' - " \
              "project does not exist or you don't have sufficient permissions"
            )
        end

        context 'when the user is external and the project is internal' do
          let(:current_user) { create(:user, :external) }
          let(:project) do
            project = create(
              :project, :custom_repo, :internal,
              files: {
                'templates/component/template.yml' => content
              }
            )
            project.repository.add_tag(project.creator, 'v0.1', project.repository.commit.sha)

            create(:release, project: project, tag: 'v0.1', sha: project.repository.commit.sha)
            create(:ci_catalog_resource, project: project)

            project
          end

          it 'returns an error response for external user accessing internal project' do
            expect(result).to be_error
            expect(result.reason).to eq(:not_allowed)
            expect(result.message)
              .to eq(
                "Component '#{address}' - " \
                "project is `Internal`, it cannot be accessed by an External User"
              )
          end
        end
      end

      context 'when version is a branch name' do
        it_behaves_like 'component address' do
          let(:version) { project.default_branch }
        end
      end

      context 'when version is a tag name' do
        it_behaves_like 'component address' do
          let(:version) { project.repository.tags.first.name }
        end
      end

      context 'when version is a commit sha' do
        it_behaves_like 'component address' do
          let(:version) { project.repository.tags.first.id }
        end
      end

      context 'when version is not provided' do
        let(:version) { nil }

        it 'returns an error' do
          expect(result).to be_error
          expect(result.reason).to eq(:content_not_found)
        end
      end

      context 'when version is ~latest' do
        let(:version) { '~latest' }
        let_it_be(:project) { create(:project) }

        context 'and the project is not a catalog resource' do
          it 'returns an error' do
            expect(result).to be_error
            expect(result.message).to eq(
              "Component '#{current_host}/#{component_path}@~latest' - " \
              'The ~latest version reference is not supported for non-catalog resources. ' \
              'Use a tag, branch, or commit SHA instead.'
            )
            expect(result.reason).to eq(:invalid_usage)
          end
        end

        context 'and the project does not exist' do
          let(:component_path) { 'unknown' }

          it 'returns an error' do
            expect(result).to be_error
            expect(result.reason).to eq(:content_not_found)
          end
        end
      end

      context 'when project does not exist' do
        let(:component_path) { 'unknown/component' }
        let(:version) { '1.0' }

        it 'returns an error' do
          expect(result).to be_error
          expect(result.reason).to eq(:content_not_found)
        end
      end

      context 'when host is different than the current instance host' do
        let(:current_host) { 'another-host.com' }
        let(:version) { '1.0' }

        it 'returns an error' do
          expect(result).to be_error
          expect(result.reason).to eq(:unsupported_path)
        end
      end
    end

    context 'when address points to an external component' do
      let(:address) { "#{current_host}/#{component_path}@#{version}" }

      context 'when component path points to a template file in a project' do
        let(:component_path) { "#{project.full_path}/first-component" }

        it_behaves_like 'an external component'
      end

      context 'when component path points to a template directory in a project' do
        let(:component_path) { "#{project.full_path}/second-component" }

        it_behaves_like 'an external component'
      end

      context 'when the project exists but the component does not' do
        let(:component_path) { "#{project.full_path}/unknown-component" }
        let(:version) { '~latest' }

        it 'returns a content not found error' do
          expect(result).to be_error
          expect(result.reason).to eq(:content_not_found)
        end
      end
    end
  end
end