debian-mirror-gitlab/spec/workers/new_merge_request_worker_spec.rb

92 lines
3.1 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe NewMergeRequestWorker do
2017-09-10 17:25:29 +05:30
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-11-24 15:15:51 +05:30
worker.perform(non_existing_record_id, create(:user).id)
2017-09-10 17:25:29 +05:30
end
it 'logs an error' do
2020-04-22 19:07:51 +05:30
user = create(:user)
2020-11-24 15:15:51 +05:30
expect(Gitlab::AppLogger).to receive(:error).with("NewMergeRequestWorker: couldn't find MergeRequest with ID=#{non_existing_record_id}, skipping job")
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
worker.perform(non_existing_record_id, user.id)
2017-09-10 17:25:29 +05:30
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-11-24 15:15:51 +05:30
worker.perform(create(:merge_request).id, non_existing_record_id)
2017-09-10 17:25:29 +05:30
end
it 'logs an error' do
2020-04-22 19:07:51 +05:30
merge_request = create(:merge_request)
2020-11-24 15:15:51 +05:30
expect(Gitlab::AppLogger).to receive(:error).with("NewMergeRequestWorker: couldn't find User with ID=#{non_existing_record_id}, skipping job")
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
worker.perform(merge_request.id, non_existing_record_id)
2017-09-10 17:25:29 +05:30
end
end
2021-04-17 20:07:23 +05:30
context 'with a user' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
let(:mentioned) { create(:user) }
2021-04-17 20:07:23 +05:30
let(:user) { nil }
2017-09-10 17:25:29 +05:30
let(:merge_request) do
create(:merge_request, source_project: project, description: "mr for #{mentioned.to_reference}")
end
2021-04-17 20:07:23 +05:30
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-04-29 21:17:54 +05:30
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-04-17 20:07:23 +05:30
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-09-10 17:25:29 +05:30
end
2021-04-17 20:07:23 +05:30
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-09-10 17:25:29 +05:30
2021-04-17 20:07:23 +05:30
worker.perform(merge_request.id, user.id)
end
2017-09-10 17:25:29 +05:30
end
end
end
end