2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Jobs::NotifyMailingListSubscribers do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:mailing_list_user) { Fabricate(:user) }
|
2016-07-05 06:20:07 -04:00
|
|
|
|
2020-02-19 15:14:42 -05:00
|
|
|
before do
|
|
|
|
mailing_list_user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 1)
|
|
|
|
end
|
2016-12-12 09:28:26 -05:00
|
|
|
before { SiteSetting.tagging_enabled = true }
|
2016-07-05 06:20:07 -04:00
|
|
|
|
2020-02-19 15:14:42 -05:00
|
|
|
fab!(:tag) { Fabricate(:tag) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, tags: [tag]) }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2020-02-19 15:14:42 -05:00
|
|
|
fab!(:post) { Fabricate(:post, topic: topic, user: user) }
|
2016-12-12 09:28:26 -05:00
|
|
|
|
|
|
|
shared_examples "no emails" do
|
|
|
|
it "doesn't send any emails" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
2016-05-26 22:35:22 -04:00
|
|
|
end
|
2016-12-12 09:28:26 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
shared_examples "one email" do
|
|
|
|
it "sends the email" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).once
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :notify_mailing_list_subscribers" do
|
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events do
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
|
|
|
expect(events).to include(
|
|
|
|
event_name: :notify_mailing_list_subscribers,
|
|
|
|
params: [[mailing_list_user], post],
|
|
|
|
)
|
|
|
|
end
|
2016-12-12 09:28:26 -05:00
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "when mailing list mode is globally disabled" do
|
|
|
|
before { SiteSetting.disable_mailing_list_mode = true }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "when mailing list mode is globally enabled" do
|
|
|
|
before { SiteSetting.disable_mailing_list_mode = false }
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2017-12-12 23:13:17 -05:00
|
|
|
context "when site requires approval and user is not approved" do
|
|
|
|
before do
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
SiteSetting.must_approve_users = true
|
2019-04-16 14:42:47 -04:00
|
|
|
|
|
|
|
User.update_all(approved: false)
|
2017-12-12 23:13:17 -05:00
|
|
|
end
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with an invalid post_id" do
|
2017-05-08 15:08:29 -04:00
|
|
|
before { post.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
2016-03-23 00:08:34 -04:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with a deleted post" do
|
|
|
|
before { post.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-07-05 06:20:07 -04:00
|
|
|
|
2020-09-02 18:58:25 -04:00
|
|
|
context "with a empty post" do
|
|
|
|
before { post.update_columns(raw: "") }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with a user_deleted post" do
|
|
|
|
before { post.update(user_deleted: true) }
|
|
|
|
include_examples "no emails"
|
2016-07-05 06:20:07 -04:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with a deleted topic" do
|
|
|
|
before { post.topic.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2021-08-26 01:16:35 -04:00
|
|
|
context "with a private message" do
|
|
|
|
before do
|
|
|
|
post.topic.update!(archetype: Archetype.private_message, category: nil)
|
|
|
|
TopicAllowedUser.create(topic: post.topic, user: mailing_list_user)
|
|
|
|
post.topic.reload
|
|
|
|
end
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with a valid post from another user" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an inactive user" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.update(active: false) }
|
|
|
|
include_examples "no emails"
|
2016-03-23 00:08:34 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to a silenced user" do
|
2017-11-13 13:41:36 -05:00
|
|
|
before { mailing_list_user.update(silenced_till: 1.year.from_now) }
|
2016-12-12 09:28:26 -05:00
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to a suspended user" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.update(suspended_till: 1.day.from_now) }
|
|
|
|
include_examples "no emails"
|
2016-10-05 19:28:58 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an anonymous user" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:mailing_list_user) { Fabricate(:anonymous) }
|
2016-12-12 09:28:26 -05:00
|
|
|
include_examples "no emails"
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
2016-05-21 09:17:54 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has disabled mailing list mode" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode: false) }
|
|
|
|
include_examples "no emails"
|
2016-10-05 19:28:58 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has frequency set to 'always'" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 1) }
|
|
|
|
include_examples "one email"
|
2016-10-05 19:28:58 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has frequency set to 'no echo'" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 2) }
|
|
|
|
include_examples "one email"
|
2016-10-05 19:28:58 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when from a muted user" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { MutedUser.create(user: mailing_list_user, muted_user: user) }
|
|
|
|
include_examples "no emails"
|
2016-05-21 09:17:54 -04:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when from an ignored user" do
|
2019-03-21 07:15:34 -04:00
|
|
|
before { Fabricate(:ignored_user, user: mailing_list_user, ignored_user: user) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when from a muted topic" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before do
|
|
|
|
TopicUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
topic: post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2016-12-12 09:28:26 -05:00
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when from a muted category" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before do
|
|
|
|
CategoryUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
category: post.topic.category,
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2016-12-12 09:28:26 -05:00
|
|
|
include_examples "no emails"
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with mute all categories by default setting" do
|
2020-12-15 17:30:21 -05:00
|
|
|
before { SiteSetting.mute_all_categories_by_default = true }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with mute all categories by default setting but user is watching category" do
|
2020-12-15 17:30:21 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
|
|
|
CategoryUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
category: post.topic.category,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
include_examples "one email"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with mute all categories by default setting but user is watching tag" do
|
2020-12-15 17:30:21 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
|
|
|
TagUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
tag: tag,
|
|
|
|
notification_level: TagUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
include_examples "one email"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with mute all categories by default setting but user is watching topic" do
|
2020-12-15 17:30:21 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
|
|
|
TopicUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
topic: post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
include_examples "one email"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when from a muted tag" do
|
2020-02-19 15:14:42 -05:00
|
|
|
before do
|
|
|
|
TagUser.create(
|
|
|
|
user: mailing_list_user,
|
|
|
|
tag: tag,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2020-02-19 15:14:42 -05:00
|
|
|
include_examples "no emails"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when max emails per day was reached" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { SiteSetting.max_emails_per_day_per_user = 2 }
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
it "doesn't send any emails" do
|
|
|
|
(SiteSetting.max_emails_per_day_per_user + 1).times do
|
|
|
|
mailing_list_user.email_logs.create(
|
|
|
|
email_type: "foobar",
|
|
|
|
to_address: mailing_list_user.email,
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2018-08-08 04:25:00 -04:00
|
|
|
expect do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).never
|
|
|
|
|
|
|
|
2.times { Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id) }
|
2018-08-20 22:04:35 -04:00
|
|
|
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: Fabricate(:post, user: user).id)
|
2018-08-08 04:25:00 -04:00
|
|
|
end.to change { SkippedEmailLog.count }.by(1)
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2018-07-24 00:55:43 -04:00
|
|
|
expect(
|
|
|
|
SkippedEmailLog.exists?(
|
|
|
|
email_type: "mailing_list",
|
|
|
|
user: mailing_list_user,
|
|
|
|
post: post,
|
|
|
|
to_address: mailing_list_user.email,
|
|
|
|
reason_type: SkippedEmailLog.reason_types[:exceeded_emails_limit],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2018-07-24 00:55:43 -04:00
|
|
|
).to eq(true)
|
2018-08-20 22:04:35 -04:00
|
|
|
|
2018-08-20 23:14:43 -04:00
|
|
|
freeze_time(Time.zone.now.tomorrow + 1.second)
|
2018-08-20 22:04:35 -04:00
|
|
|
|
|
|
|
expect do
|
2018-08-20 23:14:43 -04:00
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).once
|
|
|
|
|
2018-08-20 22:04:35 -04:00
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
2022-07-19 10:03:03 -04:00
|
|
|
end.not_to change { SkippedEmailLog.count }
|
2016-12-12 09:28:26 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when bounce score was reached" do
|
2016-12-12 09:28:26 -05:00
|
|
|
it "doesn't send any emails" do
|
|
|
|
mailing_list_user.user_stat.update(bounce_score: SiteSetting.bounce_score_threshold + 1)
|
|
|
|
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).never
|
|
|
|
|
2018-07-24 00:55:43 -04:00
|
|
|
expect(
|
|
|
|
SkippedEmailLog.exists?(
|
|
|
|
email_type: "mailing_list",
|
|
|
|
user: mailing_list_user,
|
|
|
|
post: post,
|
|
|
|
to_address: mailing_list_user.email,
|
|
|
|
reason_type: SkippedEmailLog.reason_types[:exceeded_bounces_limit],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2018-07-24 00:55:43 -04:00
|
|
|
).to eq(true)
|
2016-12-12 09:28:26 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:28:26 -05:00
|
|
|
context "with a valid post from same user" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:post) { Fabricate(:post, user: mailing_list_user) }
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has frequency set to 'daily'" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 0) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has frequency set to 'always'" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 1) }
|
|
|
|
include_examples "one email"
|
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when to an user who has frequency set to 'no echo'" do
|
2016-12-12 09:28:26 -05:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 2) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|