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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Groups::UpdateService, feature_category: :groups_and_projects do
let!(:user) { create(:user) }
let!(:private_group) { create(:group, :private) }
let!(:internal_group) { create(:group, :internal) }
let!(:public_group) { create(:group, :public) }
describe "#execute" do
context 'with project' do
let!(:group) { create(:group, :public) }
let(:project) { create(:project, namespace: group) }
context 'located in a subgroup' do
let(:subgroup) { create(:group, parent: group) }
let!(:project) { create(:project, namespace: subgroup) }
before do
subgroup.add_owner(user)
end
it 'does allow a path update if there is not a root namespace change' do
expect(update_group(subgroup, user, path: 'updated')).to be true
expect(subgroup.errors[:path]).to be_empty
end
end
end
context "project visibility_level validation" do
context "public group with public projects" do
let!(:service) { described_class.new(public_group, user, visibility_level: Gitlab::VisibilityLevel::INTERNAL) }
before do
public_group.add_member(user, Gitlab::Access::OWNER)
create(:project, :public, group: public_group)
expect(TodosDestroyer::GroupPrivateWorker).not_to receive(:perform_in)
end
it "does not change permission level" do
service.execute
expect(public_group.errors.count).to eq(1)
expect(TodosDestroyer::GroupPrivateWorker).not_to receive(:perform_in)
end
it "returns false if save failed" do
allow(public_group).to receive(:save).and_return(false)
expect(service.execute).to be_falsey
end
context 'when a project has container images' do
let(:params) { { path: SecureRandom.hex } }
let!(:container_repository) { create(:container_repository, project: project) }
subject { described_class.new(public_group, user, params).execute }
context 'within group' do
let(:project) { create(:project, group: public_group) }
context 'with path updates' do
it 'does not allow the update' do
expect(subject).to be false
expect(public_group.errors[:base].first).to match(/Docker images in their Container Registry/)
end
end
context 'with name updates' do
let(:params) { { name: 'new-name' } }
it 'allows the update' do
expect(subject).to be true
expect(public_group.reload.name).to eq('new-name')
end
end
context 'when the path does not change' do
let(:params) { { name: 'new-name', path: public_group.path } }
it 'allows the update' do
expect(subject).to be true
expect(public_group.reload.name).to eq('new-name')
end
end
end
context 'within subgroup' do
let(:subgroup) { create(:group, parent: public_group) }
let(:project) { create(:project, group: subgroup) }
it 'does not allow path updates' do
expect(subject).to be false
expect(public_group.errors[:base].first).to match(/Docker images in their Container Registry/)
end
end
end
end
context "internal group with internal project" do
let!(:service) { described_class.new(internal_group, user, visibility_level: Gitlab::VisibilityLevel::PRIVATE) }
before do
internal_group.add_member(user, Gitlab::Access::OWNER)
create(:project, :internal, group: internal_group)
expect(TodosDestroyer::GroupPrivateWorker).not_to receive(:perform_in)
end
it "does not change permission level" do
service.execute
expect(internal_group.errors.count).to eq(1)
end
end
context "internal group with private project" do
let!(:service) { described_class.new(internal_group, user, visibility_level: Gitlab::VisibilityLevel::PRIVATE) }
before do
internal_group.add_member(user, Gitlab::Access::OWNER)
create(:project, :private, group: internal_group)
expect(TodosDestroyer::GroupPrivateWorker).to receive(:perform_in)
.with(Todo::WAIT_FOR_DELETE, internal_group.id)
end
it "changes permission level to private" do
service.execute
expect(internal_group.visibility_level)
.to eq(Gitlab::VisibilityLevel::PRIVATE)
end
end
end
context "with parent_id user doesn't have permissions for" do
let(:service) { described_class.new(public_group, user, parent_id: private_group.id) }
before do
service.execute
end
it 'does not update parent_id' do
updated_group = public_group.reload
expect(updated_group.parent_id).to be_nil
end
end
context 'crm_enabled param' do
context 'when no existing crm_settings' do
it 'when param not present, leave crm disabled' do
params = {}
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_falsey
end
it 'when param set true, enables crm' do
params = { crm_enabled: true }
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_truthy
end
end
context 'with existing crm_settings' do
it 'when param set true, enables crm' do
params = { crm_enabled: true }
create(:crm_settings, group: public_group)
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_truthy
end
it 'when param set false, disables crm' do
params = { crm_enabled: false }
create(:crm_settings, group: public_group, enabled: true)
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_falsy
end
it 'when param not present, crm remains disabled' do
params = {}
create(:crm_settings, group: public_group)
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_falsy
end
it 'when param not present, crm remains enabled' do
params = {}
create(:crm_settings, group: public_group, enabled: true)
described_class.new(public_group, user, params).execute
updated_group = public_group.reload
expect(updated_group.crm_enabled?).to be_truthy
end
end
end
end
context "unauthorized visibility_level validation" do
let!(:service) { described_class.new(internal_group, user, visibility_level: 99) }
before do
internal_group.add_member(user, Gitlab::Access::MAINTAINER)
end
it "does not change permission level" do
service.execute
expect(internal_group.errors.count).to eq(1)
end
end
context "path change validation" do
let_it_be(:group) { create(:group) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:project) { create(:project, namespace: subgroup) }
subject(:execute_update) { update_group(target_group, user, update_params) }
shared_examples 'not allowing a path update' do
let(:update_params) { { path: 'updated' } }
it 'does not allow a path update' do
target_group.add_maintainer(user)
expect(execute_update).to be false
expect(target_group.errors[:path]).to include('cannot change when group contains projects with NPM packages')
end
end
shared_examples 'allowing an update' do |on:|
let(:update_params) { { on => 'updated' } }
it "allows an update on #{on}" do
target_group.reload.add_maintainer(user)
expect(execute_update).to be true
expect(target_group.errors).to be_empty
expect(target_group[on]).to eq('updated')
end
end
context 'with namespaced npm packages' do
let_it_be(:package) { create(:npm_package, project: project, name: "@#{group.path}/test") }
context 'updating the root group' do
let_it_be_with_refind(:target_group) { group }
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
context 'updating the subgroup' do
let_it_be_with_refind(:target_group) { subgroup }
it_behaves_like 'allowing an update', on: :path
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
end
context 'with scoped npm packages' do
let_it_be(:package) { create(:npm_package, project: project, name: '@any_scope/test') }
context 'updating the root group' do
let_it_be_with_refind(:target_group) { group }
it_behaves_like 'allowing an update', on: :path
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
context 'updating the subgroup' do
let_it_be_with_refind(:target_group) { subgroup }
it_behaves_like 'allowing an update', on: :path
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
end
context 'with unscoped npm packages' do
let_it_be(:package) { create(:npm_package, project: project, name: 'test') }
context 'updating the root group' do
let_it_be_with_refind(:target_group) { group }
it_behaves_like 'allowing an update', on: :path
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
context 'updating the subgroup' do
let_it_be_with_refind(:target_group) { subgroup }
it_behaves_like 'allowing an update', on: :path
it_behaves_like 'allowing an update', on: :name
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
expect_next_instance_of(::Groups::UpdateService) do |service|
expect(service).to receive(:valid_path_change_with_npm_packages?).and_call_original
end
end
it_behaves_like 'not allowing a path update'
it_behaves_like 'allowing an update', on: :name
end
end
end
end
context 'when user is not group owner' do
context 'when group is private' do
before do
private_group.add_maintainer(user)
end
it 'does not update the group to public' do
result = described_class.new(private_group, user, visibility_level: Gitlab::VisibilityLevel::PUBLIC).execute
expect(result).to eq(false)
expect(private_group.errors.count).to eq(1)
expect(private_group).to be_private
end
it 'does not update the group to public with tricky value' do
result = described_class.new(private_group, user, visibility_level: Gitlab::VisibilityLevel::PUBLIC.to_s + 'r').execute
expect(result).to eq(false)
expect(private_group.errors.count).to eq(1)
expect(private_group).to be_private
end
end
context 'when group is public' do
before do
public_group.add_maintainer(user)
end
it 'does not update the group to private' do
result = described_class.new(public_group, user, visibility_level: Gitlab::VisibilityLevel::PRIVATE).execute
expect(result).to eq(false)
expect(public_group.errors.count).to eq(1)
expect(public_group).to be_public
end
it 'does not update the group to private with invalid string value' do
result = described_class.new(public_group, user, visibility_level: 'invalid').execute
expect(result).to eq(false)
expect(public_group.errors.count).to eq(1)
expect(public_group).to be_public
end
it 'does not update the group to private with valid string value' do
result = described_class.new(public_group, user, visibility_level: 'private').execute
expect(result).to eq(false)
expect(public_group.errors.count).to eq(1)
expect(public_group).to be_public
end
# See https://gitlab.com/gitlab-org/gitlab/-/issues/359910
it 'does not update the group to private because of Active Record typecasting' do
result = described_class.new(public_group, user, visibility_level: 'public').execute
expect(result).to eq(true)
expect(public_group.errors.count).to eq(0)
expect(public_group).to be_public
end
end
end
context 'when updating #emails_disabled' do
let(:service) { described_class.new(internal_group, user, emails_disabled: true) }
it 'updates the attribute' do
internal_group.add_member(user, Gitlab::Access::OWNER)
expect { service.execute }.to change { internal_group.emails_disabled }.to(true)
end
it 'does not update when not group owner' do
expect { service.execute }.not_to change { internal_group.emails_disabled }
end
end
context 'updating default_branch_protection' do
let(:service) do
described_class.new(internal_group, user, default_branch_protection: Gitlab::Access::PROTECTION_NONE)
end
let(:settings) { internal_group.namespace_settings }
let(:expected_settings) { Gitlab::Access::BranchProtection.protection_none.stringify_keys }
context 'for users who have the ability to update default_branch_protection' do
it 'updates default_branch_protection attribute' do
internal_group.add_owner(user)
expect { service.execute }.to change { internal_group.default_branch_protection }.from(Gitlab::Access::PROTECTION_FULL).to(Gitlab::Access::PROTECTION_NONE)
end
it 'updates default_branch_protection_defaults to match default_branch_protection' do
internal_group.add_owner(user)
expect { service.execute }.to change { settings.default_branch_protection_defaults }.from({}).to(expected_settings)
end
end
context 'for users who do not have the ability to update default_branch_protection' do
it 'does not update the attribute' do
expect { service.execute }.not_to change { internal_group.default_branch_protection }
expect { service.execute }.not_to change { internal_group.namespace_settings.default_branch_protection_defaults }
end
end
end
context 'updating default_branch_protection_defaults' do
let(:branch_protection) { ::Gitlab::Access::BranchProtection.protected_against_developer_pushes.stringify_keys }
let(:service) do
described_class.new(internal_group, user, default_branch_protection_defaults: branch_protection)
end
let(:settings) { internal_group.namespace_settings }
let(:expected_settings) { branch_protection }
context 'for users who have the ability to update default_branch_protection_defaults' do
it 'updates default_branch_protection attribute' do
internal_group.add_owner(user)
expect { service.execute }.to change { internal_group.default_branch_protection_defaults }.from({}).to(expected_settings)
end
end
context 'for users who do not have the ability to update default_branch_protection_defaults' do
it 'does not update the attribute' do
expect { service.execute }.not_to change { internal_group.default_branch_protection_defaults }
expect { service.execute }.not_to change { internal_group.namespace_settings.default_branch_protection_defaults }
end
end
end
context 'EventStore' do
let(:service) { described_class.new(group, user, **params) }
let(:root_group) { create(:group, path: 'root') }
let(:group) do
create(:group, parent: root_group, path: 'old-path').tap { |g| g.add_owner(user) }
end
context 'when changing a group path' do
let(:new_path) { SecureRandom.hex }
let(:params) { { path: new_path } }
it 'publishes a GroupPathChangedEvent' do
old_path = group.full_path
expect { service.execute }
.to publish_event(Groups::GroupPathChangedEvent)
.with(
group_id: group.id,
root_namespace_id: group.root_ancestor.id,
old_path: old_path,
new_path: "root/#{new_path}"
)
end
end
context 'when not changing a group path' do
let(:params) { { name: 'very-new-name' } }
it 'does not publish a GroupPathChangedEvent' do
expect { service.execute }
.not_to publish_event(Groups::GroupPathChangedEvent)
end
end
end
context 'rename group' do
let(:new_path) { SecureRandom.hex }
let!(:service) { described_class.new(internal_group, user, path: new_path) }
before do
internal_group.add_member(user, Gitlab::Access::MAINTAINER)
create(:project, :internal, group: internal_group)
end
it 'returns true' do
expect(service.execute).to eq(true)
end
context 'error moving group' do
before do
allow(internal_group).to receive(:move_dir).and_raise(Gitlab::UpdatePathError)
end
it 'does not raise an error' do
expect { service.execute }.not_to raise_error
end
it 'returns false' do
expect(service.execute).to eq(false)
end
it 'has the right error' do
service.execute
expect(internal_group.errors.full_messages.first).to eq('Gitlab::UpdatePathError')
end
it "hasn't changed the path" do
expect { service.execute }.not_to change { internal_group.reload.path }
end
end
end
context 'for a subgroup' do
let(:subgroup) { create(:group, :private, parent: private_group) }
context 'when the parent group share_with_group_lock is enabled' do
before do
private_group.update_column(:share_with_group_lock, true)
end
context 'for the parent group owner' do
it 'allows disabling share_with_group_lock' do
private_group.add_owner(user)
result = described_class.new(subgroup, user, share_with_group_lock: false).execute
expect(result).to be_truthy
expect(subgroup.reload.share_with_group_lock).to be_falsey
end
end
context 'for a subgroup owner (who does not own the parent)' do
it 'does not allow disabling share_with_group_lock' do
subgroup_owner = create(:user)
subgroup.add_owner(subgroup_owner)
result = described_class.new(subgroup, subgroup_owner, share_with_group_lock: false).execute
expect(result).to be_falsey
expect(subgroup.errors.full_messages.first).to match(/cannot be disabled when the parent group "Share with group lock" is enabled, except by the owner of the parent group/)
expect(subgroup.reload.share_with_group_lock).to be_truthy
end
end
end
end
context 'change shared Runners config' do
let(:group) { create(:group) }
let(:project) { create(:project, shared_runners_enabled: true, group: group) }
subject { described_class.new(group, user, shared_runners_setting: Namespace::SR_DISABLED_AND_UNOVERRIDABLE).execute }
before do
group.add_owner(user)
end
it 'calls the shared runners update service' do
expect_any_instance_of(::Groups::UpdateSharedRunnersService).to receive(:execute).and_return({ status: :success })
expect(subject).to be_truthy
end
it 'handles errors in the shared runners update service' do
expect_any_instance_of(::Groups::UpdateSharedRunnersService).to receive(:execute).and_return({ status: :error, message: 'something happened' })
expect(subject).to be_falsy
expect(group.errors[:update_shared_runners].first).to eq('something happened')
end
end
context 'changes allowing subgroups to establish own 2FA' do
let(:group) { create(:group) }
let(:params) { { allow_mfa_for_subgroups: false } }
subject { described_class.new(group, user, params).execute }
it 'changes settings' do
subject
expect(group.namespace_settings.reload.allow_mfa_for_subgroups).to eq(false)
end
it 'enqueues update subgroups and its members' do
expect(DisallowTwoFactorForSubgroupsWorker).to receive(:perform_async).with(group.id)
subject
end
end
def update_group(group, user, opts)
Groups::UpdateService.new(group, user, opts).execute
end
end
|