2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 07:14:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe NotificationRecipients::BuildService do
|
2018-06-07 07:14:27 -04:00
|
|
|
let(:service) { described_class }
|
|
|
|
let(:assignee) { create(:user) }
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:other_projects) { create_list(:project, 5, :public) }
|
|
|
|
|
|
|
|
describe '#build_new_note_recipients' do
|
|
|
|
let(:issue) { create(:issue, project: project, assignees: [assignee]) }
|
|
|
|
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) }
|
|
|
|
|
2018-10-02 10:29:45 -04:00
|
|
|
shared_examples 'no N+1 queries' do
|
2018-10-02 08:39:46 -04:00
|
|
|
it 'avoids N+1 queries', :request_store do
|
2022-05-19 08:08:42 -04:00
|
|
|
# existing N+1 due to multiple users having to be looked up in the project_authorizations table
|
2022-06-15 17:10:04 -04:00
|
|
|
threshold = project.private? ? 1 : 0
|
2022-05-19 08:08:42 -04:00
|
|
|
|
2018-10-02 10:29:45 -04:00
|
|
|
create_user
|
2018-10-02 08:39:46 -04:00
|
|
|
|
|
|
|
service.build_new_note_recipients(note)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
service.build_new_note_recipients(note)
|
|
|
|
end
|
2018-06-07 07:14:27 -04:00
|
|
|
|
2018-10-02 10:29:45 -04:00
|
|
|
create_user
|
2018-10-02 08:39:46 -04:00
|
|
|
|
2022-05-19 08:08:42 -04:00
|
|
|
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count).with_threshold(threshold)
|
2018-06-07 07:14:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when there are multiple watchers' do
|
|
|
|
def create_user
|
|
|
|
watcher = create(:user)
|
|
|
|
create(:notification_setting, source: project, user: watcher, level: :watch)
|
2022-05-19 08:08:42 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
other_projects.each do |other_project|
|
|
|
|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
|
2018-10-02 10:29:45 -04:00
|
|
|
end
|
2022-06-15 17:10:04 -04:00
|
|
|
end
|
2018-10-02 10:29:45 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
include_examples 'no N+1 queries'
|
|
|
|
end
|
2018-10-02 10:29:45 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when there are multiple subscribers' do
|
|
|
|
def create_user
|
|
|
|
subscriber = create(:user)
|
|
|
|
issue.subscriptions.create!(user: subscriber, project: project, subscribed: true)
|
|
|
|
end
|
2018-06-07 07:14:27 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
include_examples 'no N+1 queries'
|
2018-06-07 07:14:27 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when the project is private' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
|
2018-10-02 08:39:46 -04:00
|
|
|
end
|
2022-06-15 17:10:04 -04:00
|
|
|
|
|
|
|
include_examples 'no N+1 queries'
|
2018-10-02 08:39:46 -04:00
|
|
|
end
|
2018-06-07 07:14:27 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
|
|
|
|
describe '#build_new_review_recipients' do
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
|
|
|
|
let(:review) { create(:review, merge_request: merge_request, project: project, author: merge_request.author) }
|
|
|
|
let(:notes) { create_list(:note_on_merge_request, 3, review: review, noteable: review.merge_request, project: review.project) }
|
|
|
|
|
|
|
|
shared_examples 'no N+1 queries' do
|
|
|
|
it 'avoids N+1 queries', :request_store do
|
2022-05-19 08:08:42 -04:00
|
|
|
# existing N+1 due to multiple users having to be looked up in the project_authorizations table
|
2022-06-15 17:10:04 -04:00
|
|
|
threshold = project.private? ? 1 : 0
|
2022-05-19 08:08:42 -04:00
|
|
|
|
2020-05-27 14:08:14 -04:00
|
|
|
create_user
|
|
|
|
|
|
|
|
service.build_new_review_recipients(review)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
service.build_new_review_recipients(review)
|
|
|
|
end
|
|
|
|
|
|
|
|
create_user
|
|
|
|
|
2022-05-19 08:08:42 -04:00
|
|
|
expect { service.build_new_review_recipients(review) }.not_to exceed_query_limit(control_count).with_threshold(threshold)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when there are multiple watchers' do
|
|
|
|
def create_user
|
|
|
|
watcher = create(:user)
|
|
|
|
create(:notification_setting, source: project, user: watcher, level: :watch)
|
2022-05-19 08:08:42 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
other_projects.each do |other_project|
|
|
|
|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
2022-06-15 17:10:04 -04:00
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
include_examples 'no N+1 queries'
|
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when there are multiple subscribers' do
|
|
|
|
def create_user
|
|
|
|
subscriber = create(:user)
|
|
|
|
merge_request.subscriptions.create!(user: subscriber, project: project, subscribed: true)
|
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
include_examples 'no N+1 queries'
|
2020-05-27 14:08:14 -04:00
|
|
|
|
2022-06-15 17:10:04 -04:00
|
|
|
context 'when the project is private' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
2022-06-15 17:10:04 -04:00
|
|
|
|
|
|
|
include_examples 'no N+1 queries'
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-02 07:10:15 -05:00
|
|
|
|
|
|
|
describe '#build_requested_review_recipients' do
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
merge_request.reviewers.push(assignee)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'no N+1 queries' do
|
|
|
|
it 'avoids N+1 queries', :request_store do
|
|
|
|
create_user
|
|
|
|
|
|
|
|
service.build_requested_review_recipients(note)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
service.build_requested_review_recipients(note)
|
|
|
|
end
|
|
|
|
|
|
|
|
create_user
|
|
|
|
|
|
|
|
expect { service.build_requested_review_recipients(note) }.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 07:14:27 -04:00
|
|
|
end
|