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
|
# 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, content_filename: 'template.yml') }
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_it_be(:existing_project) { create(:project, :repository) }
let(:project_path) { existing_project.full_path }
let(:address) { "acme.com/#{project_path}/component@#{version}" }
let(:version) { 'master' }
context 'when project exists' do
it 'provides the expected attributes', :aggregate_failures do
expect(path.project).to eq(existing_project)
expect(path.host).to eq(current_host)
expect(path.sha).to eq(existing_project.commit('master').id)
expect(path.project_file_path).to eq('component/template.yml')
end
context 'when content exists' do
let(:content) { 'image: alpine' }
before do
allow_next_instance_of(Repository) do |instance|
allow(instance)
.to receive(:blob_data_at)
.with(existing_project.commit('master').id, 'component/template.yml')
.and_return(content)
end
end
context 'when user has permissions to read code' do
before do
existing_project.add_developer(user)
end
it 'fetches the content' do
expect(path.fetch_content!(current_user: user)).to eq(content)
end
shared_examples 'prevents infinite loop' do |prefix|
context "when the project path starts with '#{prefix}'" do
let(:project_path) { "#{prefix}#{existing_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', '//'
end
context 'when user does not have permissions to download code' do
it 'raises an error when fetching the content' do
expect { path.fetch_content!(current_user: user) }
.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
end
end
context 'when project path is nested under a subgroup' do
let(:existing_group) { create(:group, :nested) }
let(:existing_project) { create(:project, :repository, group: existing_group) }
it 'provides the expected attributes', :aggregate_failures do
expect(path.project).to eq(existing_project)
expect(path.host).to eq(current_host)
expect(path.sha).to eq(existing_project.commit('master').id)
expect(path.project_file_path).to eq('component/template.yml')
end
end
context 'when current GitLab instance is installed on a relative URL' do
let(:address) { "acme.com/gitlab/#{project_path}/component@#{version}" }
let(:current_host) { 'acme.com/gitlab/' }
it 'provides the expected attributes', :aggregate_failures do
expect(path.project).to eq(existing_project)
expect(path.host).to eq(current_host)
expect(path.sha).to eq(existing_project.commit('master').id)
expect(path.project_file_path).to eq('component/template.yml')
end
end
context 'when version does not exist' do
let(:version) { 'non-existent' }
it 'provides the expected attributes', :aggregate_failures do
expect(path.project).to eq(existing_project)
expect(path.host).to eq(current_host)
expect(path.sha).to be_nil
expect(path.project_file_path).to eq('component/template.yml')
end
it 'returns nil when fetching the content' do
expect(path.fetch_content!(current_user: user)).to be_nil
end
end
context 'when version is `~latest`' do
let(:version) { '~latest' }
context 'when project has releases' do
let_it_be(:latest_release) do
create(:release, project: existing_project, sha: 'sha-1', released_at: Time.zone.now)
end
before_all do
# Previous release
create(:release, project: existing_project, sha: 'sha-2', released_at: Time.zone.now - 1.day)
end
it 'returns the sha of the latest release' do
expect(path.sha).to eq(latest_release.sha)
end
end
context 'when project does not have releases' do
it { expect(path.sha).to be_nil }
end
end
context 'when project does not exist' do
let(:project_path) { 'non-existent/project' }
it 'provides the expected attributes', :aggregate_failures do
expect(path.project).to be_nil
expect(path.host).to eq(current_host)
expect(path.sha).to be_nil
expect(path.project_file_path).to be_nil
end
it 'returns nil when fetching the content' do
expect(path.fetch_content!(current_user: user)).to be_nil
end
end
end
end
|