2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-01 08:38:45 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe NewIssueWorker do
|
2021-04-08 14:09:32 -04:00
|
|
|
include AfterNextHelpers
|
|
|
|
|
2017-08-01 08:38:45 -04:00
|
|
|
describe '#perform' do
|
|
|
|
let(:worker) { described_class.new }
|
|
|
|
|
|
|
|
context 'when an issue not found' do
|
|
|
|
it 'does not call Services' do
|
|
|
|
expect(EventCreateService).not_to receive(:new)
|
|
|
|
expect(NotificationService).not_to receive(:new)
|
|
|
|
|
2020-09-10 05:08:27 -04:00
|
|
|
worker.perform(non_existing_record_id, create(:user).id)
|
2017-08-01 08:38:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error' do
|
2020-09-10 05:08:27 -04:00
|
|
|
expect(Gitlab::AppLogger).to receive(:error).with("NewIssueWorker: couldn't find Issue with ID=#{non_existing_record_id}, skipping job")
|
2017-08-01 08:38:45 -04:00
|
|
|
|
2020-09-10 05:08:27 -04:00
|
|
|
worker.perform(non_existing_record_id, create(:user).id)
|
2017-08-01 08:38:45 -04: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 05:08:27 -04:00
|
|
|
worker.perform(create(:issue).id, non_existing_record_id)
|
2017-08-01 08:38:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error' do
|
2020-04-01 11:07:45 -04:00
|
|
|
issue = create(:issue)
|
|
|
|
|
2020-09-10 05:08:27 -04:00
|
|
|
expect(Gitlab::AppLogger).to receive(:error).with("NewIssueWorker: couldn't find User with ID=#{non_existing_record_id}, skipping job")
|
2017-08-01 08:38:45 -04:00
|
|
|
|
2020-09-10 05:08:27 -04:00
|
|
|
worker.perform(issue.id, non_existing_record_id)
|
2017-08-01 08:38:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-10 16:09:10 -05:00
|
|
|
context 'with a user' do
|
2020-09-04 11:08:46 -04:00
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:mentioned) { create(:user) }
|
2021-03-10 16:09:10 -05:00
|
|
|
let_it_be(:user) { nil }
|
2020-09-04 11:08:46 -04:00
|
|
|
let_it_be(:issue) { create(:issue, project: project, description: "issue for #{mentioned.to_reference}") }
|
2017-08-01 08:38:45 -04:00
|
|
|
|
2021-03-10 16:09:10 -05:00
|
|
|
shared_examples 'a new issue where the current_user cannot trigger notifications' do
|
|
|
|
it 'does not create a notification for the mentioned user' do
|
|
|
|
expect(Notify).not_to receive(:new_issue_email)
|
|
|
|
.with(mentioned.id, issue.id, NotificationReason::MENTIONED)
|
|
|
|
|
2021-03-16 02:09:57 -04:00
|
|
|
expect(Gitlab::AppLogger).to receive(:warn).with(message: 'Skipping sending notifications', user: user.id, klass: issue.class.to_s, object_id: issue.id)
|
2021-03-10 16:09:10 -05:00
|
|
|
|
|
|
|
worker.perform(issue.id, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the new issue author is blocked' do
|
|
|
|
let_it_be(:user) { create_default(:user, :blocked) }
|
|
|
|
|
|
|
|
it_behaves_like 'a new issue where the current_user cannot trigger notifications'
|
2017-08-01 08:38:45 -04:00
|
|
|
end
|
|
|
|
|
2021-03-10 16:09:10 -05:00
|
|
|
context 'when the new issue author is a ghost' do
|
|
|
|
let_it_be(:user) { create_default(:user, :ghost) }
|
|
|
|
|
|
|
|
it_behaves_like 'a new issue where the current_user cannot trigger notifications'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when everything is ok' do
|
|
|
|
let_it_be(:user) { create_default(:user) }
|
|
|
|
|
|
|
|
it 'creates a new event record' do
|
|
|
|
expect { worker.perform(issue.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_issue_email).with(mentioned.id, issue.id, NotificationReason::MENTIONED)
|
|
|
|
.and_return(double(deliver_later: true))
|
2017-08-01 08:38:45 -04:00
|
|
|
|
2021-03-10 16:09:10 -05:00
|
|
|
worker.perform(issue.id, user.id)
|
|
|
|
end
|
2021-04-08 14:09:32 -04:00
|
|
|
|
2021-04-13 20:10:39 -04:00
|
|
|
it 'calls Issues::AfterCreateService' do
|
|
|
|
expect_next(::Issues::AfterCreateService)
|
|
|
|
.to receive(:execute)
|
2021-04-08 14:09:32 -04:00
|
|
|
|
2021-04-13 20:10:39 -04:00
|
|
|
worker.perform(issue.id, user.id)
|
2021-04-08 14:09:32 -04:00
|
|
|
end
|
2017-08-01 08:38:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|