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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Components::InstancePath, feature_category: :pipeline_composition do
let_it_be(:user) { create(:user) }
let(:path) { described_class.new(address: address) }
let(:settings) { GitlabSettings::Options.build({ 'component_fqdn' => current_host }) }
let(:current_host) { 'acme.com/' }
before do
allow(::Settings).to receive(:gitlab_ci).and_return(settings)
end
describe 'FQDN path' do
let(:version) { 'master' }
let(:project_path) { project.full_path }
let(:address) { "acme.com/#{project_path}/secret-detection@#{version}" }
context 'when the project repository contains a templates directory' do
let_it_be(:project) do
create(
:project, :custom_repo,
files: {
'templates/secret-detection.yml' => 'image: alpine_1',
'templates/dast/template.yml' => 'image: alpine_2',
'templates/dast/another-template.yml' => 'image: alpine_3',
'templates/dast/another-folder/template.yml' => 'image: alpine_4'
}
)
end
before do
project.add_developer(user)
end
context 'when user does not have permissions' do
it 'raises an error when fetching the content' do
expect { path.fetch_content!(current_user: build(:user)) }
.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
shared_examples 'does not find the component' do
it 'returns nil' do
result = path.fetch_content!(current_user: user)
expect(result).to be_nil
end
end
shared_examples 'finds the component' do
shared_examples 'fetches the component content' do
it 'fetches the component content', :aggregate_failures do
result = path.fetch_content!(current_user: user)
expect(result.content).to eq(file_content)
expect(result.path).to eq(file_path)
expect(path.host).to eq(current_host)
expect(path.project).to eq(project)
expect(path.sha).to eq(project.commit('master').id)
end
end
it_behaves_like 'fetches the component content'
context 'when feature flag ci_redirect_component_project is disabled' do
before do
stub_feature_flags(ci_redirect_component_project: false)
end
it_behaves_like 'fetches the component content'
end
context 'when the there is a redirect set for the project' do
let!(:redirect_route) { project.redirect_routes.create!(path: 'another-group/new-project') }
let(:project_path) { redirect_route.path }
it_behaves_like 'fetches the component content'
context 'when feature flag ci_redirect_component_project is disabled' do
before do
stub_feature_flags(ci_redirect_component_project: false)
end
it_behaves_like 'does not find the component'
end
end
end
context 'when the component is simple (single file template)' do
it_behaves_like 'finds the component' do
let(:file_path) { 'templates/secret-detection.yml' }
let(:file_content) { 'image: alpine_1' }
end
end
context 'when the component is complex (directory-based template)' do
let(:address) { "acme.com/#{project_path}/dast@#{version}" }
it_behaves_like 'finds the component' do
let(:file_path) { 'templates/dast/template.yml' }
let(:file_content) { 'image: alpine_2' }
end
context 'when there is an invalid nested component folder' do
let(:address) { "acme.com/#{project_path}/dast/another-folder@#{version}" }
it_behaves_like 'does not find the component'
end
context 'when there is an invalid nested component path' do
let(:address) { "acme.com/#{project_path}/dast/another-template@#{version}" }
it_behaves_like 'does not find the component'
end
end
context "when the project path starts with '/'" do
let(:project_path) { "/#{project.full_path}" }
it_behaves_like 'does not find the component'
end
# TODO: remove when deleting the feature flag `ci_redirect_component_project`
shared_examples 'prevents infinite loop' do |prefix|
context "when the project path starts with '#{prefix}'" do
let(:project_path) { "#{prefix}#{project.full_path}" }
it 'returns nil' do
result = path.fetch_content!(current_user: user)
expect(result).to be_nil
end
end
end
it_behaves_like 'prevents infinite loop', '/'
it_behaves_like 'prevents infinite loop', '//'
context 'when fetching the latest version of a component' do
let_it_be(:project) do
create(
:project, :custom_repo,
files: {
'templates/secret-detection.yml' => 'image: alpine_1'
}
)
end
let(:version) { '~latest' }
let(:latest_sha) do
project.repository.commit('master').id
end
before do
create(:release, project: project, sha: project.repository.root_ref_sha,
released_at: Time.zone.now - 1.day)
project.repository.update_file(
user, 'templates/secret-detection.yml', 'image: alpine_2',
message: 'Updates image', branch_name: project.default_branch
)
create(:release, project: project, sha: latest_sha,
released_at: Time.zone.now)
end
it 'returns the component content of the latest project release', :aggregate_failures do
result = path.fetch_content!(current_user: user)
expect(result.content).to eq('image: alpine_2')
expect(result.path).to eq('templates/secret-detection.yml')
expect(path.host).to eq(current_host)
expect(path.project).to eq(project)
expect(path.sha).to eq(latest_sha)
end
context 'when the project is a catalog resource' do
let_it_be(:resource) { create(:ci_catalog_resource, project: project) }
before do
project.releases.each do |release|
create(:ci_catalog_resource_version, catalog_resource: resource, release: release)
end
end
it 'returns the component content of the latest catalog resource version', :aggregate_failures do
result = path.fetch_content!(current_user: user)
expect(result.content).to eq('image: alpine_2')
expect(result.path).to eq('templates/secret-detection.yml')
expect(path.host).to eq(current_host)
expect(path.project).to eq(project)
expect(path.sha).to eq(latest_sha)
end
end
end
context 'when version does not exist' do
let(:version) { 'non-existent' }
it 'returns nil', :aggregate_failures do
expect(path.fetch_content!(current_user: user)).to be_nil
expect(path.host).to eq(current_host)
expect(path.project).to eq(project)
expect(path.sha).to be_nil
end
end
context 'when current GitLab instance is installed on a relative URL' do
let(:address) { "acme.com/gitlab/#{project_path}/secret-detection@#{version}" }
let(:current_host) { 'acme.com/gitlab/' }
it 'fetches the component content', :aggregate_failures do
result = path.fetch_content!(current_user: user)
expect(result.content).to eq('image: alpine_1')
expect(result.path).to eq('templates/secret-detection.yml')
expect(path.host).to eq(current_host)
expect(path.project).to eq(project)
expect(path.sha).to eq(project.commit('master').id)
end
end
end
end
end
|