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

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

405 lines
14 KiB
Ruby
Raw Normal View History

2022-04-04 11:22:00 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe WorkItems::UpdateService do
let_it_be(:developer) { create(:user) }
2022-08-27 11:52:29 +05:30
let_it_be(:guest) { create(:user) }
let_it_be(:project) { create(:project) }
2022-08-13 15:12:31 +05:30
let_it_be(:parent) { create(:work_item, project: project) }
2022-04-04 11:22:00 +05:30
let_it_be_with_reload(:work_item) { create(:work_item, project: project, assignees: [developer]) }
let(:spam_params) { double }
2022-07-23 23:45:48 +05:30
let(:widget_params) { {} }
2022-04-04 11:22:00 +05:30
let(:opts) { {} }
let(:current_user) { developer }
2022-08-27 11:52:29 +05:30
before do
project.add_developer(developer)
project.add_guest(guest)
end
2022-04-04 11:22:00 +05:30
describe '#execute' do
2022-08-27 11:52:29 +05:30
let(:service) do
2022-08-13 15:12:31 +05:30
described_class.new(
2023-04-23 21:23:45 +05:30
container: project,
2022-08-13 15:12:31 +05:30
current_user: current_user,
params: opts,
spam_params: spam_params,
widget_params: widget_params
2022-08-27 11:52:29 +05:30
)
2022-08-13 15:12:31 +05:30
end
2022-04-04 11:22:00 +05:30
2022-08-27 11:52:29 +05:30
subject(:update_work_item) { service.execute(work_item) }
2022-04-04 11:22:00 +05:30
before do
stub_spam_services
end
2022-08-27 11:52:29 +05:30
shared_examples 'update service that triggers graphql dates updated subscription' do
it 'triggers graphql subscription issueableDatesUpdated' do
expect(GraphqlTriggers).to receive(:issuable_dates_updated).with(work_item).and_call_original
update_work_item
end
end
2022-04-04 11:22:00 +05:30
context 'when title is changed' do
let(:opts) { { title: 'changed' } }
it 'triggers issuable_title_updated graphql subscription' do
expect(GraphqlTriggers).to receive(:issuable_title_updated).with(work_item).and_call_original
2022-05-07 20:08:51 +05:30
expect(Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter).to receive(:track_work_item_title_changed_action).with(author: current_user)
# During the work item transition we also want to track work items as issues
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_title_changed_action)
2022-08-13 15:12:31 +05:30
expect(update_work_item[:status]).to eq(:success)
2022-04-04 11:22:00 +05:30
end
2022-10-11 01:57:18 +05:30
it_behaves_like 'issue_edit snowplow tracking' do
let(:property) { Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_TITLE_CHANGED }
let(:user) { current_user }
subject(:service_action) { update_work_item[:status] }
end
2022-04-04 11:22:00 +05:30
end
context 'when title is not changed' do
let(:opts) { { description: 'changed' } }
it 'does not trigger issuable_title_updated graphql subscription' do
expect(GraphqlTriggers).not_to receive(:issuable_title_updated)
2022-05-07 20:08:51 +05:30
expect(Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter).not_to receive(:track_work_item_title_changed_action)
2022-08-13 15:12:31 +05:30
expect(update_work_item[:status]).to eq(:success)
2022-04-04 11:22:00 +05:30
end
2022-10-11 01:57:18 +05:30
it 'does not emit Snowplow event', :snowplow do
expect_no_snowplow_event
update_work_item
end
2022-04-04 11:22:00 +05:30
end
2022-08-27 11:52:29 +05:30
context 'when dates are changed' do
let(:opts) { { start_date: Date.today } }
it 'tracks users updating work item dates' do
expect(Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter).to receive(:track_work_item_date_changed_action).with(author: current_user)
update_work_item
end
end
2022-11-25 23:54:43 +05:30
context 'when decription is changed' do
let(:opts) { { description: 'description changed' } }
it 'triggers GraphQL description updated subscription' do
expect(GraphqlTriggers).to receive(:issuable_description_updated).with(work_item).and_call_original
update_work_item
end
end
context 'when decription is not changed' do
let(:opts) { { title: 'title changed' } }
it 'does not trigger GraphQL description updated subscription' do
expect(GraphqlTriggers).not_to receive(:issuable_description_updated)
update_work_item
end
end
2022-04-04 11:22:00 +05:30
context 'when updating state_event' do
context 'when state_event is close' do
let(:opts) { { state_event: 'close' } }
it 'closes the work item' do
expect do
update_work_item
work_item.reload
end.to change(work_item, :state).from('opened').to('closed')
end
end
context 'when state_event is reopen' do
let(:opts) { { state_event: 'reopen' } }
before do
work_item.close!
end
it 'reopens the work item' do
expect do
update_work_item
work_item.reload
end.to change(work_item, :state).from('closed').to('opened')
end
end
end
2022-07-23 23:45:48 +05:30
2022-08-13 15:12:31 +05:30
it_behaves_like 'work item widgetable service' do
let(:widget_params) do
{
hierarchy_widget: { parent: parent },
2022-08-27 11:52:29 +05:30
description_widget: { description: 'foo' }
2022-08-13 15:12:31 +05:30
}
end
let(:service) do
described_class.new(
2023-04-23 21:23:45 +05:30
container: project,
2022-08-13 15:12:31 +05:30
current_user: current_user,
params: opts,
spam_params: spam_params,
widget_params: widget_params
)
end
let(:service_execute) { service.execute(work_item) }
let(:supported_widgets) do
[
2022-08-27 11:52:29 +05:30
{ klass: WorkItems::Widgets::DescriptionService::UpdateService, callback: :before_update_callback, params: { description: 'foo' } },
2022-08-13 15:12:31 +05:30
{ klass: WorkItems::Widgets::HierarchyService::UpdateService, callback: :before_update_in_transaction, params: { parent: parent } }
]
end
end
2022-07-23 23:45:48 +05:30
context 'when updating widgets' do
2022-08-13 15:12:31 +05:30
let(:widget_service_class) { WorkItems::Widgets::DescriptionService::UpdateService }
let(:widget_params) { { description_widget: { description: 'changed' } } }
context 'when widget service is not present' do
before do
allow(widget_service_class).to receive(:new).and_return(nil)
end
it 'ignores widget param' do
expect { update_work_item }.not_to change(work_item, :description)
end
end
2022-07-23 23:45:48 +05:30
2022-08-13 15:12:31 +05:30
context 'when the widget does not support update callback' do
before do
allow_next_instance_of(widget_service_class) do |instance|
allow(instance)
2022-08-27 11:52:29 +05:30
.to receive(:before_update_callback)
2022-08-13 15:12:31 +05:30
.with(params: { description: 'changed' }).and_return(nil)
end
end
it 'ignores widget param' do
expect { update_work_item }.not_to change(work_item, :description)
end
end
context 'for the description widget' do
2022-07-23 23:45:48 +05:30
it 'updates the description of the work item' do
update_work_item
expect(work_item.description).to eq('changed')
end
2022-08-27 11:52:29 +05:30
context 'with mentions', :mailer, :sidekiq_might_not_need_inline do
shared_examples 'creates the todo and sends email' do |attribute|
it 'creates a todo and sends email' do
expect { perform_enqueued_jobs { update_work_item } }.to change(Todo, :count).by(1)
expect(work_item.reload.attributes[attribute.to_s]).to eq("mention #{guest.to_reference}")
should_email(guest)
end
end
context 'when description contains a user mention' do
let(:widget_params) { { description_widget: { description: "mention #{guest.to_reference}" } } }
it_behaves_like 'creates the todo and sends email', :description
end
context 'when title contains a user mention' do
let(:opts) { { title: "mention #{guest.to_reference}" } }
it_behaves_like 'creates the todo and sends email', :title
end
end
context 'when work item validation fails' do
let(:opts) { { title: '' } }
it 'returns validation errors' do
expect(update_work_item[:message]).to contain_exactly("Title can't be blank")
end
it 'does not execute after-update widgets', :aggregate_failures do
expect(service).to receive(:update).and_call_original
expect(service).not_to receive(:execute_widgets).with(callback: :update, widget_params: widget_params)
expect { update_work_item }.not_to change(work_item, :description)
end
end
end
context 'for start and due date widget' do
let(:updated_date) { 1.week.from_now.to_date }
context 'when due_date is updated' do
let(:widget_params) { { start_and_due_date_widget: { due_date: updated_date } } }
it_behaves_like 'update service that triggers graphql dates updated subscription'
end
context 'when start_date is updated' do
let(:widget_params) { { start_and_due_date_widget: { start_date: updated_date } } }
it_behaves_like 'update service that triggers graphql dates updated subscription'
end
context 'when no date param is updated' do
let(:opts) { { title: 'should not trigger' } }
it 'does not trigger date updated subscription' do
expect(GraphqlTriggers).not_to receive(:issuable_dates_updated)
update_work_item
end
end
2022-07-23 23:45:48 +05:30
end
2022-08-13 15:12:31 +05:30
context 'for the hierarchy widget' do
let(:opts) { { title: 'changed' } }
let_it_be(:child_work_item) { create(:work_item, :task, project: project) }
let(:widget_params) { { hierarchy_widget: { children: [child_work_item] } } }
it 'updates the children of the work item' do
expect do
update_work_item
work_item.reload
end.to change(WorkItems::ParentLink, :count).by(1)
expect(work_item.work_item_children).to include(child_work_item)
end
context 'when child type is invalid' do
let_it_be(:child_work_item) { create(:work_item, project: project) }
it 'returns error status' do
expect(subject[:status]).to be(:error)
expect(subject[:message])
2023-03-04 22:38:38 +05:30
.to match("#{child_work_item.to_reference} cannot be added: is not allowed to add this type of parent")
2022-08-13 15:12:31 +05:30
end
it 'does not update work item attributes' do
expect do
update_work_item
work_item.reload
end.to not_change(WorkItems::ParentLink, :count).and(not_change(work_item, :title))
end
end
2022-08-27 11:52:29 +05:30
context 'when work item validation fails' do
let(:opts) { { title: '' } }
it 'returns validation errors' do
expect(update_work_item[:message]).to contain_exactly("Title can't be blank")
end
it 'does not execute after-update widgets', :aggregate_failures do
expect(service).to receive(:update).and_call_original
expect(service).not_to receive(:execute_widgets).with(callback: :before_update_in_transaction, widget_params: widget_params)
expect(work_item.work_item_children).not_to include(child_work_item)
update_work_item
end
end
2022-08-13 15:12:31 +05:30
end
2023-01-13 00:05:48 +05:30
context 'for milestone widget' do
let_it_be(:milestone) { create(:milestone, project: project) }
let(:widget_params) { { milestone_widget: { milestone_id: milestone.id } } }
context 'when milestone is updated' do
it "triggers 'issuableMilestoneUpdated'" do
expect(work_item.milestone).to eq(nil)
expect(GraphqlTriggers).to receive(:issuable_milestone_updated).with(work_item).and_call_original
update_work_item
end
end
context 'when milestone remains unchanged' do
before do
update_work_item
end
it "does not trigger 'issuableMilestoneUpdated'" do
expect(work_item.milestone).to eq(milestone)
expect(GraphqlTriggers).not_to receive(:issuable_milestone_updated)
update_work_item
end
end
end
2022-07-23 23:45:48 +05:30
end
2022-11-25 23:54:43 +05:30
describe 'label updates' do
let_it_be(:label1) { create(:label, project: project) }
let_it_be(:label2) { create(:label, project: project) }
context 'when labels are changed' do
let(:label) { create(:label, project: project) }
let(:opts) { { label_ids: [label1.id] } }
it 'tracks users updating work item labels' do
expect(Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter).to receive(:track_work_item_labels_changed_action).with(author: current_user)
update_work_item
end
it_behaves_like 'broadcasting issuable labels updates' do
let(:issuable) { work_item }
let(:label_a) { label1 }
let(:label_b) { label2 }
def update_issuable(update_params)
described_class.new(
2023-04-23 21:23:45 +05:30
container: project,
2022-11-25 23:54:43 +05:30
current_user: current_user,
params: update_params,
spam_params: spam_params,
widget_params: widget_params
).execute(work_item)
end
end
end
context 'when labels are not changed' do
shared_examples 'work item update that does not track label updates' do
it 'does not track users updating work item labels' do
expect(Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter).not_to receive(:track_work_item_labels_changed_action)
update_work_item
end
end
context 'when labels param is not provided' do
let(:opts) { { title: 'not updating labels' } }
it_behaves_like 'work item update that does not track label updates'
end
context 'when labels param is provided but labels remain unchanged' do
let(:opts) { { label_ids: [] } }
it_behaves_like 'work item update that does not track label updates'
end
context 'when labels param is provided invalid values' do
let(:opts) { { label_ids: [non_existing_record_id] } }
it_behaves_like 'work item update that does not track label updates'
end
end
end
2022-04-04 11:22:00 +05:30
end
end