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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ProjectAuthorization do
describe 'unique user, project authorizations' do
let_it_be(:user) { create(:user) }
let_it_be(:project_1) { create(:project) }
let!(:project_auth) do
create(
:project_authorization,
user: user,
project: project_1,
access_level: Gitlab::Access::DEVELOPER
)
end
context 'with duplicate user and project authorization' do
subject { project_auth.dup }
it { is_expected.to be_invalid }
context 'after validation' do
before do
subject.valid?
end
it 'contains duplicate error' do
expect(subject.errors[:user]).to include('has already been taken')
end
end
end
context 'with multiple access levels for the same user and project' do
subject do
project_auth.dup.tap do |auth|
auth.access_level = Gitlab::Access::MAINTAINER
end
end
it { is_expected.to be_invalid }
context 'after validation' do
before do
subject.valid?
end
it 'contains duplicate error' do
expect(subject.errors[:user]).to include('has already been taken')
end
end
end
end
describe 'relations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:project) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:access_level) }
it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.all_values) }
end
describe '.insert_all' 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) }
it 'skips duplicates and inserts the remaining rows without error' do
create(:project_authorization, user: user, project: project_1, access_level: Gitlab::Access::MAINTAINER)
attributes = [
{ 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 }
]
described_class.insert_all(attributes)
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
end
end
describe '.insert_all_in_batches' 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(:attributes) 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
before do
# Configure as if a replica database is enabled
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
end
shared_examples_for 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch' do
specify do
expect(described_class).not_to receive(:sleep)
described_class.insert_all_in_batches(attributes, per_batch_size)
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
end
end
context 'when the total number of records to be inserted is greater than the batch size' do
let(:per_batch_size) { 2 }
it 'inserts the rows in batches, as per the `per_batch` size, with a delay between each batch' do
expect(described_class).to receive(:insert_all).twice.and_call_original
expect(described_class).to receive(:sleep).twice
described_class.insert_all_in_batches(attributes, per_batch_size)
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
end
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 each batch'
end
end
context 'when the total number of records to be inserted is less than the batch size' do
let(:per_batch_size) { 5 }
it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch'
end
end
describe '.delete_all_in_batches_for_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] }
before do
# Configure as if a replica database is enabled
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
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 the project authorizations of the specified users in the current project, without a delay between each batch' do
specify do
expect(described_class).not_to receive(:sleep)
described_class.delete_all_in_batches_for_project(
project: project,
user_ids: user_ids,
per_batch: per_batch_size
)
expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
end
end
context 'when the total number of records to be removed is greater than the batch size' do
let(:per_batch_size) { 2 }
it 'removes the project authorizations of the specified users in the current project, with a delay between each batch' do
expect(described_class).to receive(:sleep).twice
described_class.delete_all_in_batches_for_project(
project: project,
user_ids: user_ids,
per_batch: per_batch_size
)
expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
end
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 the project authorizations of the specified users in the current project, without a delay between each batch'
end
end
context 'when the total number of records to be removed is less than the batch size' do
let(:per_batch_size) { 5 }
it_behaves_like 'removes the project authorizations of the specified users in the current project, without a delay between each batch'
end
end
describe '.delete_all_in_batches_for_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] }
before do
# Configure as if a replica database is enabled
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
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 the project authorizations of the specified projects from the current user, without a delay between each batch' do
specify do
expect(described_class).not_to receive(:sleep)
described_class.delete_all_in_batches_for_user(
user: user,
project_ids: project_ids,
per_batch: per_batch_size
)
expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
end
end
context 'when the total number of records to be removed is greater than the batch size' do
let(:per_batch_size) { 2 }
it 'removes the project authorizations of the specified projects from the current user, with a delay between each batch' do
expect(described_class).to receive(:sleep).twice
described_class.delete_all_in_batches_for_user(
user: user,
project_ids: project_ids,
per_batch: per_batch_size
)
expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
end
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 the project authorizations of the specified projects from the current user, without a delay between each batch'
end
end
context 'when the total number of records to be removed is less than the batch size' do
let(:per_batch_size) { 5 }
it_behaves_like 'removes the project authorizations of the specified projects from the current user, without a delay between each batch'
end
end
end
|