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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Label, feature_category: :team_planning do
using RSpec::Parameterized::TableSyntax
let_it_be(:project) { create(:project) }
describe 'modules' do
it { is_expected.to include_module(Referable) }
it { is_expected.to include_module(Subscribable) }
end
describe 'associations' do
it { is_expected.to have_many(:issues).through(:label_links).source(:target) }
it { is_expected.to have_many(:label_links).dependent(:destroy) }
it { is_expected.to have_many(:lists).dependent(:destroy) }
it { is_expected.to have_many(:priorities).class_name('LabelPriority') }
end
describe 'validation' do
it { is_expected.to validate_uniqueness_of(:title).scoped_to([:group_id, :project_id]) }
it 'validates color code' do
is_expected.not_to allow_value('G-ITLAB').for(:color)
is_expected.not_to allow_value('AABBCC').for(:color)
is_expected.not_to allow_value('#AABBCCEE').for(:color)
is_expected.not_to allow_value('GGHHII').for(:color)
is_expected.not_to allow_value('#').for(:color)
is_expected.not_to allow_value('').for(:color)
is_expected.to allow_value('#AABBCC').for(:color)
is_expected.to allow_value('#abcdef').for(:color)
end
it 'validates title' do
is_expected.not_to allow_value('G,ITLAB').for(:title)
is_expected.not_to allow_value('').for(:title)
is_expected.not_to allow_value('s' * 256).for(:title)
is_expected.to allow_value('GITLAB').for(:title)
is_expected.to allow_value('gitlab').for(:title)
is_expected.to allow_value('G?ITLAB').for(:title)
is_expected.to allow_value('G&ITLAB').for(:title)
is_expected.to allow_value("customer's request").for(:title)
is_expected.to allow_value('s' * 255).for(:title)
end
describe 'description length' do
let(:invalid_description) { 'x' * (::Label::DESCRIPTION_LENGTH_MAX + 1) }
let(:valid_description) { 'short description' }
let(:label) { build(:label, project: project, description: description) }
let(:error_message) do
format(
_('is too long (%{size}). The maximum size is %{max_size}.'),
size: ActiveSupport::NumberHelper.number_to_human_size(invalid_description.bytesize),
max_size: ActiveSupport::NumberHelper.number_to_human_size(::Label::DESCRIPTION_LENGTH_MAX)
)
end
subject(:validate) { label.validate }
context 'when label is a new record' do
context 'when description exceeds the maximum size' do
let(:description) { invalid_description }
it 'adds a description too long error' do
validate
expect(label.errors[:description]).to contain_exactly(error_message)
end
end
context 'when description is within the allowed limits' do
let(:description) { valid_description }
it 'does not add a validation error' do
validate
expect(label.errors).not_to have_key(:description)
end
end
end
context 'when label is an existing record' do
before do
label.description = existing_description
label.save!(validate: false)
label.description = description
end
context 'when record already had a valid description' do
let(:existing_description) { 'small difference so it triggers description_changed?' }
context 'when new description exceeds the maximum size' do
let(:description) { invalid_description }
it 'adds a description too long error' do
validate
expect(label.errors[:description]).to contain_exactly(error_message)
end
end
context 'when new description is within the allowed limits' do
let(:description) { valid_description }
it 'does not add a validation error' do
validate
expect(label.errors).not_to have_key(:description)
end
end
end
context 'when record existed with an invalid description' do
let(:existing_description) { "#{invalid_description} small difference so it triggers description_changed?" }
context 'when description is not changed' do
let(:description) { existing_description }
it 'does not add a validation error' do
validate
expect(label.errors).not_to have_key(:description)
end
end
context 'when new description exceeds the maximum size' do
context 'when new description is shorter than existing description' do
let(:description) { invalid_description }
it 'allows updating descriptions that already existed above the limit' do
validate
expect(label.errors).not_to have_key(:description)
end
end
context 'when new description is longer than existing description' do
let(:description) { "#{existing_description}1" }
it 'adds a description too long error' do
validate
expect(label.errors[:description]).to contain_exactly(error_message)
end
end
end
context 'when new description is within the allowed limits' do
let(:description) { valid_description }
it 'does not add a validation error' do
validate
expect(label.errors).not_to have_key(:description)
end
end
end
end
end
end
describe 'ensure_lock_on_merge_allowed' do
let(:validation_error) { 'can not be set for template labels' }
# rubocop:disable Rails/SaveBang
context 'when creating a label' do
let(:label) { described_class.create(title: 'test', template: template, lock_on_merge: lock_on_merge) }
where(:template, :lock_on_merge, :valid, :errors) do
false | false | true | []
false | true | true | []
true | false | true | []
true | true | false | [validation_error]
false | true | true | []
end
with_them do
it 'validates lock_on_merge on label creation' do
expect(label.valid?).to be(valid)
expect(label.errors[:lock_on_merge]).to eq(errors)
end
end
end
# rubocop:enable Rails/SaveBang
context 'when updating a label' do
let_it_be(:template_label) { create(:label, template: true) }
where(:lock_on_merge, :valid, :errors) do
true | false | [validation_error]
false | true | []
end
with_them do
it 'validates lock_on_merge value if label is a template' do
template_label.update_column(:lock_on_merge, lock_on_merge)
expect(template_label.valid?).to be(valid)
expect(template_label.errors[:lock_on_merge]).to eq(errors)
end
end
end
end
describe 'scopes' do
describe '.on_board' do
let(:board) { create(:board, project: project) }
let!(:list1) { create(:list, board: board, label: development) }
let!(:list2) { create(:list, board: board, label: testing) }
let!(:development) { create(:label, project: project, name: 'Development') }
let!(:testing) { create(:label, project: project, name: 'Testing') }
let!(:regression) { create(:label, project: project, name: 'Regression') }
it 'returns only the board labels' do
expect(described_class.on_board(board.id)).to match_array([development, testing])
end
end
describe '.with_lock_on_merge' do
let(:label) { create(:label, project: project, name: 'Label') }
let(:label_locked) { create(:label, project: project, name: 'Label locked', lock_on_merge: true) }
it 'return only locked labels' do
expect(described_class.with_lock_on_merge).to match_array([label_locked])
end
end
end
describe 'destroying labels' do
context 'when lock_on_merge is true' do
it 'prevents label from being destroyed' do
label = create(:label, lock_on_merge: true)
expect(label.destroy).to be false
expect(label.errors.full_messages).to include("#{label.name} is locked and was not removed")
end
end
context 'when lock_on_merge is false' do
it 'allows label to be destroyed' do
label = create(:label, lock_on_merge: false)
expect(label.destroy).to eq label
expect(label.destroyed?).to be_truthy
end
end
end
describe '#color' do
it 'strips color' do
label = described_class.new(color: ' #abcdef ')
label.valid?
expect(label.color).to be_color('#abcdef')
end
it 'uses default color if color is missing' do
label = described_class.new(color: nil)
expect(label.color).to be_color(Label::DEFAULT_COLOR)
end
end
describe '#text_color' do
it 'uses default color if color is missing' do
label = described_class.new(color: nil)
expect(label.text_color).to eq(Label::DEFAULT_COLOR.contrast)
end
end
describe '#title' do
it 'sanitizes title' do
label = described_class.new(title: '<b>foo & bar?</b>')
expect(label.title).to eq('foo & bar?')
end
it 'strips title' do
label = described_class.new(title: ' label ')
label.valid?
expect(label.title).to eq('label')
end
end
describe '#description' do
it 'sanitizes description' do
label = described_class.new(description: '<b>foo & bar?</b>')
expect(label.description).to eq('foo & bar?')
end
it 'accepts an empty string' do
label = described_class.new(title: 'foo', description: 'bar')
label.update!(description: '')
expect(label.description).to eq('')
end
end
describe 'priorization' do
subject(:label) { create(:label, project: project) }
describe '#prioritize!' do
context 'when label is not prioritized' do
it 'creates a label priority' do
expect { label.prioritize!(project, 1) }.to change(label.priorities, :count).by(1)
end
it 'sets label priority' do
label.prioritize!(project, 1)
expect(label.priorities.first.priority).to eq 1
end
end
context 'when label is prioritized' do
let!(:priority) { create(:label_priority, project: project, label: label, priority: 0) }
it 'does not create a label priority' do
expect { label.prioritize!(project, 1) }.not_to change(label.priorities, :count)
end
it 'updates label priority' do
label.prioritize!(project, 1)
expect(priority.reload.priority).to eq 1
end
end
end
describe '#unprioritize!' do
it 'removes label priority' do
create(:label_priority, project: project, label: label, priority: 0)
expect { label.unprioritize!(project) }.to change(label.priorities, :count).by(-1)
end
end
describe '#priority' do
context 'when label is not prioritized' do
it 'returns nil' do
expect(label.priority(project)).to be_nil
end
end
context 'when label is prioritized' do
it 'returns label priority' do
create(:label_priority, project: project, label: label, priority: 1)
expect(label.priority(project)).to eq 1
end
end
end
end
describe '.search' do
let(:label) { create(:label, title: 'bug', description: 'incorrect behavior') }
it 'returns labels with a partially matching title' do
expect(described_class.search(label.title[0..2])).to eq([label])
end
it 'returns labels with a partially matching description' do
expect(described_class.search(label.description[0..5])).to eq([label])
end
it 'returns nothing' do
expect(described_class.search('feature')).to be_empty
end
end
describe '.subscribed_by' do
let!(:user) { create(:user) }
let!(:label) { create(:label) }
let!(:label2) { create(:label) }
before do
label.subscribe(user)
end
it 'returns subscribed labels' do
expect(described_class.subscribed_by(user.id)).to eq([label])
end
it 'returns nothing' do
expect(described_class.subscribed_by(0)).to be_empty
end
end
describe '.top_labels_by_target' do
let(:label) { create(:label) }
let(:popular_label) { create(:label) }
let(:merge_request1) { create(:merge_request) }
let(:merge_request2) { create(:merge_request) }
before do
merge_request1.labels = [label, popular_label]
merge_request2.labels = [popular_label]
end
it 'returns distinct labels, ordered by usage in the given target relation' do
top_labels = described_class.top_labels_by_target(MergeRequest.all)
expect(top_labels).to match_array([popular_label, label])
end
it 'excludes labels that are not assigned to any records in the given target relation' do
merge_requests = MergeRequest.where(id: merge_request2.id)
top_labels = described_class.top_labels_by_target(merge_requests)
expect(top_labels).to match_array([popular_label])
end
end
describe '.optionally_subscribed_by' do
let!(:user) { create(:user) }
let!(:label) { create(:label) }
let!(:label2) { create(:label) }
before do
label.subscribe(user)
end
it 'returns subscribed labels' do
expect(described_class.optionally_subscribed_by(user.id)).to eq([label])
end
it 'returns all labels if user_id is nil' do
expect(described_class.optionally_subscribed_by(nil)).to match_array([label, label2])
end
end
describe '#templates' do
context 'with invalid template labels' do
it 'returns only valid template labels' do
create(:label)
# Project labels should not have template set to true
create(:label, template: true)
valid_template_label = described_class.create!(title: 'test', template: true, type: nil)
expect(described_class.templates).to eq([valid_template_label])
end
end
end
describe '.pluck_titles' do
subject(:pluck_titles) { described_class.pluck_titles }
it 'returns the audit event type of the event type filter' do
label1 = create(:label, title: "TITLE1")
label2 = create(:label, title: "TITLE2")
expect(pluck_titles).to contain_exactly(label1.title, label2.title)
end
end
end
|