2020-07-03 05:08:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe MergeRequests::ApprovalService do
|
|
|
|
describe '#execute' do
|
|
|
|
let(:user) { create(:user) }
|
2021-12-08 07:13:04 -05:00
|
|
|
let(:merge_request) { create(:merge_request, reviewers: [user]) }
|
2020-07-03 05:08:53 -04:00
|
|
|
let(:project) { merge_request.project }
|
|
|
|
let!(:todo) { create(:todo, user: user, project: project, target: merge_request) }
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
subject(:service) { described_class.new(project: project, current_user: user) }
|
2020-07-03 05:08:53 -04:00
|
|
|
|
2020-07-10 14:09:45 -04:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2020-07-03 05:08:53 -04:00
|
|
|
context 'with invalid approval' do
|
|
|
|
before do
|
|
|
|
allow(merge_request.approvals).to receive(:new).and_return(double(save: false))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create an approval note' do
|
|
|
|
expect(SystemNoteService).not_to receive(:approve_mr)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not mark pending todos as done' do
|
|
|
|
service.execute(merge_request)
|
|
|
|
|
|
|
|
expect(todo.reload).to be_pending
|
|
|
|
end
|
2021-02-08 16:09:25 -05:00
|
|
|
|
|
|
|
it 'does not track merge request approve action' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.not_to receive(:track_approve_mr_action).with(user: user)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
2020-07-03 05:08:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid approval' do
|
|
|
|
it 'creates an approval note and marks pending todos as done' do
|
|
|
|
expect(SystemNoteService).to receive(:approve_mr).with(merge_request, user)
|
|
|
|
expect(merge_request.approvals).to receive(:reset)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
|
|
|
|
expect(todo.reload).to be_done
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates approve MR event' do
|
|
|
|
expect_next_instance_of(EventCreateService) do |instance|
|
|
|
|
expect(instance).to receive(:approve_mr)
|
|
|
|
.with(merge_request, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
2021-12-08 07:13:04 -05:00
|
|
|
it 'removes attention requested state' do
|
|
|
|
expect(MergeRequests::RemoveAttentionRequestedService).to receive(:new)
|
|
|
|
.with(project: project, current_user: user, merge_request: merge_request, user: user)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
2020-07-03 05:08:53 -04:00
|
|
|
context 'with remaining approvals' do
|
|
|
|
it 'fires an approval webhook' do
|
|
|
|
expect(service).to receive(:execute_hooks).with(merge_request, 'approved')
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
end
|
2021-02-08 16:09:25 -05:00
|
|
|
|
|
|
|
it 'tracks merge request approve action' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.to receive(:track_approve_mr_action).with(user: user)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
2020-07-03 05:08:53 -04:00
|
|
|
end
|
2020-07-10 14:09:45 -04:00
|
|
|
|
|
|
|
context 'user cannot update the merge request' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update approvals' do
|
|
|
|
expect { service.execute(merge_request) }.not_to change { merge_request.approvals.size }
|
|
|
|
end
|
|
|
|
end
|
2020-07-03 05:08:53 -04:00
|
|
|
end
|
|
|
|
end
|