From 8c56c6ecefb6aa50ea83094f8eb87fe80108d1db Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Tue, 7 Apr 2026 13:15:46 +0100 Subject: [PATCH] Actually use notification preferences --- app/notifiers/changeset_comment_notifier.rb | 1 + app/notifiers/diary_comment_notifier.rb | 1 + app/notifiers/direct_message_notifier.rb | 1 + app/notifiers/gpx_import_failure_notifier.rb | 1 + app/notifiers/gpx_import_success_notifier.rb | 1 + app/notifiers/new_follower_notifier.rb | 1 + app/notifiers/note_comment_notifier.rb | 1 + test/notifiers/changeset_notifier_test.rb | 44 +++++++++++++++++++ test/notifiers/diary_comment_notifier_test.rb | 43 ++++++++++++++++++ .../notifiers/direct_message_notifier_test.rb | 38 ++++++++++++++++ .../gpx_import_failure_notifier_test.rb | 41 +++++++++++++++++ .../gpx_import_success_notifier_test.rb | 38 ++++++++++++++++ test/notifiers/new_follower_notifier_test.rb | 37 ++++++++++++++++ test/notifiers/note_comment_notifier_test.rb | 44 +++++++++++++++++++ test/system/notification_preferences_test.rb | 17 +++++++ 15 files changed, 309 insertions(+) create mode 100644 test/notifiers/changeset_notifier_test.rb create mode 100644 test/notifiers/diary_comment_notifier_test.rb create mode 100644 test/notifiers/direct_message_notifier_test.rb create mode 100644 test/notifiers/gpx_import_failure_notifier_test.rb create mode 100644 test/notifiers/gpx_import_success_notifier_test.rb create mode 100644 test/notifiers/new_follower_notifier_test.rb create mode 100644 test/notifiers/note_comment_notifier_test.rb diff --git a/app/notifiers/changeset_comment_notifier.rb b/app/notifiers/changeset_comment_notifier.rb index b3291cd70..5cc561397 100644 --- a/app/notifiers/changeset_comment_notifier.rb +++ b/app/notifiers/changeset_comment_notifier.rb @@ -8,5 +8,6 @@ class ChangesetCommentNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "changeset_comment_notification" + config.if = -> { recipient.notification_preferences.changeset_comment.include?("email") } end end diff --git a/app/notifiers/diary_comment_notifier.rb b/app/notifiers/diary_comment_notifier.rb index afe992ab6..0ef515d8e 100644 --- a/app/notifiers/diary_comment_notifier.rb +++ b/app/notifiers/diary_comment_notifier.rb @@ -8,5 +8,6 @@ class DiaryCommentNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "diary_comment_notification" + config.if = -> { recipient.notification_preferences.diary_comment.include?("email") } end end diff --git a/app/notifiers/direct_message_notifier.rb b/app/notifiers/direct_message_notifier.rb index 781419564..7374695e6 100644 --- a/app/notifiers/direct_message_notifier.rb +++ b/app/notifiers/direct_message_notifier.rb @@ -8,5 +8,6 @@ class DirectMessageNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "message_notification" + config.if = -> { recipient.notification_preferences.direct_message.include?("email") } end end diff --git a/app/notifiers/gpx_import_failure_notifier.rb b/app/notifiers/gpx_import_failure_notifier.rb index 598b39a05..0b6cbe101 100644 --- a/app/notifiers/gpx_import_failure_notifier.rb +++ b/app/notifiers/gpx_import_failure_notifier.rb @@ -4,5 +4,6 @@ class GpxImportFailureNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "gpx_failure" + config.if = -> { recipient.notification_preferences.gpx_import_failure.include?("email") } end end diff --git a/app/notifiers/gpx_import_success_notifier.rb b/app/notifiers/gpx_import_success_notifier.rb index 6edf67fbf..b0beda515 100644 --- a/app/notifiers/gpx_import_success_notifier.rb +++ b/app/notifiers/gpx_import_success_notifier.rb @@ -8,5 +8,6 @@ class GpxImportSuccessNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "gpx_success" + config.if = -> { recipient.notification_preferences.gpx_import_success.include?("email") } end end diff --git a/app/notifiers/new_follower_notifier.rb b/app/notifiers/new_follower_notifier.rb index 1508e2064..12c9e0a69 100644 --- a/app/notifiers/new_follower_notifier.rb +++ b/app/notifiers/new_follower_notifier.rb @@ -8,5 +8,6 @@ class NewFollowerNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "follow_notification" + config.if = -> { recipient.notification_preferences.new_follower.include?("email") } end end diff --git a/app/notifiers/note_comment_notifier.rb b/app/notifiers/note_comment_notifier.rb index 03cbb1794..98e4a6780 100644 --- a/app/notifiers/note_comment_notifier.rb +++ b/app/notifiers/note_comment_notifier.rb @@ -8,5 +8,6 @@ class NoteCommentNotifier < ApplicationNotifier deliver_by :email do |config| config.mailer = "UserMailer" config.method = "note_comment_notification" + config.if = -> { recipient.notification_preferences.note_comment.include?("email") } end end diff --git a/test/notifiers/changeset_notifier_test.rb b/test/notifiers/changeset_notifier_test.rb new file mode 100644 index 000000000..3abf6653d --- /dev/null +++ b/test/notifiers/changeset_notifier_test.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "test_helper" + +class ChangesetCommentNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("changeset_comment" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("changeset_comment" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(changeset_author) + changeset = create(:changeset, :user => changeset_author) + create(:changeset_subscription, :changeset => changeset, :subscriber => changeset_author) + + comment_author = create(:user) + changeset_comment = create(:changeset_comment, :author => comment_author, :changeset => changeset) + + perform_enqueued_jobs do + Nominatim.stub :describe_location, nil do + ChangesetCommentNotifier.with(:record => changeset_comment).deliver + end + end + end +end diff --git a/test/notifiers/diary_comment_notifier_test.rb b/test/notifiers/diary_comment_notifier_test.rb new file mode 100644 index 000000000..3c54ee3dc --- /dev/null +++ b/test/notifiers/diary_comment_notifier_test.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require "test_helper" + +class DiaryCommentNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("diary_comment" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("diary_comment" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(diary_author) + create(:language, :code => "en") + diary_entry = create(:diary_entry, :user => diary_author) + create(:diary_entry_subscription, :diary_entry => diary_entry, :user => diary_author) + + comment_author = create(:user) + diary_comment = create(:diary_comment, :user => comment_author, :diary_entry => diary_entry) + + perform_enqueued_jobs do + DiaryCommentNotifier.with(:record => diary_comment).deliver + end + end +end diff --git a/test/notifiers/direct_message_notifier_test.rb b/test/notifiers/direct_message_notifier_test.rb new file mode 100644 index 000000000..cc311ac8b --- /dev/null +++ b/test/notifiers/direct_message_notifier_test.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require "test_helper" + +class DirectMessageNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("direct_message" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("direct_message" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(message_recipient) + message = create(:message, :recipient => message_recipient) + + perform_enqueued_jobs do + DirectMessageNotifier.with(:record => message).deliver + end + end +end diff --git a/test/notifiers/gpx_import_failure_notifier_test.rb b/test/notifiers/gpx_import_failure_notifier_test.rb new file mode 100644 index 000000000..2064a3432 --- /dev/null +++ b/test/notifiers/gpx_import_failure_notifier_test.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require "test_helper" + +class GpxImportFailureNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("gpx_import_failure" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("gpx_import_failure" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(trace_author) + perform_enqueued_jobs do + GpxImportFailureNotifier.with( + :trace_name => "My trace", + :trace_description => "There are others like it, etc", + :trace_tags => %w[vegetarian assume elapse], + :error => "I'm sorry, Dave" + ).deliver(trace_author) + end + end +end diff --git a/test/notifiers/gpx_import_success_notifier_test.rb b/test/notifiers/gpx_import_success_notifier_test.rb new file mode 100644 index 000000000..8bc8b0135 --- /dev/null +++ b/test/notifiers/gpx_import_success_notifier_test.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require "test_helper" + +class GpxImportSuccessNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("gpx_import_success" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("gpx_import_success" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(trace_author) + trace = create(:trace, :user => trace_author) + + perform_enqueued_jobs do + GpxImportSuccessNotifier.with(:record => trace, :possible_points => 5).deliver + end + end +end diff --git a/test/notifiers/new_follower_notifier_test.rb b/test/notifiers/new_follower_notifier_test.rb new file mode 100644 index 000000000..d6ef0c4a1 --- /dev/null +++ b/test/notifiers/new_follower_notifier_test.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require "test_helper" + +class NewFollowerNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("new_follower" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("new_follower" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(followee) + follow = create(:follow, :following => followee) + perform_enqueued_jobs do + NewFollowerNotifier.with(:record => follow).deliver + end + end +end diff --git a/test/notifiers/note_comment_notifier_test.rb b/test/notifiers/note_comment_notifier_test.rb new file mode 100644 index 000000000..8249aff6a --- /dev/null +++ b/test/notifiers/note_comment_notifier_test.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "test_helper" + +class NoteCommentNotifierTest < ActiveSupport::TestCase + def setup + ActionMailer::Base.deliveries.clear + end + + def test_send_email_when_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("note_comment" => ["email"]) + + trigger_notification(candidate_recipient) + + email = ActionMailer::Base.deliveries.first + assert_equal [candidate_recipient.email], email.to + end + + def test_do_not_send_email_when_not_subscribed + candidate_recipient = create(:user) + candidate_recipient.notification_preferences.update("note_comment" => []) + + trigger_notification(candidate_recipient) + + assert_empty ActionMailer::Base.deliveries + end + + private + + def trigger_notification(note_author) + note = create(:note, :author => note_author) + create(:note_subscription, :note => note, :user => note_author) + + comment_author = create(:user) + note_comment = create(:note_comment, :author => comment_author, :note => note) + + perform_enqueued_jobs do + Nominatim.stub :describe_location, nil do + NoteCommentNotifier.with(:record => note_comment).deliver + end + end + end +end diff --git a/test/system/notification_preferences_test.rb b/test/system/notification_preferences_test.rb index 4bffdb47b..2471616c1 100644 --- a/test/system/notification_preferences_test.rb +++ b/test/system/notification_preferences_test.rb @@ -4,6 +4,8 @@ require "application_system_test_case" class NotificationPreferencesTest < ApplicationSystemTestCase test "toggling preferences" do + ActionMailer::Base.deliveries.clear + user = create(:user) sign_in_as(user) @@ -11,11 +13,26 @@ class NotificationPreferencesTest < ApplicationSystemTestCase assert_selector ".notification_preferences input:checked", :count => 7 + follow1 = create(:follow, :following => user) + perform_enqueued_jobs do + NewFollowerNotifier.with(:record => follow1).deliver + end + email = ActionMailer::Base.deliveries.first + assert_equal 1, email.to.count + assert_equal user.email, email.to.first + ActionMailer::Base.deliveries.clear + uncheck "user_notification_preferences_new_follower_email" uncheck "user_notification_preferences_gpx_import_success_email" click_on "Update Preferences" assert_selector ".notification_preferences input:checked", :count => 5 assert_selector "input#user_notification_preferences_new_follower_email:not(checked)" + + follow2 = create(:follow, :following => user) + perform_enqueued_jobs do + NewFollowerNotifier.with(:record => follow2).deliver + end + assert_empty ActionMailer::Base.deliveries end end -- 2.39.5