2019-02-12 03:35:34 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Labels::AvailableLabelsService do
|
2019-02-12 03:35:34 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :public, group: group) }
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
|
|
|
|
let(:project_label) { create(:label, project: project) }
|
|
|
|
let(:other_project_label) { create(:label) }
|
|
|
|
let(:group_label) { create(:group_label, group: group) }
|
|
|
|
let(:other_group_label) { create(:group_label) }
|
2020-08-06 14:09:41 -04:00
|
|
|
let!(:labels) { [project_label, other_project_label, group_label, other_group_label] }
|
2019-02-12 03:35:34 -05:00
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#find_or_create_by_titles' do
|
2019-02-12 03:35:34 -05:00
|
|
|
let(:label_titles) { labels.map(&:title).push('non existing title') }
|
|
|
|
|
|
|
|
context 'when parent is a project' do
|
|
|
|
context 'when a user is not a project member' do
|
|
|
|
it 'returns only relevant label ids' do
|
|
|
|
result = described_class.new(user, project, labels: label_titles).find_or_create_by_titles
|
|
|
|
|
|
|
|
expect(result).to match_array([project_label, group_label])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is a project member' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates new labels for not found titles' do
|
|
|
|
result = described_class.new(user, project, labels: label_titles).find_or_create_by_titles
|
|
|
|
|
|
|
|
expect(result.count).to eq(5)
|
|
|
|
expect(result).to include(project_label, group_label)
|
|
|
|
expect(result).not_to include(other_project_label, other_group_label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when parent is a group' do
|
|
|
|
context 'when a user is not a group member' do
|
|
|
|
it 'returns only relevant label ids' do
|
|
|
|
result = described_class.new(user, group, labels: label_titles).find_or_create_by_titles
|
|
|
|
|
|
|
|
expect(result).to match_array([group_label])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is a group member' do
|
|
|
|
before do
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates new labels for not found titles' do
|
|
|
|
result = described_class.new(user, group, labels: label_titles).find_or_create_by_titles
|
|
|
|
|
|
|
|
expect(result.count).to eq(5)
|
|
|
|
expect(result).to include(group_label)
|
|
|
|
expect(result).not_to include(project_label, other_project_label, other_group_label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#filter_labels_ids_in_param' do
|
2020-04-01 11:07:45 -04:00
|
|
|
let(:label_ids) { labels.map(&:id).push(non_existing_record_id) }
|
2019-02-12 03:35:34 -05:00
|
|
|
|
|
|
|
context 'when parent is a project' do
|
|
|
|
it 'returns only relevant label ids' do
|
|
|
|
result = described_class.new(user, project, ids: label_ids).filter_labels_ids_in_param(:ids)
|
|
|
|
|
|
|
|
expect(result).to match_array([project_label.id, group_label.id])
|
|
|
|
end
|
2020-06-11 08:08:54 -04:00
|
|
|
|
|
|
|
it 'returns labels in preserved order' do
|
|
|
|
result = described_class.new(user, project, ids: label_ids.reverse).filter_labels_ids_in_param(:ids)
|
|
|
|
|
|
|
|
expect(result).to eq([group_label.id, project_label.id])
|
|
|
|
end
|
2019-02-12 03:35:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when parent is a group' do
|
|
|
|
it 'returns only relevant label ids' do
|
|
|
|
result = described_class.new(user, group, ids: label_ids).filter_labels_ids_in_param(:ids)
|
|
|
|
|
|
|
|
expect(result).to match_array([group_label.id])
|
|
|
|
end
|
|
|
|
end
|
2020-08-13 11:10:03 -04:00
|
|
|
|
|
|
|
it 'accepts a single id parameter' do
|
|
|
|
result = described_class.new(user, project, label_id: project_label.id).filter_labels_ids_in_param(:label_id)
|
|
|
|
|
|
|
|
expect(result).to match_array([project_label.id])
|
|
|
|
end
|
2019-02-12 03:35:34 -05:00
|
|
|
end
|
2020-08-06 14:09:41 -04:00
|
|
|
|
|
|
|
describe '#available_labels' do
|
|
|
|
context 'when parent is a project' do
|
|
|
|
it 'returns only relevant labels' do
|
|
|
|
result = described_class.new(user, project, {}).available_labels
|
|
|
|
|
|
|
|
expect(result.count).to eq(2)
|
|
|
|
expect(result).to include(project_label, group_label)
|
|
|
|
expect(result).not_to include(other_project_label, other_group_label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when parent is a group' do
|
|
|
|
it 'returns only relevant labels' do
|
|
|
|
result = described_class.new(user, group, {}).available_labels
|
|
|
|
|
|
|
|
expect(result.count).to eq(1)
|
|
|
|
expect(result).to include(group_label)
|
|
|
|
expect(result).not_to include(project_label, other_project_label, other_group_label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-12 03:35:34 -05:00
|
|
|
end
|