2014-04-03 11:16:05 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe MergeRequests::CloseService do
|
2014-04-03 11:16:05 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
2016-08-09 11:51:40 -04:00
|
|
|
let(:guest) { create(:user) }
|
2017-08-04 13:14:04 -04:00
|
|
|
let(:merge_request) { create(:merge_request, assignee: user2, author: create(:user)) }
|
2014-04-03 11:16:05 -04:00
|
|
|
let(:project) { merge_request.project }
|
2016-02-20 08:59:59 -05:00
|
|
|
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: merge_request, author: user2) }
|
2014-04-03 11:16:05 -04:00
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user2)
|
|
|
|
project.add_guest(guest)
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#execute' do
|
2017-05-26 09:29:42 -04:00
|
|
|
it_behaves_like 'cache counters invalidator'
|
|
|
|
|
2015-01-17 19:34:34 -05:00
|
|
|
context 'valid params' do
|
2016-08-09 11:51:40 -04:00
|
|
|
let(:service) { described_class.new(project, user, {}) }
|
2015-01-17 19:34:34 -05:00
|
|
|
|
2014-04-03 11:16:05 -04:00
|
|
|
before do
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(service).to receive(:execute_hooks)
|
2015-01-17 19:34:34 -05:00
|
|
|
|
2015-11-30 11:03:07 -05:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
@merge_request = service.execute(merge_request)
|
|
|
|
end
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@merge_request).to be_valid }
|
|
|
|
it { expect(@merge_request).to be_closed }
|
2014-04-03 11:16:05 -04:00
|
|
|
|
2016-08-11 11:30:18 -04:00
|
|
|
it 'executes hooks with close action' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(service).to have_received(:execute_hooks)
|
|
|
|
.with(@merge_request, 'close')
|
2015-01-17 19:34:34 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'sends email to user2 about assign of new merge_request' do
|
2014-04-03 11:16:05 -04:00
|
|
|
email = ActionMailer::Base.deliveries.last
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(email.to.first).to eq(user2.email)
|
|
|
|
expect(email.subject).to include(merge_request.title)
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'creates system note about merge_request reassign' do
|
2014-04-03 11:16:05 -04:00
|
|
|
note = @merge_request.notes.last
|
2016-11-23 01:55:23 -05:00
|
|
|
expect(note.note).to include 'closed'
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
2016-02-16 18:47:33 -05:00
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
it 'marks todos as done' do
|
|
|
|
expect(todo.reload).to be_done
|
2016-02-18 08:37:35 -05:00
|
|
|
end
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
2016-08-09 11:51:40 -04:00
|
|
|
|
2017-12-07 12:41:30 -05:00
|
|
|
it 'updates metrics' do
|
|
|
|
metrics = merge_request.metrics
|
|
|
|
metrics_service = double(MergeRequestMetricsService)
|
|
|
|
allow(MergeRequestMetricsService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(metrics)
|
|
|
|
.and_return(metrics_service)
|
|
|
|
|
|
|
|
expect(metrics_service).to receive(:close)
|
|
|
|
|
|
|
|
described_class.new(project, user, {}).execute(merge_request)
|
|
|
|
end
|
|
|
|
|
2017-09-19 07:55:56 -04:00
|
|
|
it 'refreshes the number of open merge requests for a valid MR', :use_clean_rails_memory_store_caching do
|
2017-08-17 11:21:25 -04:00
|
|
|
service = described_class.new(project, user, {})
|
|
|
|
|
|
|
|
expect { service.execute(merge_request) }
|
|
|
|
.to change { project.open_merge_requests_count }.from(1).to(0)
|
|
|
|
end
|
|
|
|
|
2016-08-09 11:51:40 -04:00
|
|
|
context 'current user is not authorized to close merge request' do
|
|
|
|
before do
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
@merge_request = described_class.new(project, guest).execute(merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not close the merge request' do
|
|
|
|
expect(@merge_request).to be_open
|
|
|
|
end
|
|
|
|
end
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
end
|