File: changes_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 (463 lines) | stat: -rw-r--r-- 19,303 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
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 ProjectAuthorizations::Changes, feature_category: :groups_and_projects do
  describe '.apply!' do
    subject(:apply_project_authorization_changes) { project_authorization_changes.apply! }

    shared_examples_for 'does not log any detail' do
      it 'does not log any detail' do
        expect(Gitlab::AppLogger).not_to receive(:info)

        apply_project_authorization_changes
      end
    end

    shared_examples_for 'logs the detail' do |batch_size:|
      it 'logs the detail' do
        expect(Gitlab::AppLogger).to receive(:info).with(
          entire_size: 3,
          message: 'Project authorizations refresh performed with delay',
          total_delay: (3 / batch_size.to_f).ceil * ProjectAuthorizations::Changes::SLEEP_DELAY,
          **Gitlab::ApplicationContext.current
        )

        apply_project_authorization_changes
      end
    end

    shared_examples_for 'publishes AuthorizationsRemovedEvent' do
      it 'publishes a AuthorizationsRemovedEvent event with project id' do
        allow(::Gitlab::EventStore).to receive(:publish_group)
        project_events = project_ids.map do |project_id|
          project_data = { project_id: project_id, user_ids: user_ids }
          project_event = instance_double('::ProjectAuthorizations::AuthorizationsRemovedEvent', data: project_data)

          allow(::ProjectAuthorizations::AuthorizationsRemovedEvent).to receive(:new)
                                                                          .with(data: project_data)
                                                                          .and_return(project_event)
          project_event
        end
        expect(::Gitlab::EventStore).to receive(:publish_group).with(project_events)

        apply_project_authorization_changes
      end
    end

    shared_examples_for 'publishes AuthorizationsAddedEvent' do
      it 'publishes a AuthorizationsAddedEvent event with project ids' do
        allow(::Gitlab::EventStore).to receive(:publish_group)
        project_data = { project_ids: project_ids, user_ids: user_ids }
        project_event = instance_double('::ProjectAuthorizations::AuthorizationsAddedEvent', data: project_data)

        allow(::ProjectAuthorizations::AuthorizationsAddedEvent).to receive(:new)
                                                                        .with(data: project_data)
                                                                        .and_return(project_event)
        expect(::Gitlab::EventStore).to receive(:publish_group).with([project_event])

        apply_project_authorization_changes
      end
    end

    shared_examples_for 'does not publish AuthorizationsRemovedEvent' do
      it 'does not publish a AuthorizationsRemovedEvent event' do
        expect(::Gitlab::EventStore).not_to(
          receive(:publish_group).with(
            array_including(an_instance_of(::ProjectAuthorizations::AuthorizationsRemovedEvent))
          )
        )

        apply_project_authorization_changes
      end
    end

    shared_examples_for 'does not publish AuthorizationsAddedEvent' do
      it 'does not publish a AuthorizationsAddedEvent event' do
        expect(::Gitlab::EventStore).not_to(
          receive(:publish_group).with(
            array_including(an_instance_of(::ProjectAuthorizations::AuthorizationsAddedEvent))
          )
        )

        apply_project_authorization_changes
      end
    end

    context 'when new authorizations should be added' do
      let_it_be(:user) { create(:user) }
      let_it_be(:project_1) { create(:project) }
      let_it_be(:project_2) { create(:project) }
      let_it_be(:project_3) { create(:project) }
      let(:project_ids) { [project_1.id, project_2.id, project_3.id] }
      let(:user_ids) { [user.id] }

      let(:authorizations_to_add) do
        [
          { user_id: user.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
          { user_id: user.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
          { user_id: user.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
        ]
      end

      let(:project_authorization_changes) do
        ProjectAuthorizations::Changes.new do |changes|
          changes.add(authorizations_to_add)
        end
      end

      before do
        # Configure as if a replica database is enabled
        allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
      end

      shared_examples_for 'inserts the rows in batches, as per the `per_batch` size, without a delay between batches' do
        specify do
          expect(project_authorization_changes).not_to receive(:sleep)

          apply_project_authorization_changes

          expect(user.project_authorizations.pluck(:user_id, :project_id,
            :access_level, :is_unique)).to match_array(authorizations_to_add.map(&:values))
        end
      end

      context 'when the total number of records to be inserted is greater than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 2)
        end

        it 'inserts the rows in batches, as per the `per_batch` size, with a delay between each batch' do
          expect(ProjectAuthorization).to receive(:insert_all).twice.and_call_original
          expect(project_authorization_changes).to receive(:sleep).twice

          apply_project_authorization_changes

          expect(user.project_authorizations.pluck(:user_id, :project_id,
            :access_level, :is_unique)).to match_array(authorizations_to_add.map(&:values))
        end

        it 'writes is_unique' do
          apply_project_authorization_changes

          expect(user.project_authorizations.pluck(:is_unique)).to all(be(true))
        end

        it_behaves_like 'logs the detail', batch_size: 2
        it_behaves_like 'publishes AuthorizationsAddedEvent'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'

        context 'when the GitLab installation does not have a replica database configured' do
          before do
            # Configure as if a replica database is not enabled
            allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
          end

          it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between batches'
          it_behaves_like 'does not log any detail'
          it_behaves_like 'publishes AuthorizationsAddedEvent'
          it_behaves_like 'does not publish AuthorizationsRemovedEvent'
        end
      end

      context 'when the total number of records to be inserted is less than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 5)
        end

        it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between batches'
        it_behaves_like 'does not log any detail'
        it_behaves_like 'publishes AuthorizationsAddedEvent'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
      end

      describe 'and authorizations should be removed as well' do
        let(:project_authorization_changes) do
          ProjectAuthorizations::Changes.new do |changes|
            changes.add(authorizations_to_add)
            changes.remove_projects_for_user(user, project_ids)
          end
        end

        it_behaves_like 'publishes AuthorizationsAddedEvent'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
      end

      context 'when a single user is added to multiple projects' do
        let(:events_batch_size) { 2 }

        before do
          stub_const("#{described_class}::EVENTS_BATCH_SIZE", events_batch_size)
        end

        it 'publishes events in batches for projects' do
          allow(::Gitlab::EventStore).to receive(:publish_group)

          apply_project_authorization_changes

          expect(::Gitlab::EventStore).to have_received(:publish_group) do |events|
            expect(events.size).to eq(2)

            expect(events.map(&:data)).to match_array([
              { project_ids: [project_1.id, project_2.id], user_ids: [user.id] },
              { project_ids: [project_3.id], user_ids: [user.id] }
            ])
          end
        end
      end

      context 'when multiple users are added to multiple projects' do
        let_it_be(:user_1) { create(:user) }
        let_it_be(:user_2) { create(:user) }
        let_it_be(:user_3) { create(:user) }
        let_it_be(:project_1) { create(:project) }
        let_it_be(:project_2) { create(:project) }
        let_it_be(:project_3) { create(:project) }
        let(:project_ids) { [project_1.id, project_2.id, project_3.id] }
        let(:user_ids) { [user_1.id, user_2.id, user_3.id] }

        let(:authorizations_to_add) do
          [
            { user_id: user_1.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_1.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_1.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_2.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_2.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_2.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_3.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_3.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
            { user_id: user_3.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
          ]
        end

        let(:events_batch_size) { 2 }

        before do
          stub_const("#{described_class}::EVENTS_BATCH_SIZE", events_batch_size)
        end

        it 'publishes events in batches for both users and projects' do
          allow(::Gitlab::EventStore).to receive(:publish_group)

          apply_project_authorization_changes

          expect(::Gitlab::EventStore).to have_received(:publish_group) do |events|
            expect(events.size).to eq(4)

            expect(events.first).to be_a(ProjectAuthorizations::AuthorizationsAddedEvent)
            expect(events.map(&:data)).to match_array([
              { project_ids: [project_1.id, project_2.id], user_ids: [user_1.id, user_2.id] },
              { project_ids: [project_1.id, project_2.id], user_ids: [user_3.id] },
              { project_ids: [project_3.id], user_ids: [user_1.id, user_2.id] },
              { project_ids: [project_3.id], user_ids: [user_3.id] }
            ])
          end
        end
      end
    end

    context 'when authorizations should be deleted for a project' do
      let_it_be(:project) { create(:project) }
      let_it_be(:user_1) { create(:user) }
      let_it_be(:user_2) { create(:user) }
      let_it_be(:user_3) { create(:user) }
      let_it_be(:user_4) { create(:user) }

      let(:user_ids) { [user_1.id, user_2.id, user_3.id] }
      let(:project_ids) { [project.id] }

      let(:project_authorization_changes) do
        ProjectAuthorizations::Changes.new do |changes|
          changes.remove_users_in_project(project, user_ids)
        end
      end

      before do
        # Configure as if a replica database is enabled
        allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
      end

      before_all do
        create(:project_authorization, user: user_1, project: project)
        create(:project_authorization, user: user_2, project: project)
        create(:project_authorization, user: user_3, project: project)
        create(:project_authorization, user: user_4, project: project)
      end

      shared_examples_for 'removes project authorizations of the users in the current project, without a delay' do
        specify do
          expect(project_authorization_changes).not_to receive(:sleep)

          apply_project_authorization_changes

          expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
        end
      end

      shared_examples_for 'does not removes project authorizations of the users in the current project' do
        it 'does not delete any project authorization' do
          expect { apply_project_authorization_changes }.not_to change { project.project_authorizations.count }
        end
      end

      context 'when the total number of records to be removed is greater than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 2)
        end

        it 'removes project authorizations of the users in the current project, with a delay' do
          expect(project_authorization_changes).to receive(:sleep).twice

          apply_project_authorization_changes

          expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
        end

        it_behaves_like 'logs the detail', batch_size: 2
        it_behaves_like 'publishes AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'

        context 'when the GitLab installation does not have a replica database configured' do
          before do
            # Configure as if a replica database is not enabled
            allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
          end

          it_behaves_like 'removes project authorizations of the users in the current project, without a delay'
          it_behaves_like 'does not log any detail'
          it_behaves_like 'publishes AuthorizationsRemovedEvent'
          it_behaves_like 'does not publish AuthorizationsAddedEvent'
        end
      end

      context 'when the total number of records to be removed is less than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 5)
        end

        it_behaves_like 'removes project authorizations of the users in the current project, without a delay'
        it_behaves_like 'does not log any detail'
        it_behaves_like 'publishes AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end

      context 'when the user_ids list is empty' do
        let(:user_ids) { [] }

        it_behaves_like 'does not removes project authorizations of the users in the current project'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end

      context 'when the user_ids list is nil' do
        let(:user_ids) { nil }

        it_behaves_like 'does not removes project authorizations of the users in the current project'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end
    end

    describe 'when authorizations should be deleted for an user' do
      let_it_be(:user) { create(:user) }
      let_it_be(:project_1) { create(:project) }
      let_it_be(:project_2) { create(:project) }
      let_it_be(:project_3) { create(:project) }
      let_it_be(:project_4) { create(:project) }

      let(:project_ids) { [project_1.id, project_2.id, project_3.id] }
      let(:user_ids) { [user.id] }

      let(:project_authorization_changes) do
        ProjectAuthorizations::Changes.new do |changes|
          changes.remove_projects_for_user(user, project_ids)
        end
      end

      before do
        # Configure as if a replica database is enabled
        allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
      end

      before_all do
        create(:project_authorization, user: user, project: project_1)
        create(:project_authorization, user: user, project: project_2)
        create(:project_authorization, user: user, project: project_3)
        create(:project_authorization, user: user, project: project_4)
      end

      shared_examples_for 'removes project authorizations of projects from the current user, without a delay' do
        specify do
          expect(project_authorization_changes).not_to receive(:sleep)

          apply_project_authorization_changes

          expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
        end
      end

      shared_examples_for 'does not removes any project authorizations from the current user' do
        it 'does not delete any project authorization' do
          expect { apply_project_authorization_changes }.not_to change { user.project_authorizations.count }
        end
      end

      context 'when the total number of records to be removed is greater than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 2)
        end

        it 'removes the project authorizations of projects from the current user, with a delay between each batch' do
          expect(project_authorization_changes).to receive(:sleep).twice

          apply_project_authorization_changes

          expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
        end

        it_behaves_like 'logs the detail', batch_size: 2
        it_behaves_like 'publishes AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'

        context 'when the GitLab installation does not have a replica database configured' do
          before do
            # Configure as if a replica database is not enabled
            allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
          end

          it_behaves_like 'removes project authorizations of projects from the current user, without a delay'
          it_behaves_like 'does not log any detail'
          it_behaves_like 'publishes AuthorizationsRemovedEvent'
          it_behaves_like 'does not publish AuthorizationsAddedEvent'
        end
      end

      context 'when the total number of records to be removed is less than the batch size' do
        before do
          stub_const("#{described_class}::BATCH_SIZE", 5)
        end

        it_behaves_like 'removes project authorizations of projects from the current user, without a delay'
        it_behaves_like 'does not log any detail'
        it_behaves_like 'publishes AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end

      context 'when the project_ids list is empty' do
        let(:project_ids) { [] }

        it_behaves_like 'does not removes any project authorizations from the current user'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end

      context 'when the user_ids list is nil' do
        let(:project_ids) { nil }

        it_behaves_like 'does not removes any project authorizations from the current user'
        it_behaves_like 'does not publish AuthorizationsRemovedEvent'
        it_behaves_like 'does not publish AuthorizationsAddedEvent'
      end
    end
  end
end