2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-07-29 10:10:15 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Label do
|
2021-04-07 17:09:01 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
2016-09-20 16:07:56 -04:00
|
|
|
describe 'modules' do
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
it { is_expected.to include_module(Subscribable) }
|
|
|
|
end
|
2014-07-29 10:10:15 -04:00
|
|
|
|
2015-05-02 23:11:21 -04:00
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to have_many(:issues).through(:label_links).source(:target) }
|
2016-09-20 16:07:56 -04:00
|
|
|
it { is_expected.to have_many(:label_links).dependent(:destroy) }
|
2016-08-08 15:14:22 -04:00
|
|
|
it { is_expected.to have_many(:lists).dependent(:destroy) }
|
2016-10-14 15:12:07 -04:00
|
|
|
it { is_expected.to have_many(:priorities).class_name('LabelPriority') }
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validation' do
|
2016-09-21 16:47:58 -04:00
|
|
|
it { is_expected.to validate_uniqueness_of(:title).scoped_to([:group_id, :project_id]) }
|
2014-08-20 06:31:16 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'validates color code' do
|
2016-09-20 16:07:56 -04:00
|
|
|
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)
|
2014-08-20 06:31:16 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'validates title' do
|
2016-09-20 16:07:56 -04:00
|
|
|
is_expected.not_to allow_value('G,ITLAB').for(:title)
|
|
|
|
is_expected.not_to allow_value('').for(:title)
|
2016-08-11 03:37:09 -04:00
|
|
|
is_expected.not_to allow_value('s' * 256).for(:title)
|
2016-09-20 16:07:56 -04:00
|
|
|
|
|
|
|
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)
|
2016-08-11 03:37:09 -04:00
|
|
|
is_expected.to allow_value('s' * 255).for(:title)
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2017-04-11 17:56:33 -04:00
|
|
|
describe '#color' do
|
|
|
|
it 'strips color' do
|
|
|
|
label = described_class.new(color: ' #abcdef ')
|
|
|
|
label.valid?
|
|
|
|
|
2022-03-02 19:20:18 -05:00
|
|
|
expect(label.color).to be_color('#abcdef')
|
2017-04-11 17:56:33 -04:00
|
|
|
end
|
2017-05-24 08:36:00 -04:00
|
|
|
|
|
|
|
it 'uses default color if color is missing' do
|
|
|
|
label = described_class.new(color: nil)
|
|
|
|
|
2022-03-02 19:20:18 -05:00
|
|
|
expect(label.color).to be_color(Label::DEFAULT_COLOR)
|
2017-05-24 08:36:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#text_color' do
|
|
|
|
it 'uses default color if color is missing' do
|
|
|
|
label = described_class.new(color: nil)
|
|
|
|
|
2022-03-02 19:20:18 -05:00
|
|
|
expect(label.text_color).to eq(Label::DEFAULT_COLOR.contrast)
|
2017-05-24 08:36:00 -04:00
|
|
|
end
|
2017-04-11 17:56:33 -04:00
|
|
|
end
|
|
|
|
|
2016-06-16 19:09:13 -04:00
|
|
|
describe '#title' do
|
|
|
|
it 'sanitizes title' do
|
|
|
|
label = described_class.new(title: '<b>foo & bar?</b>')
|
|
|
|
expect(label.title).to eq('foo & bar?')
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
2017-04-11 17:56:33 -04:00
|
|
|
|
|
|
|
it 'strips title' do
|
|
|
|
label = described_class.new(title: ' label ')
|
|
|
|
label.valid?
|
|
|
|
|
|
|
|
expect(label.title).to eq('label')
|
|
|
|
end
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
2016-10-17 14:34:22 -04:00
|
|
|
|
2019-07-15 07:29:56 -04:00
|
|
|
describe '#description' do
|
|
|
|
it 'sanitizes description' do
|
|
|
|
label = described_class.new(description: '<b>foo & bar?</b>')
|
|
|
|
expect(label.description).to eq('foo & bar?')
|
|
|
|
end
|
2022-03-10 04:08:06 -05:00
|
|
|
|
|
|
|
it 'accepts an empty string' do
|
|
|
|
label = described_class.new(title: 'foo', description: 'bar')
|
|
|
|
label.update!(description: '')
|
|
|
|
expect(label.description).to eq('')
|
|
|
|
end
|
2019-07-15 07:29:56 -04:00
|
|
|
end
|
|
|
|
|
2016-10-17 14:34:22 -04:00
|
|
|
describe 'priorization' do
|
2021-04-07 17:09:01 -04:00
|
|
|
subject(:label) { create(:label, project: project) }
|
2016-10-17 14:34:22 -04:00
|
|
|
|
|
|
|
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
|
2018-07-23 07:09:53 -04:00
|
|
|
|
|
|
|
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
|
2018-09-27 10:19:28 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-02-14 19:08:48 -05:00
|
|
|
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
|
|
|
|
|
2018-09-27 10:19:28 -04:00
|
|
|
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
|
2019-07-24 02:53:29 -04:00
|
|
|
|
|
|
|
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
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|