debian-mirror-gitlab/spec/services/work_items/create_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

257 lines
7.8 KiB
Ruby
Raw Normal View History

2022-03-02 08:16:31 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe WorkItems::CreateService, feature_category: :team_planning do
2022-03-02 08:16:31 +05:30
include AfterNextHelpers
2023-06-20 00:43:36 +05:30
RSpec.shared_examples 'creates work item in container' do |container_type|
let_it_be_with_reload(:project) { create(:project) }
let_it_be_with_reload(:group) { create(:group) }
let_it_be(:container) do
case container_type
when :project then project
when :project_namespace then project.project_namespace
when :group then group
end
2022-08-13 15:12:31 +05:30
end
2023-06-20 00:43:36 +05:30
let_it_be(:container_args) do
case container_type
when :project, :project_namespace then { project: project }
when :group then { namespace: group }
end
end
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
let_it_be(:parent) { create(:work_item, **container_args) }
let_it_be(:guest) { create(:user) }
let_it_be(:reporter) { create(:user) }
let_it_be(:user_with_no_access) { create(:user) }
let(:widget_params) { {} }
let(:spam_params) { double }
let(:current_user) { guest }
let(:opts) do
{
title: 'Awesome work_item',
description: 'please fix'
}
2022-03-02 08:16:31 +05:30
end
2023-06-20 00:43:36 +05:30
before_all do
memberships_container = container.is_a?(Namespaces::ProjectNamespace) ? container.reload.project : container
memberships_container.add_guest(guest)
memberships_container.add_reporter(reporter)
end
2022-04-04 11:22:00 +05:30
2023-06-20 00:43:36 +05:30
describe '#execute' do
shared_examples 'fails creating work item and returns errors' do
it 'does not create new work item if parent can not be set' do
expect { service_result }.not_to change(WorkItem, :count)
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
expect(service_result[:status]).to be(:error)
expect(service_result[:message]).to match(error_message)
end
2022-03-02 08:16:31 +05:30
end
2023-06-20 00:43:36 +05:30
let(:service) do
described_class.new(
container: container,
current_user: current_user,
params: opts,
spam_params: spam_params,
widget_params: widget_params
)
end
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
subject(:service_result) { service.execute }
2022-04-04 11:22:00 +05:30
2023-06-20 00:43:36 +05:30
before do
stub_spam_services
2022-03-02 08:16:31 +05:30
end
2022-08-27 11:52:29 +05:30
2023-06-20 00:43:36 +05:30
context 'when user is not allowed to create a work item in the container' do
let(:current_user) { user_with_no_access }
it { is_expected.to be_error }
2022-08-27 11:52:29 +05:30
2023-06-20 00:43:36 +05:30
it 'returns an access error' do
expect(service_result.errors).to contain_exactly('Operation not allowed')
end
2022-08-27 11:52:29 +05:30
end
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when applying quick actions' do
let(:work_item) { service_result[:work_item] }
let(:opts) do
{
title: 'My work item',
work_item_type: work_item_type,
description: '/shrug'
}
end
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when work item type is not the default Issue' do
let(:work_item_type) { create(:work_item_type, :task, namespace: group) }
2022-04-04 11:22:00 +05:30
2023-06-20 00:43:36 +05:30
it 'saves the work item without applying the quick action' do
expect(service_result).to be_success
expect(work_item).to be_persisted
expect(work_item.description).to eq('/shrug')
end
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when work item type is the default Issue' do
let(:work_item_type) { WorkItems::Type.default_by_type(:issue) }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
it 'saves the work item and applies the quick action' do
expect(service_result).to be_success
expect(work_item).to be_persisted
expect(work_item.description).to eq(' ¯\_(ツ)_/¯')
end
end
2022-08-13 15:12:31 +05:30
end
2022-03-02 08:16:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when params are valid' do
it 'created instance is a WorkItem' do
expect(Issuable::CommonSystemNotesService).to receive_message_chain(:new, :execute)
work_item = service_result[:work_item]
expect(work_item).to be_persisted
expect(work_item).to be_a(::WorkItem)
expect(work_item.title).to eq('Awesome work_item')
expect(work_item.description).to eq('please fix')
expect(work_item.work_item_type.base_type).to eq('issue')
2022-03-02 08:16:31 +05:30
end
2023-06-20 00:43:36 +05:30
it 'calls NewIssueWorker with correct arguments' do
expect(NewIssueWorker).to receive(:perform_async).with(Integer, current_user.id, 'WorkItem')
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
service_result
end
2022-08-13 15:12:31 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when params are invalid' do
let(:opts) { { title: '' } }
it { is_expected.to be_error }
it 'returns validation errors' do
expect(service_result.errors).to contain_exactly("Title can't be blank")
end
it 'does not execute after-create transaction widgets' do
expect(service).to receive(:create).and_call_original
expect(service).not_to receive(:execute_widgets)
.with(callback: :after_create_in_transaction, widget_params: widget_params)
service_result
end
2022-08-13 15:12:31 +05:30
end
2023-06-20 00:43:36 +05:30
context 'checking spam' do
it 'executes SpamActionService' do
expect_next_instance_of(
Spam::SpamActionService,
{
spammable: kind_of(WorkItem),
spam_params: spam_params,
user: an_instance_of(User),
action: :create
}
) do |instance|
expect(instance).to receive(:execute)
end
service_result
end
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'work item widgetable service' do
let(:widget_params) do
2022-08-13 15:12:31 +05:30
{
2023-06-20 00:43:36 +05:30
hierarchy_widget: { parent: parent }
2022-08-13 15:12:31 +05:30
}
2023-06-20 00:43:36 +05:30
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
let(:service) do
described_class.new(
container: container,
current_user: current_user,
params: opts,
spam_params: spam_params,
widget_params: widget_params
)
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
let(:service_execute) { service.execute }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
let(:supported_widgets) do
[
{
klass: WorkItems::Widgets::HierarchyService::CreateService,
callback: :after_create_in_transaction,
params: { parent: parent }
}
]
2022-08-13 15:12:31 +05:30
end
end
2023-06-20 00:43:36 +05:30
describe 'hierarchy widget' do
let(:widget_params) { { hierarchy_widget: { parent: parent } } }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when user can admin parent link' do
let(:current_user) { reporter }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
context 'when parent is valid work item' do
let(:opts) do
{
title: 'Awesome work_item',
description: 'please fix',
work_item_type: WorkItems::Type.default_by_type(:task)
}
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
it 'creates new work item and sets parent reference' do
expect { service_result }.to change(WorkItem, :count).by(1).and(
change(WorkItems::ParentLink, :count).by(1)
)
expect(service_result[:status]).to be(:success)
end
2022-08-13 15:12:31 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when parent type is invalid' do
let_it_be(:parent) { create(:work_item, :task, **container_args) }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'fails creating work item and returns errors' do
let(:error_message) { 'is not allowed to add this type of parent' }
end
2022-08-13 15:12:31 +05:30
end
end
2023-06-20 00:43:36 +05:30
context 'when user cannot admin parent link' do
let(:current_user) { guest }
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
let(:opts) do
{
title: 'Awesome work_item',
description: 'please fix',
work_item_type: WorkItems::Type.default_by_type(:task)
}
end
2022-08-13 15:12:31 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'fails creating work item and returns errors' do
let(:error_message) { 'No matching work item found. Make sure that you are adding a valid work item ID.' }
end
2022-08-13 15:12:31 +05:30
end
end
end
2022-03-02 08:16:31 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'creates work item in container', :project
it_behaves_like 'creates work item in container', :project_namespace
it_behaves_like 'creates work item in container', :group
2022-03-02 08:16:31 +05:30
end