2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe NotificationEmailer do
|
2016-12-21 23:29:34 -05:00
|
|
|
before do
|
2020-07-24 05:16:52 -04:00
|
|
|
freeze_time
|
2016-12-21 23:29:34 -05:00
|
|
|
NotificationEmailer.enable
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
2016-01-18 19:39:57 -05:00
|
|
|
|
2015-09-23 01:24:30 -04:00
|
|
|
# something is off with fabricator
|
2015-11-24 10:58:26 -05:00
|
|
|
def create_notification(type, user = nil)
|
2015-09-23 01:24:30 -04:00
|
|
|
user ||= Fabricate(:user)
|
2016-07-07 12:23:19 -04:00
|
|
|
Notification.create(data: "{\"a\": 1}",
|
|
|
|
user: user,
|
|
|
|
notification_type: Notification.types[type],
|
|
|
|
topic: topic,
|
|
|
|
post_number: post.post_number)
|
2015-09-23 01:24:30 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
shared_examples "enqueue" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "enqueues a job for the email" do
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: NotificationEmailer::EmailUser.notification_params(notification, type),
|
2021-01-07 11:49:49 -05:00
|
|
|
at: no_delay ? Time.zone.now : Time.zone.now + delay
|
2020-07-24 05:16:52 -04:00
|
|
|
) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2013-08-26 00:55:35 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with an inactive user" do
|
2015-11-24 10:58:26 -05:00
|
|
|
before { notification.user.active = false }
|
|
|
|
|
|
|
|
it "doesn't enqueue a job" do
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2015-11-24 10:58:26 -05:00
|
|
|
end
|
|
|
|
|
2020-03-31 22:12:51 -04:00
|
|
|
it "enqueues a job if the user is staged for non-linked and non-quoted types" do
|
2015-11-24 10:58:26 -05:00
|
|
|
notification.user.staged = true
|
2020-07-24 05:16:52 -04:00
|
|
|
|
2020-03-31 22:12:51 -04:00
|
|
|
if type == :user_linked || type == :user_quoted
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_not_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: { type: type }
|
|
|
|
) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2020-03-31 22:12:51 -04:00
|
|
|
else
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: NotificationEmailer::EmailUser.notification_params(notification, type),
|
2021-01-07 11:49:49 -05:00
|
|
|
at: no_delay ? Time.zone.now : Time.zone.now + delay
|
2020-07-24 05:16:52 -04:00
|
|
|
) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2020-03-31 22:12:51 -04:00
|
|
|
end
|
2015-11-24 10:58:26 -05:00
|
|
|
end
|
2017-08-28 17:32:07 -04:00
|
|
|
|
2020-03-31 22:12:51 -04:00
|
|
|
it "enqueues a job if the user is staged even if site requires user approval for non-linked and non-quoted typed" do
|
2017-08-28 17:32:07 -04:00
|
|
|
notification.user.staged = true
|
2020-03-31 22:12:51 -04:00
|
|
|
SiteSetting.must_approve_users = true
|
2020-07-24 05:16:52 -04:00
|
|
|
|
2020-03-31 22:12:51 -04:00
|
|
|
if type == :user_linked || type == :user_quoted
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_not_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: { type: type }
|
|
|
|
) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2020-03-31 22:12:51 -04:00
|
|
|
else
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: NotificationEmailer::EmailUser.notification_params(notification, type),
|
2021-01-07 11:49:49 -05:00
|
|
|
at: no_delay ? Time.zone.now : Time.zone.now + delay
|
2020-07-24 05:16:52 -04:00
|
|
|
) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2020-03-31 22:12:51 -04:00
|
|
|
end
|
2017-08-28 17:32:07 -04:00
|
|
|
end
|
2016-06-16 12:55:47 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with an active but unapproved user" do
|
2016-06-16 12:55:47 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
notification.user.approved = false
|
|
|
|
notification.user.active = true
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-06-16 12:55:47 -04:00
|
|
|
it "doesn't enqueue a job" do
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2016-06-16 12:55:47 -04:00
|
|
|
end
|
2015-08-31 01:54:37 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a small action" do
|
2016-01-18 19:39:57 -05:00
|
|
|
|
|
|
|
it "doesn't enqueue a job" do
|
|
|
|
Post.any_instance.expects(:post_type).returns(Post.types[:small_action])
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2016-01-18 19:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
shared_examples "enqueue_public" do
|
|
|
|
include_examples "enqueue"
|
2013-02-27 15:38:44 -05:00
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
it "doesn't enqueue a job if the user has mention emails disabled" do
|
2019-03-15 10:55:11 -04:00
|
|
|
notification.user.user_option.update_columns(email_level: UserOption.email_level_types[:never])
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
2021-01-07 11:49:49 -05:00
|
|
|
NotificationEmailer.process_notification(notification, no_delay: no_delay)
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2013-02-27 15:38:44 -05:00
|
|
|
end
|
2015-11-24 10:58:26 -05:00
|
|
|
end
|
2013-02-27 15:38:44 -05:00
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
shared_examples "enqueue_private" do
|
|
|
|
include_examples "enqueue"
|
2013-02-27 15:38:44 -05:00
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
it "doesn't enqueue a job if the user has private message emails disabled" do
|
2019-03-15 10:55:11 -04:00
|
|
|
notification.user.user_option.update_columns(email_messages_level: UserOption.email_level_types[:never])
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
|
|
|
NotificationEmailer.process_notification(notification)
|
|
|
|
end
|
2015-08-31 01:54:37 -04:00
|
|
|
end
|
2016-02-01 13:12:10 -05:00
|
|
|
|
2013-02-27 15:38:44 -05:00
|
|
|
end
|
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
[true, false].each do |no_delay|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_mentioned' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_mentioned }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:mentioned) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
2020-07-24 05:16:52 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
it "enqueue a delayed job for users that are online" do
|
|
|
|
notification.user.last_seen_at = 1.minute.ago
|
|
|
|
|
|
|
|
expect_enqueued_with(
|
|
|
|
job: :user_email,
|
|
|
|
args: NotificationEmailer::EmailUser.notification_params(notification, type),
|
|
|
|
at: Time.zone.now + delay
|
|
|
|
) do
|
|
|
|
NotificationEmailer.process_notification(notification)
|
|
|
|
end
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_replied' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_replied }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:replied) }
|
2015-08-31 01:54:37 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_quoted' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_quoted }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:quoted) }
|
2015-08-31 01:54:37 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_linked' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_linked }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:linked) }
|
2016-02-16 12:29:23 -05:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2016-02-16 12:29:23 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_posted' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_posted }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:posted) }
|
2015-08-31 01:54:37 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-12-29 09:36:53 -05:00
|
|
|
context 'with user_watching_category_or_tag' do
|
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_posted }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:watching_category_or_tag) }
|
|
|
|
|
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_private_message' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_private_message }
|
|
|
|
let(:delay) { SiteSetting.personal_email_time_window_seconds }
|
|
|
|
let!(:notification) { create_notification(:private_message) }
|
2015-06-09 03:35:26 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_private"
|
2016-02-01 13:12:10 -05:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
it "doesn't enqueue a job for a small action" do
|
|
|
|
notification.data_hash["original_post_type"] = Post.types[:small_action]
|
2020-07-24 05:16:52 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
|
|
|
|
NotificationEmailer.process_notification(notification)
|
|
|
|
end
|
2020-07-24 05:16:52 -04:00
|
|
|
end
|
2016-02-01 13:12:10 -05:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
end
|
2015-06-09 03:35:26 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_invited_to_private_message' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_invited_to_private_message }
|
|
|
|
let(:delay) { SiteSetting.personal_email_time_window_seconds }
|
|
|
|
let!(:notification) { create_notification(:invited_to_private_message) }
|
2015-08-31 01:54:37 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2015-06-09 03:35:26 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with user_invited_to_topic' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_invited_to_topic }
|
|
|
|
let(:delay) { SiteSetting.personal_email_time_window_seconds }
|
|
|
|
let!(:notification) { create_notification(:invited_to_topic) }
|
2016-07-07 12:23:19 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2016-07-07 12:23:19 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when watching the first post' do
|
2021-01-07 11:49:49 -05:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :user_watching_first_post }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:watching_first_post) }
|
2015-08-31 01:54:37 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2021-04-12 11:08:23 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with post_approved' do
|
2021-04-12 11:08:23 -04:00
|
|
|
let(:no_delay) { no_delay }
|
|
|
|
let(:type) { :post_approved }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:post_approved) }
|
|
|
|
|
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
2015-03-30 14:36:47 -04:00
|
|
|
end
|
2022-12-29 09:36:53 -05:00
|
|
|
|
|
|
|
it "has translations for each sendable notification type" do
|
|
|
|
notification = create_notification(:mentioned)
|
|
|
|
email_user = NotificationEmailer::EmailUser.new(notification, no_delay: true)
|
|
|
|
subkeys = ["title", "subject_template", "text_body_template"]
|
|
|
|
|
|
|
|
# some notification types need special handling
|
|
|
|
replace_keys = {
|
|
|
|
"post_approved" => ["post_approved"],
|
|
|
|
"private_message" => ["user_posted"],
|
|
|
|
"invited_to_private_message" => [
|
|
|
|
"user_invited_to_private_message_pm",
|
|
|
|
"user_invited_to_private_message_pm_group",
|
|
|
|
"user_invited_to_private_message_pm_staged"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
Notification.types.keys.each do |notification_type|
|
|
|
|
if email_user.respond_to?(notification_type)
|
|
|
|
type_keys = replace_keys[notification_type.to_s] || ["user_#{notification_type}"]
|
|
|
|
|
|
|
|
type_keys.each do |type_key|
|
|
|
|
subkeys.each do |subkey|
|
|
|
|
key = "user_notifications.#{type_key}.#{subkey}"
|
|
|
|
expect(I18n.exists?(key)).to eq(true), "missing translation: #{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-26 00:55:35 -04:00
|
|
|
end
|