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

510 lines
16 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Issues::CreateService do
2021-04-29 21:17:54 +05:30
include AfterNextHelpers
2020-11-24 15:15:51 +05:30
let_it_be_with_reload(:project) { create(:project) }
let_it_be(:user) { create(:user) }
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
describe '#execute' do
2020-11-24 15:15:51 +05:30
let_it_be(:assignee) { create(:user) }
let_it_be(:milestone) { create(:milestone, project: project) }
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
let(:issue) { described_class.new(project: project, current_user: user, params: opts).execute }
2016-06-02 11:05:42 +05:30
context 'when params are valid' do
2020-11-24 15:15:51 +05:30
let_it_be(:labels) { create_pair(:label, project: project) }
2016-06-02 11:05:42 +05:30
2020-11-24 15:15:51 +05:30
before_all do
2021-09-04 01:27:46 +05:30
project.add_guest(user)
project.add_guest(assignee)
2016-06-02 11:05:42 +05:30
end
2016-04-02 18:10:28 +05:30
2016-06-02 11:05:42 +05:30
let(:opts) do
{ title: 'Awesome issue',
2016-04-02 18:10:28 +05:30
description: 'please fix',
2017-08-17 22:00:37 +05:30
assignee_ids: [assignee.id],
2016-06-02 11:05:42 +05:30
label_ids: labels.map(&:id),
2016-11-03 12:29:30 +05:30
milestone_id: milestone.id,
2020-10-04 03:57:07 +05:30
milestone: milestone,
2016-11-03 12:29:30 +05:30
due_date: Date.tomorrow }
2014-09-02 18:07:02 +05:30
end
2016-11-03 12:29:30 +05:30
it 'creates the issue with the given params' do
2020-11-24 15:15:51 +05:30
expect(Issuable::CommonSystemNotesService).to receive_message_chain(:new, :execute)
2016-11-03 12:29:30 +05:30
expect(issue).to be_persisted
expect(issue.title).to eq('Awesome issue')
2017-08-17 22:00:37 +05:30
expect(issue.assignees).to eq [assignee]
2016-11-03 12:29:30 +05:30
expect(issue.labels).to match_array labels
expect(issue.milestone).to eq milestone
expect(issue.due_date).to eq Date.tomorrow
end
2020-11-24 15:15:51 +05:30
context 'when skip_system_notes is true' do
2021-06-08 01:23:25 +05:30
let(:issue) { described_class.new(project: project, current_user: user, params: opts).execute(skip_system_notes: true) }
2020-11-24 15:15:51 +05:30
it 'does not call Issuable::CommonSystemNotesService' do
expect(Issuable::CommonSystemNotesService).not_to receive(:new)
issue
end
end
it_behaves_like 'not an incident issue'
context 'issue is incident type' do
before do
opts.merge!(issue_type: 'incident')
end
let(:current_user) { user }
let(:incident_label_attributes) { attributes_for(:label, :incident) }
subject { issue }
it_behaves_like 'incident issue'
2021-02-22 17:27:13 +05:30
it_behaves_like 'has incident label'
2020-11-24 15:15:51 +05:30
it 'does create an incident label' do
expect { subject }
.to change { Label.where(incident_label_attributes).count }.by(1)
end
context 'when invalid' do
before do
opts.merge!(title: '')
end
2021-09-04 01:27:46 +05:30
it 'does not apply an incident label prematurely' do
expect { subject }.to not_change(LabelLink, :count).and not_change(Issue, :count)
2020-11-24 15:15:51 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
it 'refreshes the number of open issues', :use_clean_rails_memory_store_caching do
expect { issue }.to change { project.open_issues_count }.from(0).to(1)
end
2021-09-04 01:27:46 +05:30
context 'when current user cannot set issue metadata in the project' do
let_it_be(:non_member) { create(:user) }
2017-08-17 22:00:37 +05:30
2021-09-04 01:27:46 +05:30
it 'filters out params that cannot be set without the :set_issue_metadata permission' do
issue = described_class.new(project: project, current_user: non_member, params: opts).execute
2016-11-03 12:29:30 +05:30
expect(issue).to be_persisted
expect(issue.title).to eq('Awesome issue')
2017-08-17 22:00:37 +05:30
expect(issue.description).to eq('please fix')
expect(issue.assignees).to be_empty
2016-11-03 12:29:30 +05:30
expect(issue.labels).to be_empty
expect(issue.milestone).to be_nil
expect(issue.due_date).to be_nil
end
2020-10-04 03:57:07 +05:30
2021-09-04 01:27:46 +05:30
it 'can create confidential issues' do
issue = described_class.new(project: project, current_user: non_member, params: { confidential: true }).execute
2020-10-04 03:57:07 +05:30
expect(issue.confidential).to be_truthy
end
2016-11-03 12:29:30 +05:30
end
2016-04-02 18:10:28 +05:30
2020-11-24 15:15:51 +05:30
it 'moves the issue to the end, in an asynchronous worker' do
expect(IssuePlacementWorker).to receive(:perform_async).with(be_nil, Integer)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2020-11-24 15:15:51 +05:30
end
2016-11-03 12:29:30 +05:30
context 'when label belongs to project group' do
let(:group) { create(:group) }
let(:group_labels) { create_pair(:group_label, group: group) }
let(:opts) do
{
title: 'Title',
description: 'Description',
label_ids: group_labels.map(&:id)
}
end
before do
2020-11-24 15:15:51 +05:30
project.update!(group: group)
2016-11-03 12:29:30 +05:30
end
it 'assigns group labels' do
expect(issue.labels).to match_array group_labels
end
end
2016-06-02 11:05:42 +05:30
context 'when label belongs to different project' do
let(:label) { create(:label) }
let(:opts) do
{ title: 'Title',
description: 'Description',
label_ids: [label.id] }
end
it 'does not assign label' do
expect(issue.labels).not_to include label
2016-06-02 11:05:42 +05:30
end
end
2020-05-24 23:13:21 +05:30
context 'when labels is nil' do
let(:opts) do
{ title: 'Title',
description: 'Description',
labels: nil }
end
it 'does not assign label' do
expect(issue.labels).to be_empty
end
end
context 'when labels is nil and label_ids is present' do
let(:opts) do
{ title: 'Title',
description: 'Description',
labels: nil,
label_ids: labels.map(&:id) }
end
it 'assigns group labels' do
expect(issue.labels).to match_array labels
end
end
2016-06-02 11:05:42 +05:30
context 'when milestone belongs to different project' do
let(:milestone) { create(:milestone) }
let(:opts) do
{ title: 'Title',
description: 'Description',
milestone_id: milestone.id }
end
it 'does not assign milestone' do
expect(issue.milestone).not_to eq milestone
2016-06-02 11:05:42 +05:30
end
end
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
context 'when assignee is set' do
let(:opts) do
{ title: 'Title',
description: 'Description',
assignees: [assignee] }
end
it 'invalidates open issues counter for assignees when issue is assigned' do
2018-11-18 11:00:15 +05:30
project.add_maintainer(assignee)
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(assignee.assigned_open_issues_count).to eq 1
end
end
2019-07-07 11:18:12 +05:30
context 'when duplicate label titles are given' do
let(:label) { create(:label, project: project) }
let(:opts) do
{ title: 'Title',
description: 'Description',
labels: [label.title, label.title] }
end
it 'assigns the label once' do
expect(issue.labels).to contain_exactly(label)
end
end
2021-08-04 16:29:09 +05:30
context 'when sentry identifier is given' do
before do
sentry_attributes = { sentry_issue_attributes: { sentry_issue_identifier: 42 } }
opts.merge!(sentry_attributes)
end
2021-09-04 01:27:46 +05:30
it 'does not assign the sentry error' do
expect(issue.sentry_issue).to eq(nil)
end
context 'user is reporter or above' do
2021-08-04 16:29:09 +05:30
before do
2021-09-04 01:27:46 +05:30
project.add_reporter(user)
2021-08-04 16:29:09 +05:30
end
2021-09-04 01:27:46 +05:30
it 'assigns the sentry error' do
expect(issue.sentry_issue).to be_kind_of(SentryIssue)
2021-08-04 16:29:09 +05:30
end
end
end
2016-09-29 09:46:39 +05:30
it 'executes issue hooks when issue is not confidential' do
opts = { title: 'Title', description: 'Description', confidential: false }
expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks)
expect(project).to receive(:execute_services).with(an_instance_of(Hash), :issue_hooks)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2016-09-29 09:46:39 +05:30
end
it 'executes confidential issue hooks when issue is confidential' do
opts = { title: 'Title', description: 'Description', confidential: true }
expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :confidential_issue_hooks)
expect(project).to receive(:execute_services).with(an_instance_of(Hash), :confidential_issue_hooks)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2016-09-29 09:46:39 +05:30
end
2020-03-13 15:44:24 +05:30
context 'after_save callback to store_mentions' do
context 'when mentionable attributes change' do
let(:opts) { { title: 'Title', description: "Description with #{user.to_reference}" } }
it 'saves mentions' do
expect_next_instance_of(Issue) do |instance|
expect(instance).to receive(:store_mentions!).and_call_original
end
expect(issue.user_mentions.count).to eq 1
end
end
context 'when save fails' do
let(:opts) { { title: '', label_ids: labels.map(&:id), milestone_id: milestone.id } }
it 'does not call store_mentions' do
expect_next_instance_of(Issue) do |instance|
expect(instance).not_to receive(:store_mentions!).and_call_original
end
expect(issue.valid?).to be false
expect(issue.user_mentions.count).to eq 0
end
end
end
2020-04-08 14:13:33 +05:30
2021-04-17 20:07:23 +05:30
it 'schedules a namespace onboarding create action worker' do
expect(Namespaces::OnboardingIssueCreatedWorker).to receive(:perform_async).with(project.namespace.id)
issue
end
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
context 'issue create service' do
context 'assignees' do
2020-11-24 15:15:51 +05:30
before_all do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'removes assignee when user id is invalid' do
opts = { title: 'Title', description: 'Description', assignee_ids: [-1] }
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.assignees).to be_empty
end
it 'removes assignee when user id is 0' do
2019-03-02 22:35:43 +05:30
opts = { title: 'Title', description: 'Description', assignee_ids: [0] }
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.assignees).to be_empty
end
it 'saves assignee when user id is valid' do
2018-11-18 11:00:15 +05:30
project.add_maintainer(assignee)
2017-08-17 22:00:37 +05:30
opts = { title: 'Title', description: 'Description', assignee_ids: [assignee.id] }
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.assignees).to eq([assignee])
end
context "when issuable feature is private" do
before do
2020-11-24 15:15:51 +05:30
project.project_feature.update!(issues_access_level: ProjectFeature::PRIVATE,
2017-08-17 22:00:37 +05:30
merge_requests_access_level: ProjectFeature::PRIVATE)
end
levels = [Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC]
levels.each do |level|
it "removes not authorized assignee when project is #{Gitlab::VisibilityLevel.level_name(level)}" do
2020-11-24 15:15:51 +05:30
project.update!(visibility_level: level)
2017-08-17 22:00:37 +05:30
opts = { title: 'Title', description: 'Description', assignee_ids: [assignee.id] }
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.assignees).to be_empty
end
end
end
end
end
2020-07-28 23:09:34 +05:30
it_behaves_like 'issuable record that supports quick actions' do
2021-06-08 01:23:25 +05:30
let(:issuable) { described_class.new(project: project, current_user: user, params: params).execute }
2020-07-28 23:09:34 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'Quick actions' do
2017-08-17 22:00:37 +05:30
context 'with assignee and milestone in params and command' do
let(:opts) do
{
assignee_ids: [create(:user).id],
milestone_id: 1,
title: 'Title',
description: %(/assign @#{assignee.username}\n/milestone %"#{milestone.name}")
}
end
2020-11-24 15:15:51 +05:30
before_all do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
project.add_maintainer(assignee)
2017-08-17 22:00:37 +05:30
end
it 'assigns and sets milestone to issuable from command' do
expect(issue).to be_persisted
expect(issue.assignees).to eq([assignee])
expect(issue.milestone).to eq(milestone)
end
end
end
context 'resolving discussions' do
2020-11-24 15:15:51 +05:30
let_it_be(:discussion) { create(:diff_note_on_merge_request).to_discussion }
let_it_be(:merge_request) { discussion.noteable }
let_it_be(:project) { merge_request.source_project }
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
before_all do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-08-17 22:00:37 +05:30
end
describe 'for a single discussion' do
let(:opts) { { discussion_to_resolve: discussion.id, merge_request_to_resolve_discussions_of: merge_request.iid } }
it 'resolves the discussion' do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
discussion.first_note.reload
expect(discussion.resolved?).to be(true)
end
it 'added a system note to the discussion' do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
reloaded_discussion = MergeRequest.find(merge_request.id).discussions.first
expect(reloaded_discussion.last_note.system).to eq(true)
end
it 'assigns the title and description for the issue' do
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.title).not_to be_nil
expect(issue.description).not_to be_nil
end
it 'can set nil explicitly to the title and description' do
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user,
params: {
merge_request_to_resolve_discussions_of: merge_request,
description: nil,
title: nil
}).execute
2017-08-17 22:00:37 +05:30
expect(issue.description).to be_nil
expect(issue.title).to be_nil
end
end
describe 'for a merge request' do
let(:opts) { { merge_request_to_resolve_discussions_of: merge_request.iid } }
it 'resolves the discussion' do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
discussion.first_note.reload
expect(discussion.resolved?).to be(true)
end
it 'added a system note to the discussion' do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
reloaded_discussion = MergeRequest.find(merge_request.id).discussions.first
expect(reloaded_discussion.last_note.system).to eq(true)
end
it 'assigns the title and description for the issue' do
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(issue.title).not_to be_nil
expect(issue.description).not_to be_nil
end
it 'can set nil explicitly to the title and description' do
2021-06-08 01:23:25 +05:30
issue = described_class.new(project: project, current_user: user,
params: {
merge_request_to_resolve_discussions_of: merge_request,
description: nil,
title: nil
}).execute
2017-08-17 22:00:37 +05:30
expect(issue.description).to be_nil
expect(issue.title).to be_nil
end
end
end
context 'checking spam' do
2021-04-29 21:17:54 +05:30
let(:request) { double(:request, headers: nil) }
2021-03-11 19:13:27 +05:30
let(:api) { true }
let(:captcha_response) { 'abc123' }
let(:spam_log_id) { 1 }
2020-05-24 23:13:21 +05:30
2021-03-11 19:13:27 +05:30
let(:params) do
2017-08-17 22:00:37 +05:30
{
2021-03-11 19:13:27 +05:30
title: 'Spam issue',
request: request,
api: api,
captcha_response: captcha_response,
spam_log_id: spam_log_id
2017-08-17 22:00:37 +05:30
}
end
2021-03-11 19:13:27 +05:30
subject do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: params)
2017-08-17 22:00:37 +05:30
end
2021-03-11 19:13:27 +05:30
before do
allow_next_instance_of(UserAgentDetailService) do |instance|
allow(instance).to receive(:create)
2017-08-17 22:00:37 +05:30
end
end
2021-03-11 19:13:27 +05:30
it 'executes SpamActionService' do
spam_params = Spam::SpamParams.new(
api: api,
captcha_response: captcha_response,
spam_log_id: spam_log_id
)
expect_next_instance_of(
Spam::SpamActionService,
{
spammable: an_instance_of(Issue),
request: request,
user: user,
action: :create
}
) do |instance|
expect(instance).to receive(:execute).with(spam_params: spam_params)
2017-08-17 22:00:37 +05:30
end
2021-03-11 19:13:27 +05:30
subject.execute
2017-08-17 22:00:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
end