2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe MergeRequests::RequestReviewService, feature_category: :code_review_workflow do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:merge_request) { create(:merge_request, reviewers: [user]) }
|
|
|
|
let(:reviewer) { merge_request.find_reviewer(user) }
|
|
|
|
let(:project) { merge_request.project }
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:service) { described_class.new(project: project, current_user: current_user) }
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:result) { service.execute(merge_request, user) }
|
|
|
|
let(:todo_service) { spy('todo service') }
|
|
|
|
let(:notification_service) { spy('notification service') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(NotificationService).to receive(:new) { notification_service }
|
|
|
|
allow(service).to receive(:todo_service).and_return(todo_service)
|
|
|
|
allow(service).to receive(:notification_service).and_return(notification_service)
|
|
|
|
|
|
|
|
reviewer.update!(state: MergeRequestReviewer.states[:reviewed])
|
|
|
|
|
|
|
|
project.add_developer(current_user)
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2022-11-25 23:54:43 +05:30
|
|
|
shared_examples_for 'failed service execution' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'returns an error' do
|
|
|
|
expect(result[:status]).to eq :error
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
it_behaves_like 'does not trigger GraphQL subscription mergeRequestReviewersUpdated' do
|
|
|
|
let(:action) { result }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'invalid permissions' do
|
|
|
|
let(:service) { described_class.new(project: project, current_user: create(:user)) }
|
|
|
|
|
|
|
|
it_behaves_like 'failed service execution'
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'reviewer does not exist' do
|
|
|
|
let(:result) { service.execute(merge_request, create(:user)) }
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it_behaves_like 'failed service execution'
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'reviewer exists' do
|
|
|
|
it 'returns success' do
|
|
|
|
expect(result[:status]).to eq :success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates reviewers state' do
|
|
|
|
service.execute(merge_request, user)
|
|
|
|
reviewer.reload
|
|
|
|
|
|
|
|
expect(reviewer.state).to eq 'unreviewed'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends email to reviewer' do
|
|
|
|
expect(notification_service).to receive_message_chain(:async, :review_requested_of_merge_request).with(merge_request, current_user, user)
|
|
|
|
|
|
|
|
service.execute(merge_request, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new todo for the reviewer' do
|
|
|
|
expect(todo_service).to receive(:create_request_review_todo).with(merge_request, current_user, user)
|
|
|
|
|
|
|
|
service.execute(merge_request, user)
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
it_behaves_like 'triggers GraphQL subscription mergeRequestReviewersUpdated' do
|
|
|
|
let(:action) { result }
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|