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

360 lines
12 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::CloseService do
2021-09-04 01:27:46 +05:30
let(:project) { create(:project, :repository) }
let(:user) { create(:user, email: "user@example.com") }
let(:user2) { create(:user, email: "user2@example.com") }
let(:guest) { create(:user) }
let(:issue) { create(:issue, title: "My issue", project: project, assignees: [user2], author: create(:user)) }
2019-12-21 20:55:43 +05:30
let(:external_issue) { ExternalIssue.new('JIRA-123', project) }
2021-09-04 01:27:46 +05:30
let(:closing_merge_request) { create(:merge_request, source_project: project) }
let(:closing_commit) { create(:commit, project: project) }
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }
2014-09-02 18:07:02 +05:30
2021-09-04 01:27:46 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
project.add_developer(user2)
project.add_guest(guest)
2014-09-02 18:07:02 +05:30
end
2016-08-24 12:49:21 +05:30
describe '#execute' do
2021-06-08 01:23:25 +05:30
let(:service) { described_class.new(project: project, current_user: user) }
2017-08-17 22:00:37 +05:30
it 'checks if the user is authorized to update the issue' do
2017-09-10 17:25:29 +05:30
expect(service).to receive(:can?).with(user, :update_issue, issue)
.and_call_original
2017-08-17 22:00:37 +05:30
service.execute(issue)
end
it 'does not close the issue when the user is not authorized to do so' do
2017-09-10 17:25:29 +05:30
allow(service).to receive(:can?).with(user, :update_issue, issue)
.and_return(false)
2017-08-17 22:00:37 +05:30
expect(service).not_to receive(:close_issue)
expect(service.execute(issue)).to eq(issue)
end
2019-12-21 20:55:43 +05:30
it 'closes the external issue even when the user is not authorized to do so' do
allow(service).to receive(:can?).with(user, :update_issue, external_issue)
.and_return(false)
expect(service).to receive(:close_issue)
.with(external_issue, closed_via: nil, notifications: true, system_note: true)
service.execute(external_issue)
end
2017-08-17 22:00:37 +05:30
it 'closes the issue when the user is authorized to do so' do
2017-09-10 17:25:29 +05:30
allow(service).to receive(:can?).with(user, :update_issue, issue)
.and_return(true)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(service).to receive(:close_issue)
2019-09-04 21:01:54 +05:30
.with(issue, closed_via: nil, notifications: true, system_note: true)
2017-08-17 22:00:37 +05:30
service.execute(issue)
end
2018-03-17 18:26:18 +05:30
it 'refreshes the number of open issues', :use_clean_rails_memory_store_caching do
2021-11-11 11:23:49 +05:30
expect do
service.execute(issue)
BatchLoader::Executor.clear_current
end.to change { project.open_issues_count }.from(1).to(0)
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
it 'invalidates counter cache for assignees' do
expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
service.execute(issue)
end
2020-11-24 15:15:51 +05:30
context 'issue is incident type' do
let(:issue) { create(:incident, project: project) }
let(:current_user) { user }
subject { service.execute(issue) }
it_behaves_like 'an incident management tracked event', :incident_management_incident_closed
end
2017-08-17 22:00:37 +05:30
end
describe '#close_issue' do
2020-04-08 14:13:33 +05:30
context 'with external issue' do
context 'with an active external issue tracker supporting close_issue' do
2021-09-30 23:02:18 +05:30
let!(:external_issue_tracker) { create(:jira_integration, project: project) }
2020-04-08 14:13:33 +05:30
it 'closes the issue on the external issue tracker' do
2021-03-11 19:13:27 +05:30
project.reload
2020-04-08 14:13:33 +05:30
expect(project.external_issue_tracker).to receive(:close_issue)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(external_issue)
2020-04-08 14:13:33 +05:30
end
end
2021-06-08 01:23:25 +05:30
context 'with inactive external issue tracker supporting close_issue' do
2021-09-30 23:02:18 +05:30
let!(:external_issue_tracker) { create(:jira_integration, project: project, active: false) }
2020-04-08 14:13:33 +05:30
it 'does not close the issue on the external issue tracker' do
2021-03-11 19:13:27 +05:30
project.reload
2020-04-08 14:13:33 +05:30
expect(project.external_issue_tracker).not_to receive(:close_issue)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(external_issue)
2020-04-08 14:13:33 +05:30
end
end
context 'with an active external issue tracker not supporting close_issue' do
2021-09-04 01:27:46 +05:30
let!(:external_issue_tracker) { create(:bugzilla_integration, project: project) }
2020-04-08 14:13:33 +05:30
it 'does not close the issue on the external issue tracker' do
2021-03-11 19:13:27 +05:30
project.reload
2020-04-08 14:13:33 +05:30
expect(project.external_issue_tracker).not_to receive(:close_issue)
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(external_issue)
2020-04-08 14:13:33 +05:30
end
end
end
2019-12-26 22:10:19 +05:30
context "closed by a merge request", :sidekiq_might_not_need_inline do
2021-03-08 18:12:59 +05:30
subject(:close_issue) do
2019-09-04 21:01:54 +05:30
perform_enqueued_jobs do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(issue, closed_via: closing_merge_request)
2019-09-04 21:01:54 +05:30
end
2021-03-08 18:12:59 +05:30
end
it 'mentions closure via a merge request' do
close_issue
2019-09-04 21:01:54 +05:30
email = ActionMailer::Base.deliveries.last
expect(email.to.first).to eq(user2.email)
expect(email.subject).to include(issue.title)
expect(email.body.parts.map(&:body)).to all(include(closing_merge_request.to_reference))
end
2021-03-08 18:12:59 +05:30
it_behaves_like 'records an onboarding progress action', :issue_auto_closed do
let(:namespace) { project.namespace }
end
2019-09-04 21:01:54 +05:30
context 'when user cannot read merge request' do
it 'does not mention merge request' do
project.project_feature.update_attribute(:repository_access_level, ProjectFeature::DISABLED)
2021-03-08 18:12:59 +05:30
close_issue
2019-09-04 21:01:54 +05:30
email = ActionMailer::Base.deliveries.last
body_text = email.body.parts.map(&:body).join(" ")
expect(email.to.first).to eq(user2.email)
expect(email.subject).to include(issue.title)
expect(body_text).not_to include(closing_merge_request.to_reference)
end
end
2020-04-08 14:13:33 +05:30
context 'updating `metrics.first_mentioned_in_commit_at`' do
context 'when `metrics.first_mentioned_in_commit_at` is not set' do
it 'uses the first commit authored timestamp' do
expected = closing_merge_request.commits.first.authored_date
2021-03-08 18:12:59 +05:30
close_issue
2020-04-08 14:13:33 +05:30
expect(issue.metrics.first_mentioned_in_commit_at).to eq(expected)
end
end
context 'when `metrics.first_mentioned_in_commit_at` is already set' do
before do
2020-05-24 23:13:21 +05:30
issue.metrics.update!(first_mentioned_in_commit_at: Time.current)
2020-04-08 14:13:33 +05:30
end
it 'does not update the metrics' do
2021-03-08 18:12:59 +05:30
expect { close_issue }.not_to change { issue.metrics.first_mentioned_in_commit_at }
2020-04-08 14:13:33 +05:30
end
end
context 'when merge request has no commits' do
let(:closing_merge_request) { create(:merge_request, :without_diffs, source_project: project) }
it 'does not update the metrics' do
2021-03-08 18:12:59 +05:30
close_issue
2020-04-08 14:13:33 +05:30
expect(issue.metrics.first_mentioned_in_commit_at).to be_nil
end
end
end
2019-09-04 21:01:54 +05:30
end
2019-12-26 22:10:19 +05:30
context "closed by a commit", :sidekiq_might_not_need_inline do
2021-09-04 01:27:46 +05:30
it 'mentions closure via a commit' do
2019-09-04 21:01:54 +05:30
perform_enqueued_jobs do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(issue, closed_via: closing_commit)
2019-09-04 21:01:54 +05:30
end
email = ActionMailer::Base.deliveries.last
expect(email.to.first).to eq(user2.email)
expect(email.subject).to include(issue.title)
expect(email.body.parts.map(&:body)).to all(include(closing_commit.id))
end
context 'when user cannot read the commit' do
it 'does not mention the commit id' do
project.project_feature.update_attribute(:repository_access_level, ProjectFeature::DISABLED)
2021-09-04 01:27:46 +05:30
perform_enqueued_jobs do
described_class.new(project: project, current_user: user).close_issue(issue, closed_via: closing_commit)
end
2019-09-04 21:01:54 +05:30
email = ActionMailer::Base.deliveries.last
body_text = email.body.parts.map(&:body).join(" ")
expect(email.to.first).to eq(user2.email)
expect(email.subject).to include(issue.title)
expect(body_text).not_to include(closing_commit.id)
end
end
end
2014-09-02 18:07:02 +05:30
context "valid params" do
2021-03-08 18:12:59 +05:30
subject(:close_issue) do
2015-12-23 02:04:40 +05:30
perform_enqueued_jobs do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user).close_issue(issue)
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
end
2021-06-08 01:23:25 +05:30
it 'verifies the number of queries' do
recorded = ActiveRecord::QueryRecorder.new { close_issue }
2021-11-11 11:23:49 +05:30
expected_queries = 27
2021-06-08 01:23:25 +05:30
expect(recorded.count).to be <= expected_queries
expect(recorded.cached_count).to eq(0)
end
2017-09-10 17:25:29 +05:30
it 'closes the issue' do
2020-04-08 14:13:33 +05:30
close_issue
2017-09-10 17:25:29 +05:30
expect(issue).to be_valid
expect(issue).to be_closed
end
2014-09-02 18:07:02 +05:30
2018-05-09 12:01:36 +05:30
it 'records closed user' do
2020-04-08 14:13:33 +05:30
close_issue
2021-06-08 01:23:25 +05:30
expect(issue.reload.closed_by_id).to be(user.id)
2018-05-09 12:01:36 +05:30
end
2019-12-26 22:10:19 +05:30
it 'sends email to user2 about assign of new issue', :sidekiq_might_not_need_inline do
2020-04-08 14:13:33 +05:30
close_issue
2014-09-02 18:07:02 +05:30
email = ActionMailer::Base.deliveries.last
2015-04-26 12:48:37 +05:30
expect(email.to.first).to eq(user2.email)
expect(email.subject).to include(issue.title)
2014-09-02 18:07:02 +05:30
end
2021-01-03 14:25:43 +05:30
it 'creates resource state event about the issue being closed' do
close_issue
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
event = issue.resource_state_events.last
expect(event.state).to eq('closed')
2014-09-02 18:07:02 +05:30
end
2016-04-02 18:10:28 +05:30
it 'marks todos as done' do
2020-04-08 14:13:33 +05:30
close_issue
2016-04-02 18:10:28 +05:30
expect(todo.reload).to be_done
end
2020-04-08 14:13:33 +05:30
2021-06-08 01:23:25 +05:30
context 'when closing the issue fails' do
it 'does not assign a closed_by value for the issue' do
allow(issue).to receive(:close).and_return(false)
close_issue
expect(issue.closed_by_id).to be_nil
end
end
2020-10-24 23:57:45 +05:30
context 'when there is an associated Alert Management Alert' do
context 'when alert can be resolved' do
let!(:alert) { create(:alert_management_alert, issue: issue, project: project) }
it 'resolves an alert and sends a system note' do
expect_next_instance_of(SystemNotes::AlertManagementService) do |notes_service|
expect(notes_service).to receive(:closed_alert_issue).with(issue)
end
close_issue
expect(alert.reload.resolved?).to eq(true)
end
end
context 'when alert cannot be resolved' do
let!(:alert) { create(:alert_management_alert, :with_validation_errors, issue: issue, project: project) }
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'writes a warning into the log' do
close_issue
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: issue.id,
alert_id: alert.id,
alert_errors: { hosts: ['hosts array is over 255 chars'] }
)
end
end
end
2020-04-08 14:13:33 +05:30
it 'deletes milestone issue counters cache' do
2020-11-24 15:15:51 +05:30
issue.update!(milestone: create(:milestone, project: project))
2020-04-08 14:13:33 +05:30
expect_next_instance_of(Milestones::ClosedIssuesCountService, issue.milestone) do |service|
expect(service).to receive(:delete_cache).and_call_original
end
close_issue
end
2021-03-08 18:12:59 +05:30
it_behaves_like 'does not record an onboarding progress action'
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
2016-09-29 09:46:39 +05:30
context 'when issue is not confidential' do
it 'executes issue hooks' do
expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks)
2021-09-30 23:02:18 +05:30
expect(project).to receive(:execute_integrations).with(an_instance_of(Hash), :issue_hooks)
2016-09-29 09:46:39 +05:30
2021-09-04 01:27:46 +05:30
described_class.new(project: project, current_user: user).close_issue(issue)
2016-09-29 09:46:39 +05:30
end
end
context 'when issue is confidential' do
2021-06-08 01:23:25 +05:30
it 'executes confidential issue hooks' do
2021-09-04 01:27:46 +05:30
issue = create(:issue, :confidential, project: project)
2016-09-29 09:46:39 +05:30
expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :confidential_issue_hooks)
2021-09-30 23:02:18 +05:30
expect(project).to receive(:execute_integrations).with(an_instance_of(Hash), :confidential_issue_hooks)
2016-09-29 09:46:39 +05:30
2021-09-04 01:27:46 +05:30
described_class.new(project: project, current_user: user).close_issue(issue)
2016-09-29 09:46:39 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'internal issues disabled' do
2015-09-11 14:41:01 +05:30
before do
2017-09-10 17:25:29 +05:30
project.issues_enabled = false
project.save!
2015-09-11 14:41:01 +05:30
end
2017-09-10 17:25:29 +05:30
it 'does not close the issue' do
expect(issue).to be_valid
expect(issue).to be_opened
expect(todo.reload).to be_pending
end
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
end
end