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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::BuildPolicy, feature_category: :continuous_integration do
let(:user) { create(:user) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
let(:policy) do
described_class.new(user, build)
end
it_behaves_like 'a deployable job policy', :ci_build
shared_context 'public pipelines disabled' do
before do
project.update_attribute(:public_builds, false)
end
end
describe '#rules' do
context 'when user does not have access to the project' do
let(:project) { create(:project, :private) }
context 'when public builds are enabled' do
it 'does not include ability to read build' do
expect(policy).not_to be_allowed :read_build
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
expect(policy).not_to be_allowed :read_build
end
end
end
context 'when anonymous user has access to the project' do
let(:project) { create(:project, :public) }
context 'when public builds are enabled' do
it 'includes ability to read build' do
expect(policy).to be_allowed :read_build
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
expect(policy).not_to be_allowed :read_build
end
end
end
context 'when team member has access to the project' do
let(:project) { create(:project, :public) }
context 'team member is a guest' do
before do
project.add_guest(user)
end
context 'when public builds are enabled' do
it 'includes ability to read build' do
expect(policy).to be_allowed :read_build
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
expect(policy).not_to be_allowed :read_build
end
end
end
context 'team member is a reporter' do
before do
project.add_reporter(user)
end
context 'when public builds are enabled' do
it 'includes ability to read build' do
expect(policy).to be_allowed :read_build
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
expect(policy).to be_allowed :read_build
end
end
end
context 'when maintainer is allowed to push to pipeline branch' do
let(:project) { create(:project, :public) }
before do
project.add_maintainer(user)
allow(project).to receive(:empty_repo?).and_return(false)
allow(project).to receive(:branch_allows_collaboration?).and_return(true)
end
it 'enables update_build if user is maintainer' do
expect(policy).to be_allowed :update_build
expect(policy).to be_allowed :update_commit_status
end
end
end
describe 'rules for protected ref' do
let(:project) { create(:project, :repository) }
let(:build) { create(:ci_build, ref: 'some-ref', pipeline: pipeline) }
before do
project.add_developer(user)
end
context 'when no one can push or merge to the branch' do
before do
create(:protected_branch, :no_one_can_push, name: build.ref, project: project)
end
it 'does not include ability to update build' do
expect(policy).to be_disallowed :update_build
end
context 'when the user is admin', :enable_admin_mode do
before do
user.update!(admin: true)
end
it 'does not include ability to update build' do
expect(policy).to be_disallowed :update_build
end
end
end
context 'when developers can push to the branch' do
before do
create(:protected_branch, :developers_can_merge, name: build.ref, project: project)
end
it 'includes ability to update build' do
expect(policy).to be_allowed :update_build
end
end
context 'when no one can create the tag' do
before do
create(:protected_tag, :no_one_can_create, name: build.ref, project: project)
build.update!(tag: true)
end
it 'does not include ability to update build' do
expect(policy).to be_disallowed :update_build
end
end
context 'when no one can create the tag but it is not a tag' do
before do
create(:protected_tag, :no_one_can_create, name: build.ref, project: project)
end
it 'includes ability to update build' do
expect(policy).to be_allowed :update_build
end
end
end
describe 'rules for erase build' do
let(:project) { create(:project, :repository) }
let(:build) { create(:ci_build, pipeline: pipeline, ref: 'some-ref', user: owner) }
context 'when a developer erases a build' do
before do
project.add_developer(user)
end
context 'when developers can push to the branch' do
context 'when the build was created by the developer' do
let(:owner) { user }
context 'when the build was created for a protected ref' do
before do
create(:protected_branch, :developers_can_push, name: build.ref, project: project)
end
it { expect(policy).to be_disallowed :erase_build }
end
context 'when the build was created for an unprotected ref' do
it { expect(policy).to be_allowed :erase_build }
end
end
context 'when the build was created by the other' do
let(:owner) { create(:user) }
it { expect(policy).to be_disallowed :erase_build }
end
end
context 'when no one can push or merge to the branch' do
let(:owner) { user }
before do
create(:protected_branch, :no_one_can_push, :no_one_can_merge, name: build.ref, project: project)
end
it { expect(policy).to be_disallowed :erase_build }
end
end
context 'when a maintainer erases a build' do
before do
project.add_maintainer(user)
end
context 'when maintainers can push to the branch' do
before do
create(:protected_branch, :maintainers_can_push, name: build.ref, project: project)
end
context 'when the build was created by the maintainer' do
let(:owner) { user }
it { expect(policy).to be_allowed :erase_build }
end
context 'when the build was created by the other' do
let(:owner) { create(:user) }
it { expect(policy).to be_allowed :erase_build }
end
end
context 'when no one can push or merge to the branch' do
let(:owner) { user }
before do
create(:protected_branch, :no_one_can_push, :no_one_can_merge, name: build.ref, project: project)
end
it { expect(policy).to be_disallowed :erase_build }
end
end
context 'when an admin erases a build', :enable_admin_mode do
let(:owner) { create(:user) }
before do
user.update!(admin: true)
end
context 'when the build was created for a protected branch' do
before do
create(:protected_branch, :developers_can_push, name: build.ref, project: project)
end
it { expect(policy).to be_disallowed :erase_build }
end
context 'when the build was created for a protected tag' do
before do
create(:protected_tag, :developers_can_create, name: build.ref, project: project)
build.update!(tag: true)
end
it { expect(policy).to be_disallowed :erase_build }
end
context 'when the build was created for an unprotected ref' do
it { expect(policy).to be_allowed :erase_build }
end
end
end
end
describe 'manage a web ide terminal' do
let(:build_permissions) { %i[read_web_ide_terminal create_build_terminal update_web_ide_terminal create_build_service_proxy] }
let_it_be(:maintainer) { create(:user) }
let(:owner) { create(:owner) }
let(:admin) { create(:admin) }
let(:maintainer) { create(:user) }
let(:developer) { create(:user) }
let(:reporter) { create(:user) }
let(:guest) { create(:user) }
let(:project) { create(:project, :public, namespace: owner.namespace) }
let(:pipeline) { create(:ci_empty_pipeline, project: project, source: :webide) }
let(:build) { create(:ci_build, pipeline: pipeline) }
before do
allow(build).to receive(:has_terminal?).and_return(true)
project.add_maintainer(maintainer)
project.add_developer(developer)
project.add_reporter(reporter)
project.add_guest(guest)
end
subject { described_class.new(current_user, build) }
context 'when create_web_ide_terminal access enabled' do
context 'with admin' do
let(:current_user) { admin }
context 'when admin mode enabled', :enable_admin_mode do
it { expect_allowed(*build_permissions) }
end
context 'when admin mode disabled' do
it { expect_disallowed(*build_permissions) }
end
context 'when build is not from a webide pipeline' do
let(:pipeline) { create(:ci_empty_pipeline, project: project, source: :chat) }
it { expect_disallowed(:read_web_ide_terminal, :update_web_ide_terminal, :create_build_service_proxy) }
end
context 'when build has no runner terminal' do
before do
allow(build).to receive(:has_terminal?).and_return(false)
end
context 'when admin mode enabled', :enable_admin_mode do
it { expect_allowed(:read_web_ide_terminal, :update_web_ide_terminal) }
it { expect_disallowed(:create_build_terminal, :create_build_service_proxy) }
end
context 'when admin mode disabled' do
it { expect_disallowed(:read_web_ide_terminal, :update_web_ide_terminal) }
it { expect_disallowed(:create_build_terminal, :create_build_service_proxy) }
end
end
context 'feature flag "build_service_proxy" is disabled' do
before do
stub_feature_flags(build_service_proxy: false)
end
it { expect_disallowed(:create_build_service_proxy) }
end
end
shared_examples 'allowed build owner access' do
it { expect_disallowed(*build_permissions) }
context 'when user is the owner of the job' do
let(:build) { create(:ci_build, pipeline: pipeline, user: current_user) }
it { expect_allowed(*build_permissions) }
end
end
shared_examples 'forbidden access' do
it { expect_disallowed(*build_permissions) }
context 'when user is the owner of the job' do
let(:build) { create(:ci_build, pipeline: pipeline, user: current_user) }
it { expect_disallowed(*build_permissions) }
end
end
context 'with owner' do
let(:current_user) { owner }
it_behaves_like 'allowed build owner access'
end
context 'with maintainer' do
let(:current_user) { maintainer }
it_behaves_like 'allowed build owner access'
end
context 'with developer' do
let(:current_user) { developer }
it_behaves_like 'forbidden access'
end
context 'with reporter' do
let(:current_user) { reporter }
it_behaves_like 'forbidden access'
end
context 'with guest' do
let(:current_user) { guest }
it_behaves_like 'forbidden access'
end
context 'with non member' do
let(:current_user) { create(:user) }
it_behaves_like 'forbidden access'
end
end
end
describe 'ability :create_build_terminal' do
let(:project) { create(:project, :private) }
subject { described_class.new(user, build) }
context 'when user can update_build' do
before do
project.add_maintainer(user)
end
context 'when job has terminal' do
before do
allow(build).to receive(:has_terminal?).and_return(true)
end
context 'when current user is the job owner' do
before do
build.update!(user: user)
end
it { expect_allowed(:create_build_terminal) }
end
context 'when current user is not the job owner' do
it { expect_disallowed(:create_build_terminal) }
end
end
context 'when job does not have terminal' do
before do
allow(build).to receive(:has_terminal?).and_return(false)
build.update!(user: user)
end
it { expect_disallowed(:create_build_terminal) }
end
end
context 'when user cannot update build' do
before do
project.add_guest(user)
allow(build).to receive(:has_terminal?).and_return(true)
end
it { expect_disallowed(:create_build_terminal) }
end
end
end
|