OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
let mail sending rely on notifications
ulferts67e3a7a15af6
diff --git a/app/services/notifications/journal_notification_service.rb b/app/services/notifications/journal_notification_service.rbindex 07732f19ed7..c2a4bc1e092 100644--- a/app/services/notifications/journal_notification_service.rb+++ b/app/services/notifications/journal_notification_service.rb@@ -50,13 +50,13 @@ class Notifications::JournalNotificationServiceaggregated = find_aggregated_journal_for(journal)if Journal::AggregatedJournal.hides_notifications?(aggregated, aggregated.predecessor)- work_package = aggregated.predecessor.journable- notify_journal_complete(work_package, aggregated.predecessor, send_mails)+ aggregated_predecessor = find_aggregated_journal_for(aggregated.predecessor)+ notify_journal_complete(aggregated_predecessor, send_mails)endenddef enqueue_work_package_notification(journal, send_mails)- job = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id, send_mails)+ job = EnqueueWorkPackageNotificationJob.new(journal.id, send_mails)Delayed::Job.enqueue job,run_at: delivery_time,priority: ::ApplicationJob.priority_number(:notification)diff --git a/app/services/notifications/journal_notifier.rb b/app/services/notifications/journal_notifier.rbindex 1c6a3ec5063..e696128033a 100644--- a/app/services/notifications/journal_notifier.rb+++ b/app/services/notifications/journal_notifier.rb@@ -35,98 +35,11 @@ module Notifications::JournalNotifierJournal::AggregatedJournal.with_version(raw_journal)end- def notify_journal_complete(work_package, journal, send_mails)- journal_complete_mail(work_package, journal, send_mails)-- journal_complete_notification(journal)- end-- def journal_complete_mail(work_package, journal, send_mails)- return nil unless send_mail?(journal, send_mails)-- notification_receivers(work_package, journal).each do |recipient|- job = DeliverWorkPackageNotificationJob.new(journal.id,- recipient.id,- User.current.id)- Delayed::Job.enqueue job, priority: ::ApplicationJob.priority_number(:notification)- end- end-- def journal_complete_notification(journal)+ def notify_journal_complete(journal, send_mails)OpenProject::Notifications.send(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal.id,- initial: journal.initial?+ journal: journal,+ send_mail: send_mails)end-- def notification_receivers(work_package, journal)- (work_package.recipients + work_package.watcher_recipients + mentioned(work_package, journal)).uniq- end-- def mentioned(work_package, journal)- mentioned_ids(journal)- .where(id: User.allowed(:view_work_packages, work_package.project))- .where.not(mail_notification: User::USER_MAIL_OPTION_NON.first)- end-- def text_for_mentions(journal)- potential_text = ""- potential_text << journal.notes if journal.try(:notes)-- %i[description subject].each do |field|- if journal.details[field].try(:any?)- from = journal.details[field].first- to = journal.details[field].second- potential_text << "\n" + Redmine::Helpers::Diff.new(to, from).additions.join(' ')- end- end- potential_text- end-- def mentioned_ids(journal)- text = text_for_mentions(journal)- user_ids, user_login_names, group_ids = text- .scan(/\b(?:user#([\d]+))|(?:user:"(.+?)")|(?:group#([\d]+))\b/)- .transpose- .each(&:compact!)-- base_scope = User- .includes(:groups)- .references(:groups_users)-- by_id = base_scope.where(id: user_ids || [])- by_login = base_scope.where(login: user_login_names || [])- by_group = base_scope.where(groups_users: { id: group_ids || [] })-- by_id- .or(by_login)- .or(by_group)- end-- def send_mail?(journal, send_mails)- send_mails && ::UserMailer.perform_deliveries && send_mail_setting?(journal)- end-- def send_mail_setting?(journal)- (Setting.notified_events.include?('work_package_added') && journal.initial?) ||- (Setting.notified_events.include?('work_package_updated') && !journal.initial?) ||- notify_for_notes?(journal) ||- notify_for_status?(journal) ||- notify_for_priority(journal)- end-- def notify_for_notes?(journal)- Setting.notified_events.include?('work_package_note_added') && journal.notes.present?- end-- def notify_for_status?(journal)- Setting.notified_events.include?('status_updated') &&- journal.details.has_key?(:status_id)- end-- def notify_for_priority(journal)- Setting.notified_events.include?('work_package_priority_updated') &&- journal.details.has_key?(:priority_id)- endenddiff --git a/app/services/notifications/journal_wp_mail_service.rb b/app/services/notifications/journal_wp_mail_service.rbnew file mode 100644index 00000000000..cb8752cb493--- /dev/null+++ b/app/services/notifications/journal_wp_mail_service.rb@@ -0,0 +1,137 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class Notifications::JournalWPMailService+ class << self+ include Notifications::JournalNotifier++ def call(journal, send_mails)+ journal_complete_mail(journal, send_mails)+ end++ private++ def journal_complete_mail(journal, send_mails)+ return nil unless send_mail?(journal, send_mails)++ author = User.find_by(id: journal.user_id) || DeletedUser.first++ notification_receivers(journal.journable, journal).each do |recipient|+ job = DeliverWorkPackageNotificationJob.new(journal.id,+ recipient.id,+ author.id)+ Delayed::Job.enqueue job,+ priority: ::ApplicationJob.priority_number(:notification)+ end+ end++ def notification_receivers(work_package, journal)+ (work_package.recipients + work_package.watcher_recipients + mentioned(work_package, journal)).uniq+ end++ def mentioned(work_package, journal)+ mentioned_ids(journal)+ .where(id: User.allowed(:view_work_packages, work_package.project))+ .where.not(mail_notification: User::USER_MAIL_OPTION_NON.first)+ end++ def text_for_mentions(journal)+ potential_text = ""+ potential_text << journal.notes if journal.try(:notes)++ %i[description subject].each do |field|+ if journal.details[field].try(:any?)+ from = journal.details[field].first+ to = journal.details[field].second+ potential_text << "\n" + Redmine::Helpers::Diff.new(to, from).additions.join(' ')+ end+ end+ potential_text+ end++ def mentioned_ids(journal)+ text = text_for_mentions(journal)+ user_ids, user_login_names, group_ids = text+ .scan(/\b(?:user#([\d]+))|(?:user:"(.+?)")|(?:group#([\d]+))\b/)+ .transpose+ .each(&:compact!)++ base_scope = User+ .includes(:groups)+ .references(:groups_users)++ by_id = base_scope.where(id: user_ids || [])+ by_login = base_scope.where(login: user_login_names || [])+ by_group = base_scope.where(groups_users: { id: group_ids || [] })++ by_id+ .or(by_login)+ .or(by_group)+ end++ def send_mail?(journal, send_mails)+ send_mails && ::UserMailer.perform_deliveries && send_mail_setting?(journal)+ end++ def send_mail_setting?(journal)+ notify_for_wp_added?(journal) ||+ notify_for_wp_updated?(journal) ||+ notify_for_notes?(journal) ||+ notify_for_status?(journal) ||+ notify_for_priority(journal)+ end++ def notify_for_wp_added?(journal)+ notification_enabled?('work_package_added') && journal.initial?+ end++ def notify_for_wp_updated?(journal)+ notification_enabled?('work_package_updated') && !journal.initial?+ end++ def notify_for_notes?(journal)+ notification_enabled?('work_package_note_added') && journal.notes.present?+ end++ def notify_for_status?(journal)+ notification_enabled?('status_updated') &&+ journal.details.has_key?(:status_id)+ end++ def notify_for_priority(journal)+ notification_enabled?('work_package_priority_updated') &&+ journal.details.has_key?(:priority_id)+ end++ def notification_enabled?(name)+ Setting.notified_events.include?(name)+ end+ end+enddiff --git a/app/workers/enqueue_work_package_notification_job.rb b/app/workers/enqueue_work_package_notification_job.rbindex 18ec4d55f8d..52f664196a3 100644--- a/app/workers/enqueue_work_package_notification_job.rb+++ b/app/workers/enqueue_work_package_notification_job.rb@@ -31,9 +31,8 @@class EnqueueWorkPackageNotificationJob < ApplicationJobinclude Notifications::JournalNotifier- def initialize(journal_id, author_id, send_mails)+ def initialize(journal_id, send_mails)@journal_id = journal_id- @author_id = author_id@send_mails = send_mailsend@@ -53,10 +52,7 @@ class EnqueueWorkPackageNotificationJob < ApplicationJob# on behalf of this job.return if Journal::AggregatedJournal.hides_notifications?(journal.successor, journal)- author = User.find_by(id: @author_id) || DeletedUser.first- User.execute_as(author) do- notify_journal_complete(work_package, journal, @send_mails)- end+ notify_journal_complete(journal, @send_mails)endprivatediff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rbindex 735fea4c523..362fa8992c9 100644--- a/config/initializers/subscribe_listeners.rb+++ b/config/initializers/subscribe_listeners.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is a project management system.# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)@@ -31,6 +32,10 @@ OpenProject::Notifications.subscribe('journal_created') do |payload|Notifications::JournalNotificationService.call(payload[:journal], payload[:send_notification])end+OpenProject::Notifications.subscribe(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY) do |payload|+ Notifications::JournalWPMailService.call(payload[:journal], payload[:send_mail])+end+OpenProject::Notifications.subscribe('watcher_added') do |payload|WatcherNotificationMailer.handle_watcher(payload[:watcher], payload[:watcher_setter])enddiff --git a/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rbindex af5e019defe..02f54ba212f 100644--- a/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb+++ b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb@@ -20,10 +20,10 @@ module OpenProject::Webhooks::EventResourcesprotecteddef handle_notification(payload, event_name)- action = payload[:initial] ? "created" : "updated"+ action = payload[:journal].initial? ? "created" : "updated"event_name = prefixed_event_name(action)active_webhooks.with_event_name(event_name).pluck(:id).each do |id|- Delayed::Job.enqueue WorkPackageWebhookJob.new(id, payload[:journal_id], event_name)+ Delayed::Job.enqueue WorkPackageWebhookJob.new(id, payload[:journal].id, event_name)endendenddiff --git a/spec/models/work_package/openproject_notifications_spec.rb b/spec/models/work_package/openproject_notifications_spec.rbindex 0a1f7779806..c739cd31903 100644--- a/spec/models/work_package/openproject_notifications_spec.rb+++ b/spec/models/work_package/openproject_notifications_spec.rb@@ -47,7 +47,7 @@ describe WorkPackage, type: :model dobefore doOpenProject::Notifications.subscribe(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY) do |payload|- journal_ids << payload[:journal_id]+ journal_ids << payload[:journal].idendenddiff --git a/spec/services/notifications/journal_notification_service_spec.rb b/spec/services/notifications/journal_notification_service_spec.rbindex 4ee6e256d16..436c8b8407c 100644--- a/spec/services/notifications/journal_notification_service_spec.rb+++ b/spec/services/notifications/journal_notification_service_spec.rb@@ -65,7 +65,7 @@ describe Notifications::JournalNotificationService doexpect(EnqueueWorkPackageNotificationJob).to receive(:new)- .with(journal.id, user.id, send_mails)+ .with(journal.id, send_mails).and_return(enqueue_job)expect(Delayed::Job).to receive(:enqueue)@@ -73,23 +73,19 @@ describe Notifications::JournalNotificationService dorun_at: anything,priority: ::ApplicationJob.priority_number(:notification))- # immediate delivery is not part of regular notfications, it only covers an edge-case- expect(Delayed::Job)- .not_to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), anything)call_listenerendend- shared_examples_for 'handles deliveries' do |notification_setting|- context 'setting enabled' do- let(:notifications) { [notification_setting] }+ shared_examples_for 'sends notification' do+ it 'sends a notification' do+ expect(OpenProject::Notifications)+ .to receive(:send)+ .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,+ journal: an_instance_of(Journal::AggregatedJournal),+ send_mail: send_mails)- it_behaves_like 'enqueues a regular notification'- end-- context 'with setting specified to not send mails' do- it_behaves_like 'enqueues a regular notification'+ call_listenerendend@@ -99,7 +95,7 @@ describe Notifications::JournalNotificationService doFactoryBot.create(:work_package, project: project)end- it_behaves_like 'handles deliveries', 'work_package_added'+ it_behaves_like 'enqueues a regular notification'endcontext 'work_package_updated' do@@ -109,11 +105,7 @@ describe Notifications::JournalNotificationService dowork_package.save!(validate: false)end- context 'setting enabled' do- let(:notifications) { ['work_package_updated'] }-- it_behaves_like 'enqueues a regular notification'- end+ it_behaves_like 'enqueues a regular notification'endcontext 'work_package_note_added' do@@ -122,27 +114,7 @@ describe Notifications::JournalNotificationService dowork_package.save!(validate: false)end- it_behaves_like 'handles deliveries', 'work_package_note_added'- end-- context 'status_updated' do- before do- work_package.add_journal(user)- work_package.status = FactoryBot.build(:status)- work_package.save!(validate: false)- end-- it_behaves_like 'handles deliveries', 'status_updated'- end-- context 'work_package_priority_updated' do- before do- work_package.add_journal(user)- work_package.priority = FactoryBot.create(:issue_priority)- work_package.save!(validate: false)- end-- it_behaves_like 'handles deliveries', 'work_package_priority_updated'+ it_behaves_like 'enqueues a regular notification'endend@@ -166,7 +138,6 @@ describe Notifications::JournalNotificationService do# This is important since late exec of a Job might cause it to _not_ skip notificationslet(:author) { user }- let(:notifications) { ['work_package_updated'] }let(:timeout) { Setting.journal_aggregation_time_minutes.to_i.minutes }let(:journal_1) { work_package.journals[1] }@@ -236,110 +207,8 @@ describe Notifications::JournalNotificationService dojournal_3.update_attribute(:created_at, journal_1.created_at + timeout + 5.seconds)end- it 'immediately delivers a mail on behalf of journal 1' do- deliver_job = double('deliver job')-- expect(DeliverWorkPackageNotificationJob)- .to receive(:new)- .with(journal_1.id, user.id, user.id)- .and_return(deliver_job)-- expect(Delayed::Job)- .to receive(:enqueue)- .with(deliver_job, priority: anything)- call_listener- end-- it 'immediately notifies on behalf of journal 1' do- expect(OpenProject::Notifications)- .to receive(:send)- .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal_1.id,- initial: false)- call_listener- end-- it 'also enqueues a regular mail for journal 3' do- enqueue_job = double('enqueue job')-- expect(EnqueueWorkPackageNotificationJob)- .to receive(:new)- .with(journal_3.id, user.id, send_mails)- .and_return(enqueue_job)-- expect(Delayed::Job)- .to receive(:enqueue)- .with(- enqueue_job,- priority: anything,- run_at: anything- )- call_listener- end-- context 'with the settings not including updates' do- let(:notifications) { [] }-- it 'does not deliver a mail on behalf of Journal 1' do- expect(DeliverWorkPackageNotificationJob)- .not_to receive(:new)- .with(journal_1.id, user.id, user.id)-- call_listener- end-- it 'immediately notifies on behalf of journal 1' do- expect(OpenProject::Notifications)- .to receive(:send)- .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal_1.id,- initial: false)- call_listener- end-- it 'also enqueues a regular mail' do- expect(Delayed::Job)- .to receive(:enqueue)- .with(- an_instance_of(EnqueueWorkPackageNotificationJob),- priority: anything,- run_at: anything- )- call_listener- end- end-- context 'with send_mails = false provided' do- let(:send_mails) { false }-- it 'does not deliver a mail on behalf of Journal 1' do- expect(DeliverWorkPackageNotificationJob)- .not_to receive(:new)- .with(journal_1.id, user.id, user.id)-- call_listener- end-- it 'immediately notifies on behalf of journal 1' do- expect(OpenProject::Notifications)- .to receive(:send)- .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal_1.id,- initial: false)- call_listener- end-- it 'also enqueues a regular mail' do- expect(Delayed::Job)- .to receive(:enqueue)- .with(- an_instance_of(EnqueueWorkPackageNotificationJob),- priority: anything,- run_at: anything- )- call_listener- end- end+ it_behaves_like 'sends notification' # for journal 1+ it_behaves_like 'enqueues a regular notification' # for journal 3endenddiff --git a/spec/services/notifications/journal_wp_mail_service_spec.rb b/spec/services/notifications/journal_wp_mail_service_spec.rbnew file mode 100644index 00000000000..314c37831f3--- /dev/null+++ b/spec/services/notifications/journal_wp_mail_service_spec.rb@@ -0,0 +1,359 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#+++require 'spec_helper'++describe Notifications::JournalWPMailService do+ let(:project) { FactoryBot.create(:project_with_types) }+ let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }+ let(:author) do+ FactoryBot.build(:user,+ mail_notification: 'none',+ member_in_project: project,+ member_through_role: role)+ end+ let(:recipient) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_through_role: role,+ login: "johndoe")+ end+ let(:work_package) do+ FactoryBot.create(:work_package,+ project: project,+ author: author,+ assigned_to: recipient,+ type: project.types.first)+ end+ let(:journal) { journal_1 }+ let(:journal_1) { work_package.journals.first }+ let(:journal_2_with_notes) do+ work_package.add_journal author, 'something I have to say'+ work_package.save(validate: false)+ work_package.journals.last+ end+ let(:journal_2_with_status) do+ work_package.status = FactoryBot.create(:status)+ work_package.save(validate: false)+ work_package.journals.last+ end+ let(:journal_2_with_priority) do+ work_package.priority = FactoryBot.create(:priority)+ work_package.save(validate: false)+ work_package.journals.last+ end+ let(:send_mails) { true }+ let(:notification_setting) { %w(work_package_added work_package_updated work_package_note_added status_updated work_package_priority_updated) }++ def call+ described_class.call(journal, send_mails)+ end++ before do+ # make sure no other calls are made due to WP creation/update+ allow(OpenProject::Notifications).to receive(:send) # ... and do nothing++ login_as(author)+ allow(Setting).to receive(:notified_events).and_return(notification_setting)++ allow(Delayed::Job).to receive(:enqueue)+ end++ shared_examples_for 'sends mail' do+ let(:sender) { author }++ it 'sends a mail' do+ deliver_job = double('deliver job')++ expect(DeliverWorkPackageNotificationJob)+ .to receive(:new)+ .with(journal.id, recipient.id, sender.id)+ .and_return(deliver_job)++ expect(Delayed::Job)+ .to receive(:enqueue)+ .with(deliver_job, priority: anything)++ call+ end+ end++ shared_examples_for 'sends no mail' do+ it 'sends no mail' do+ expect(Delayed::Job).not_to receive(:enqueue)++ call+ end+ end++ it_behaves_like 'sends mail'++ context 'notification for work_package_added disabled' do+ let(:notification_setting) { %w(work_package_updated work_package_note_added) }++ it_behaves_like 'sends no mail'+ end++ context 'if the journal has a note' do+ let(:journal) { journal_2_with_notes }++ context 'notification for work_package_updated and work_package_note_added disabled' do+ let(:notification_setting) { %w(work_package_added status_updated work_package_priority_updated) }++ it_behaves_like 'sends no mail'+ end++ context 'notification for work_package_updated enabled' do+ let(:notification_setting) { %w(work_package_updated) }++ it_behaves_like 'sends mail'+ end++ context 'notification for work_package_note_added enabled' do+ let(:notification_setting) { %w(work_package_note_added) }++ it_behaves_like 'sends mail'+ end+ end++ context 'if the journal has status update' do+ let(:journal) { journal_2_with_status }++ context 'notification for work_package_updated and status_updated disabled' do+ let(:notification_setting) { %w(work_package_added work_package_note_added work_package_priority_updated) }++ it_behaves_like 'sends no mail'+ end++ context 'notification for work_package_updated enabled' do+ let(:notification_setting) { %w(work_package_updated) }++ it_behaves_like 'sends mail'+ end++ context 'notification for status_updated enabled' do+ let(:notification_setting) { %w(status_updated) }++ it_behaves_like 'sends mail'+ end+ end++ context 'if the journal has priority' do+ let(:journal) { journal_2_with_priority }++ context 'notification for work_package_updated and work_package_priority_updated disabled' do+ let(:notification_setting) { %w(work_package_added work_package_note_added status_updated) }++ it_behaves_like 'sends no mail'+ end++ context 'notification for work_package_updated enabled' do+ let(:notification_setting) { %w(work_package_updated) }++ it_behaves_like 'sends mail'+ end++ context 'notification for work_package_priority_updated enabled' do+ let(:notification_setting) { %w(work_package_priority_updated) }++ it_behaves_like 'sends mail'+ end+ end++ context 'if the author has been deleted' do+ let!(:deleted_user) { DeletedUser.first }++ before do+ work_package+ author.destroy+ end++ it_behaves_like 'sends mail' do+ let(:sender) { deleted_user }+ end+ end++ context 'user is mentioned' do+ let(:work_package) do+ FactoryBot.create(:work_package,+ project: project,+ author: author,+ type: project.types.first)+ end++ shared_examples_for 'mentioned' do+ context 'for users' do+ context "mentioned is allowed to view the work package" do+ context "The added text contains a login name" do+ let(:note) { "Hello user:\"#{recipient.login}\"" }++ context "that is pretty normal word" do+ it_behaves_like 'sends mail'+ end++ context "that is an email address" do+ let(:recipient) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_through_role: role,+ login: "[email protected]")+ end++ it_behaves_like 'sends mail'+ end+ end++ context "The added text contains a user ID" do+ let(:note) { "Hello user##{recipient.id}" }++ it_behaves_like 'sends mail'+ end++ context "the recipient turned off all mail notifications" do+ let(:recipient) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_through_role: role,+ mail_notification: 'none')+ end++ let(:note) do+ "Hello user:\"#{recipient.login}\", hey user##{recipient.id}"+ end++ it_behaves_like 'sends no mail'+ end+ end++ context "mentioned user is not allowed to view the work package" do+ let(:recipient) do+ FactoryBot.create(:user,+ login: "[email protected]")+ end+ let(:note) do+ "Hello user:#{recipient.login}, hey user##{recipient.id}"+ end++ it_behaves_like 'sends no mail'+ end+ end++ context 'for groups' do+ let(:recipient) { FactoryBot.create(:user) }++ let(:group) do+ FactoryBot.create(:group) do |group|+ group.users << recipient++ FactoryBot.create(:member,+ project: project,+ principal: group,+ roles: [role])+ end+ end++ let(:note) do+ "Hello group##{group.id}"+ end++ context 'group member is allowed to view the work package' do+ context 'user wants to receive notifications' do+ it_behaves_like 'sends mail'+ end++ context 'user disabled notifications' do+ let(:recipient) { FactoryBot.create(:user, mail_notification: User::USER_MAIL_OPTION_NON.first) }++ it_behaves_like 'sends no mail'+ end+ end++ context 'group is not allowed to view the work package' do+ let(:role) { FactoryBot.create(:role, permissions: []) }++ it_behaves_like 'sends no mail'++ context 'but group member is allowed individually' do+ let(:recipient) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_with_permissions: [:view_work_packages])+ end++ it_behaves_like 'sends mail'+ end+ end+ end+ end++ context 'in the journal notes' do+ let(:journal) { journal_2_with_notes }+ let(:journal_2_with_notes) do+ work_package.add_journal author, note+ work_package.save(validate: false)+ work_package.journals.last+ end++ it_behaves_like 'mentioned'+ end++ context 'in the description' do+ let(:journal) { journal_2_with_description }+ let(:journal_2_with_description) do+ work_package.description = note+ work_package.save(validate: false)+ work_package.journals.last+ end++ it_behaves_like 'mentioned'+ end++ context 'in the subject' do+ let(:journal) { journal_2_with_subject }+ let(:journal_2_with_subject) do+ work_package.subject = note+ work_package.save(validate: false)+ work_package.journals.last+ end++ it_behaves_like 'mentioned'+ end+ end+end++describe 'initialization' do+ it 'subscribes the listener' do+ expect(Notifications::JournalWPMailService).to receive(:call)++ OpenProject::Notifications.send(+ OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,+ journal: double('journal', initial?: true)+ )+ end+enddiff --git a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb b/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rbindex e2a53c5d0bf..e4fb90b4de7 100644--- a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb+++ b/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is a project management system.# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)@@ -50,14 +51,11 @@ describe EnqueueWorkPackageNotificationJob, type: :model dowork_package.journals.lastendlet(:send_mail) { true }- let(:notification_setting) { %w(work_package_added work_package_updated work_package_note_added status_updated work_package_priority_updated) }- subject { described_class.new(journal.id, author.id, send_mail) }+ subject { described_class.new(journal.id, send_mail) }before do# make sure no other calls are made due to WP creation/updateallow(OpenProject::Notifications).to receive(:send) # ... and do nothing-- allow(Setting).to receive(:notified_events).and_return(notification_setting)endshared_examples_for 'sends notification' do@@ -65,8 +63,8 @@ describe EnqueueWorkPackageNotificationJob, type: :model doexpect(OpenProject::Notifications).to receive(:send).with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal.id,- initial: journal.initial?)+ journal: an_instance_of(Journal::AggregatedJournal),+ send_mail: send_mail)subject.performend@@ -81,34 +79,6 @@ describe EnqueueWorkPackageNotificationJob, type: :model doendend- shared_examples_for 'sends mail' do- let(:sender) { author }-- it 'sends a mail' do- deliver_job = double('deliver job')-- expect(DeliverWorkPackageNotificationJob)- .to receive(:new)- .with(journal.id, recipient.id, sender.id)- .and_return(deliver_job)-- expect(Delayed::Job)- .to receive(:enqueue)- .with(deliver_job, priority: anything)-- subject.perform- end- end-- shared_examples_for 'sends no mail' do- it 'sends no mail' do- expect(Delayed::Job).not_to receive(:enqueue)- subject.perform- end- end-- it_behaves_like 'sends mail'-it_behaves_like 'sends notification'context 'non-existant journal' do@@ -116,24 +86,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model dojournal.destroyend- it_behaves_like 'sends no mail'it_behaves_like 'sends no notification'end- context 'non-existant author' do- before do- allow(Delayed::Job).to receive(:enqueue)-- author.destroy- end-- it_behaves_like 'sends mail' do- let(:sender) { DeletedUser.first }- end-- it_behaves_like 'sends notification'- end-context 'outdated journal' dobefore do# make sure there is a later journal, that supersedes the original one@@ -141,28 +96,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model dowork_package.save!end- it_behaves_like 'sends no mail'it_behaves_like 'sends no notification'end- context 'notification for work_package_added disabled' do- let(:notification_setting) { %w(work_package_updated work_package_note_added) }-- it_behaves_like 'sends no mail'-- it_behaves_like 'sends notification'- end-- context 'notification for work_package_updated disabled' do- let(:notification_setting) { %w(work_package_added status_updated work_package_priority_updated) }-- let(:journal) { journal_2 }-- it_behaves_like 'sends no mail'-- it_behaves_like 'sends notification'- end-describe 'mail suppressing aggregation' do# business logic of whether to send or not to send a mail is mainly driven by the presence# of an aggregated journal. However, there is an edge case that could lead to a notification@@ -204,25 +140,22 @@ describe EnqueueWorkPackageNotificationJob, type: :model do# The job for 2 will know, that it has become part of Journal 3.# -> no special behaviour required- it 'Job 1 sends one mail for journal 1' do- expect(Delayed::Job)- .to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)- .once- described_class.new(journal_1.id, author.id, send_mail).perform+ context 'for journal 1' do+ let(:journal) { journal_1 }++ it_behaves_like 'sends notification' # for journal 1end- it 'Job 2 sends no mails' do- expect(Delayed::Job).not_to receive(:enqueue)- described_class.new(journal_2.id, author.id, send_mail).perform+ context 'for journal 2' do+ let(:journal) { journal_2 }++ it_behaves_like 'sends no notification'end- it 'Job 3 sends one mail for journal (2,3)' do- expect(Delayed::Job)- .to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)- .once- described_class.new(journal_3.id, author.id, send_mail).perform+ context 'for journal 3' do+ let(:journal) { journal_3 }++ it_behaves_like 'sends notification' # for journals 2 and 3endend@@ -240,22 +173,22 @@ describe EnqueueWorkPackageNotificationJob, type: :model dojournal_3.save!end- it 'Job 1 sends no mails' do- expect(Delayed::Job).not_to receive(:enqueue)- described_class.new(journal_1.id, author.id, send_mail).perform+ context 'for journal 1' do+ let(:journal) { journal_1 }++ it_behaves_like 'sends no notification'end- it 'Job 2 sends no mails' do- expect(Delayed::Job).not_to receive(:enqueue)- described_class.new(journal_2.id, author.id, send_mail).perform+ context 'for journal 2' do+ let(:journal) { journal_2 }++ it_behaves_like 'sends no notification'end- it 'Job 3 sends one mail for (2,3)' do- expect(Delayed::Job)- .to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)- .once- described_class.new(journal_3.id, author.id, send_mail).perform+ context 'for journal 3' do+ let(:journal) { journal_3 }++ it_behaves_like 'sends notification' # for journals 1, 2 and 3endend@@ -269,201 +202,22 @@ describe EnqueueWorkPackageNotificationJob, type: :model dojournal_3.save!end- it 'Job 1 sends no mails' do- expect(Delayed::Job).not_to receive(:enqueue)- described_class.new(journal_1.id, author.id, send_mail).perform- end+ context 'for journal 1' do+ let(:journal) { journal_1 }- it 'Job 2 sends one mail for journal (1, 2)' do- expect(Delayed::Job).to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)- .once- described_class.new(journal_2.id, author.id, send_mail).perform+ it_behaves_like 'sends no notification'end- it 'Job 3 sends one mail for journal 3' do- expect(Delayed::Job).to receive(:enqueue)- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)- .once- described_class.new(journal_3.id, author.id, send_mail).perform- end- end- end-- describe "#text_for_mentions" do- it "returns a text" do- subject.perform- expect(subject.send(:text_for_mentions, journal)).to be_a String- end-- context "subject and description changed" do- let(:title) { 'New subject' }- let(:description) { 'New description' }- let(:notes) { 'Nice notes!' }- let(:journal) { work_package.journals.last }-- subject { described_class.new(work_package.journals.last.id, author.id, send_mail) }-- before do- work_package.subject = title- work_package.description = description- work_package.save!+ context 'for journal 2' do+ let(:journal) { journal_2 }- work_package.journals.last.notes = notes- work_package.journals.last.save-- subject.perform+ it_behaves_like 'sends notification' # for journals 1 and 2end- it "returns notes, subject and description" do- expect(subject.send(:text_for_mentions, journal)).not_to be_blank- expect(subject.send(:text_for_mentions, journal)).to match title- expect(subject.send(:text_for_mentions, journal)).to match description- expect(subject.send(:text_for_mentions, journal)).to match notes- end- end- end-- describe "#mentioned" do- subject do- instance = described_class.new(journal.id, author.id, send_mail)- instance.perform-- allow(instance)- .to receive(:text_for_mentions)- .and_return(added_text)-- instance.send(:mentioned, work_package, journal)- end-- context 'for users' do- context "mentioned is allowed to view the work package" do- context "The added text contains a login name" do- let(:added_text) { "Hello user:\"#{recipient.login}\"" }-- context "that is pretty normal word" do- it "detects the user" do- is_expected- .to match_array [recipient]- end- end-- context "that is an email address" do- let(:recipient) do- FactoryBot.create(:user,- member_in_project: project,- member_through_role: role,- login: "[email protected]")- end-- it "detects the user" do- is_expected- .to match_array [recipient]- end- end- end-- context "The added text contains a user ID" do- let(:added_text) { "Hello user##{recipient.id}" }-- it "detects the user" do- is_expected- .to match_array [recipient]- end- end-- context "the recipient turned off all mail notifications" do- let(:recipient) do- FactoryBot.create(:user,- member_in_project: project,- member_through_role: role,- mail_notification: 'none')- end-- let(:added_text) do- "Hello user:\"#{recipient.login}\", hey user##{recipient.id}"- end-- it "no user gets detected" do- is_expected- .to be_empty- end- end- end-- context "mentioned user is not allowed to view the work package" do- let(:recipient) do- FactoryBot.create(:user,- login: "[email protected]")- end- let(:added_text) do- "Hello user:#{recipient.login}, hey user##{recipient.id}"- end-- it "no user gets detected" do- is_expected- .to be_empty- end- end- end-- context 'for groups' do- let(:group_member) { FactoryBot.create(:user) }-- let(:group) do- FactoryBot.create(:group) do |group|- group.users << group_member-- FactoryBot.create(:member,- project: project,- principal: group,- roles: [role])- end- end-- let(:added_text) do- "Hello group##{group.id}"- end-- context 'group member is allowed to view the work package' do- context 'user wants to receive notifications' do- it "group member gets detected" do- is_expected- .to match_array([group_member])- end- end-- context 'user disabled notifications' do- let(:group_member) { FactoryBot.create(:user, mail_notification: User::USER_MAIL_OPTION_NON.first) }-- it "group member is ignored" do- is_expected- .to be_empty- end- end- end+ context 'for journal 3' do+ let(:journal) { journal_3 }- context 'group is not allowed to view the work package' do- let(:role) { FactoryBot.create(:role, permissions: []) }-- it "group member is ignored" do- is_expected- .to be_empty- end-- context 'but group member is allowed individually' do- before do- FactoryBot.create(:member,- project: project,- principal: group_member,- roles: [FactoryBot.create(:role, permissions: [:view_work_packages])])- end-- it "group member gets detected" do- is_expected- .to match_array([group_member])- end- end+ it_behaves_like 'sends notification' # for journal 3endendend