diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-10 12:08:16 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-10 12:08:16 +0000 |
commit | 1fa79760ad2d4bd67f5c5a27f372a7533b9b7c69 (patch) | |
tree | ffdfbd9113743831ff4f1290959a62cf6567fde5 /spec/services/notification_recipients | |
parent | 82fa8a3d1e8466ef36b58604d20fcc145ea12118 (diff) | |
download | gitlab-ce-1fa79760ad2d4bd67f5c5a27f372a7533b9b7c69.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services/notification_recipients')
-rw-r--r-- | spec/services/notification_recipients/build_service_spec.rb | 61 | ||||
-rw-r--r-- | spec/services/notification_recipients/builder/default_spec.rb | 44 |
2 files changed, 105 insertions, 0 deletions
diff --git a/spec/services/notification_recipients/build_service_spec.rb b/spec/services/notification_recipients/build_service_spec.rb new file mode 100644 index 00000000000..2e848c2f04d --- /dev/null +++ b/spec/services/notification_recipients/build_service_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe NotificationRecipients::BuildService do + 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) } + + shared_examples 'no N+1 queries' do + it 'avoids N+1 queries', :request_store do + create_user + + service.build_new_note_recipients(note) + + control_count = ActiveRecord::QueryRecorder.new do + service.build_new_note_recipients(note) + end + + create_user + + expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count) + end + end + + context 'when there are multiple watchers' do + def create_user + watcher = create(:user) + create(:notification_setting, source: project, user: watcher, level: :watch) + + other_projects.each do |other_project| + create(:notification_setting, source: other_project, user: watcher, level: :watch) + end + end + + include_examples 'no N+1 queries' + end + + context 'when there are multiple subscribers' do + def create_user + subscriber = create(:user) + issue.subscriptions.create(user: subscriber, project: project, subscribed: true) + end + + include_examples 'no N+1 queries' + + context 'when the project is private' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) + end + + include_examples 'no N+1 queries' + end + end + end +end diff --git a/spec/services/notification_recipients/builder/default_spec.rb b/spec/services/notification_recipients/builder/default_spec.rb new file mode 100644 index 00000000000..307ca40248e --- /dev/null +++ b/spec/services/notification_recipients/builder/default_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe NotificationRecipients::Builder::Default do + describe '#build!' do + let_it_be(:group) { create(:group, :public) } + let_it_be(:project) { create(:project, :public, group: group).tap { |p| p.add_developer(project_watcher) } } + let_it_be(:issue) { create(:issue, project: project) } + + let_it_be(:current_user) { create(:user) } + let_it_be(:other_user) { create(:user) } + let_it_be(:participant) { create(:user) } + let_it_be(:group_watcher) { create(:user) } + let_it_be(:project_watcher) { create(:user) } + + let_it_be(:notification_setting_project_w) { create(:notification_setting, source: project, user: project_watcher, level: 2) } + let_it_be(:notification_setting_group_w) { create(:notification_setting, source: group, user: group_watcher, level: 2) } + + subject { described_class.new(issue, current_user, action: :new).tap { |s| s.build! } } + + context 'participants and project watchers' do + before do + expect(issue).to receive(:participants).and_return([participant, current_user]) + end + + it 'adds all participants and watchers' do + expect(subject.recipients.map(&:user)).to include(participant, project_watcher, group_watcher) + expect(subject.recipients.map(&:user)).not_to include(other_user) + end + end + + context 'subscribers' do + it 'adds all subscribers' do + subscriber = create(:user) + non_subscriber = create(:user) + create(:subscription, project: project, user: subscriber, subscribable: issue, subscribed: true) + create(:subscription, project: project, user: non_subscriber, subscribable: issue, subscribed: false) + + expect(subject.recipients.map(&:user)).to include(subscriber) + end + end + end +end |