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

124 lines
3.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Issues::CloseService do
2014-09-02 18:07:02 +05:30
let(:user) { create(:user) }
let(:user2) { create(:user) }
2016-09-13 17:45:13 +05:30
let(:guest) { create(:user) }
2018-03-17 18:26:18 +05:30
let(:issue) { create(:issue, assignees: [user2], author: create(:user)) }
2015-09-11 14:41:01 +05:30
let(:project) { issue.project }
2016-04-02 18:10:28 +05:30
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }
2014-09-02 18:07:02 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_master(user)
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
2017-08-17 22:00:37 +05:30
let(:service) { described_class.new(project, user) }
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
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)
.with(issue, commit: 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
expect { service.execute(issue) }
.to change { project.open_issues_count }.from(1).to(0)
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
end
describe '#close_issue' do
2014-09-02 18:07:02 +05:30
context "valid params" do
before do
2015-12-23 02:04:40 +05:30
perform_enqueued_jobs do
2017-08-17 22:00:37 +05:30
described_class.new(project, user).close_issue(issue)
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
it 'closes the issue' do
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
expect(issue.closed_by_id).to be(user.id)
end
2016-09-13 17:45:13 +05:30
it 'sends email to user2 about assign of new issue' do
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
2016-09-13 17:45:13 +05:30
it 'creates system note about issue reassign' do
2016-09-29 09:46:39 +05:30
note = issue.notes.last
2017-08-17 22:00:37 +05:30
expect(note.note).to include "closed"
2014-09-02 18:07:02 +05:30
end
2016-04-02 18:10:28 +05:30
it 'marks todos as done' do
expect(todo.reload).to be_done
end
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)
expect(project).to receive(:execute_services).with(an_instance_of(Hash), :issue_hooks)
2017-08-17 22:00:37 +05:30
described_class.new(project, user).close_issue(issue)
2016-09-29 09:46:39 +05:30
end
end
context 'when issue is confidential' do
it 'executes confidential issue hooks' do
issue = create(:issue, :confidential, project: project)
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)
2017-08-17 22:00:37 +05:30
described_class.new(project, 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