1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MergeRequests::ApprovalService do
describe '#execute' do
let(:user) { create(:user) }
let(:merge_request) { create(:merge_request, reviewers: [user]) }
let(:project) { merge_request.project }
let!(:todo) { create(:todo, user: user, project: project, target: merge_request) }
subject(:service) { described_class.new(project: project, current_user: user) }
before do
project.add_developer(user)
end
context 'with invalid approval' do
before do
allow(merge_request.approvals).to receive(:new).and_return(double(save: false))
end
it 'does not reset approvals' do
expect(merge_request.approvals).not_to receive(:reset)
service.execute(merge_request)
end
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
it 'does not publish MergeRequests::ApprovedEvent' do
expect { service.execute(merge_request) }.not_to publish_event(MergeRequests::ApprovedEvent)
end
context 'async_after_approval feature flag is disabled' do
before do
stub_feature_flags(async_after_approval: false)
end
it 'does not create approve MR event' do
expect(EventCreateService).not_to receive(:new)
service.execute(merge_request)
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
end
end
context 'with valid approval' do
it 'resets approvals' do
expect(merge_request.approvals).to receive(:reset)
service.execute(merge_request)
end
it 'tracks merge request approve action' do
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
.to receive(:track_approve_mr_action).with(user: user, merge_request: merge_request)
service.execute(merge_request)
end
it 'publishes MergeRequests::ApprovedEvent' do
expect { service.execute(merge_request) }
.to publish_event(MergeRequests::ApprovedEvent)
.with(current_user_id: user.id, merge_request_id: merge_request.id)
end
context 'async_after_approval feature flag is disabled' do
let(:notification_service) { NotificationService.new }
before do
stub_feature_flags(async_after_approval: false)
allow(service).to receive(:notification_service).and_return(notification_service)
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
it 'creates an approval note' do
expect(SystemNoteService).to receive(:approve_mr).with(merge_request, user)
service.execute(merge_request)
end
it 'marks pending todos as done' do
service.execute(merge_request)
expect(todo.reload).to be_done
end
it 'sends a notification when approving' do
expect(notification_service).to receive_message_chain(:async, :approve_mr)
.with(merge_request, user)
service.execute(merge_request)
end
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
end
end
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
end
end
|