2019-03-30 07:15:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-01 12:38:45 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe NewMergeRequestWorker do
|
2017-08-01 12:38:45 +00:00
|
|
|
describe '#perform' do
|
|
|
|
let(:worker) { described_class.new }
|
|
|
|
|
|
|
|
context 'when a merge request not found' do
|
|
|
|
it 'does not call Services' do
|
|
|
|
expect(EventCreateService).not_to receive(:new)
|
|
|
|
expect(NotificationService).not_to receive(:new)
|
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
worker.perform(non_existing_record_id, create(:user).id)
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error' do
|
2020-04-01 15:07:45 +00:00
|
|
|
user = create(:user)
|
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
expect(Gitlab::AppLogger).to receive(:error).with("NewMergeRequestWorker: couldn't find MergeRequest with ID=#{non_existing_record_id}, skipping job")
|
2017-08-01 12:38:45 +00:00
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
worker.perform(non_existing_record_id, user.id)
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user not found' do
|
|
|
|
it 'does not call Services' do
|
|
|
|
expect(EventCreateService).not_to receive(:new)
|
|
|
|
expect(NotificationService).not_to receive(:new)
|
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
worker.perform(create(:merge_request).id, non_existing_record_id)
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error' do
|
2020-04-01 15:07:45 +00:00
|
|
|
merge_request = create(:merge_request)
|
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
expect(Gitlab::AppLogger).to receive(:error).with("NewMergeRequestWorker: couldn't find User with ID=#{non_existing_record_id}, skipping job")
|
2017-08-01 12:38:45 +00:00
|
|
|
|
2020-09-10 09:08:27 +00:00
|
|
|
worker.perform(merge_request.id, non_existing_record_id)
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-10 21:09:10 +00:00
|
|
|
context 'with a user' do
|
2017-08-01 12:38:45 +00:00
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:mentioned) { create(:user) }
|
2021-03-10 21:09:10 +00:00
|
|
|
let(:user) { nil }
|
2017-08-01 12:38:45 +00:00
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project, description: "mr for #{mentioned.to_reference}")
|
|
|
|
end
|
|
|
|
|
2021-03-10 21:09:10 +00:00
|
|
|
shared_examples 'a new merge request where the author cannot trigger notifications' do
|
|
|
|
it 'does not create a notification for the mentioned user' do
|
|
|
|
expect(Notify).not_to receive(:new_merge_request_email)
|
|
|
|
.with(mentioned.id, merge_request.id, NotificationReason::MENTIONED)
|
|
|
|
|
2021-03-16 06:09:57 +00:00
|
|
|
expect(Gitlab::AppLogger).to receive(:warn).with(message: 'Skipping sending notifications', user: user.id, klass: merge_request.class.to_s, object_id: merge_request.id)
|
2021-03-10 21:09:10 +00:00
|
|
|
|
|
|
|
worker.perform(merge_request.id, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the merge request author is blocked' do
|
|
|
|
let(:user) { create(:user, :blocked) }
|
|
|
|
|
|
|
|
it_behaves_like 'a new merge request where the author cannot trigger notifications'
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
|
2021-03-10 21:09:10 +00:00
|
|
|
context 'when the merge request author is a ghost' do
|
|
|
|
let(:user) { create(:user, :ghost) }
|
|
|
|
|
|
|
|
it_behaves_like 'a new merge request where the author cannot trigger notifications'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when everything is ok' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'creates a new event record' do
|
|
|
|
expect { worker.perform(merge_request.id, user.id) }.to change { Event.count }.from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a notification for the mentioned user' do
|
|
|
|
expect(Notify).to receive(:new_merge_request_email)
|
|
|
|
.with(mentioned.id, merge_request.id, NotificationReason::MENTIONED)
|
|
|
|
.and_return(double(deliver_later: true))
|
2017-08-01 12:38:45 +00:00
|
|
|
|
2021-03-10 21:09:10 +00:00
|
|
|
worker.perform(merge_request.id, user.id)
|
|
|
|
end
|
2017-08-01 12:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|