2016-10-18 02:11:33 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Labels::FindOrCreateService do
|
2016-10-18 02:11:33 +00:00
|
|
|
describe '#execute' do
|
|
|
|
let(:group) { create(:group) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, namespace: group) }
|
2016-10-18 02:11:33 +00:00
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
title: 'Security',
|
|
|
|
description: 'Security related stuff.',
|
|
|
|
color: '#FF0000'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-28 09:31:45 +00:00
|
|
|
context 'when acting on behalf of a specific user' do
|
|
|
|
let(:user) { create(:user) }
|
2016-10-18 02:11:33 +00:00
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
context 'when finding labels on project level' do
|
|
|
|
subject(:service) { described_class.new(user, project, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
2016-10-28 09:31:45 +00:00
|
|
|
end
|
2016-10-18 02:11:33 +00:00
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
context 'when label does not exist at group level' do
|
|
|
|
it 'creates a new label at project level' do
|
|
|
|
expect { service.execute }.to change(project.labels, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
2016-10-18 02:11:33 +00:00
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
context 'when label exists at group level' do
|
|
|
|
it 'returns the group label' do
|
|
|
|
group_label = create(:group_label, group: group, title: 'Security')
|
|
|
|
|
|
|
|
expect(service.execute).to eq group_label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when label exists at project level' do
|
|
|
|
it 'returns the project label' do
|
|
|
|
project_label = create(:label, project: project, title: 'Security')
|
|
|
|
|
|
|
|
expect(service.execute).to eq project_label
|
|
|
|
end
|
2016-10-28 09:31:45 +00:00
|
|
|
end
|
2018-07-04 09:52:58 +00:00
|
|
|
|
|
|
|
context 'when include_ancestor_groups is true' do
|
|
|
|
let(:group) { create(:group, :nested) }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
title: 'Audit',
|
|
|
|
include_ancestor_groups: true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ancestor group labels' do
|
|
|
|
group_label = create(:group_label, group: group.parent, title: 'Audit')
|
|
|
|
|
|
|
|
expect(service.execute).to eq group_label
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates new labels if labels are not found' do
|
|
|
|
expect { service.execute }.to change(project.labels, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
2016-10-18 02:11:33 +00:00
|
|
|
end
|
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
context 'when finding labels on group level' do
|
|
|
|
subject(:service) { described_class.new(user, group, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when label does not exist at group level' do
|
|
|
|
it 'creates a new label at group level' do
|
|
|
|
expect { service.execute }.to change(group.labels, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when label exists at group level' do
|
|
|
|
it 'returns the group label' do
|
|
|
|
group_label = create(:group_label, group: group, title: 'Security')
|
|
|
|
|
|
|
|
expect(service.execute).to eq group_label
|
|
|
|
end
|
2016-10-28 09:31:45 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-26 16:23:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authorization is not required' do
|
|
|
|
context 'when finding labels on project level' do
|
|
|
|
subject(:service) { described_class.new(nil, project, params) }
|
2016-10-28 09:31:45 +00:00
|
|
|
|
|
|
|
it 'returns the project label' do
|
|
|
|
project_label = create(:label, project: project, title: 'Security')
|
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
expect(service.execute(skip_authorization: true)).to eq project_label
|
2016-10-28 09:31:45 +00:00
|
|
|
end
|
2016-10-18 02:11:33 +00:00
|
|
|
end
|
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
context 'when finding labels on group level' do
|
|
|
|
subject(:service) { described_class.new(nil, group, params) }
|
2016-10-28 09:31:45 +00:00
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
it 'returns the group label' do
|
|
|
|
group_label = create(:group_label, group: group, title: 'Security')
|
2016-10-18 02:11:33 +00:00
|
|
|
|
2018-02-26 16:23:19 +00:00
|
|
|
expect(service.execute(skip_authorization: true)).to eq group_label
|
|
|
|
end
|
2016-10-18 02:11:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|