2015-10-11 04:41:23 -05:00
|
|
|
require "rails_helper"
|
2014-11-24 10:40:21 -06:00
|
|
|
|
|
|
|
describe Jobs::NotifyMailingListSubscribers do
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
let(:mailing_list_user) { Fabricate(:user) }
|
2016-07-05 05:20:07 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 1) }
|
2016-07-05 05:20:07 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
|
|
|
|
|
|
|
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 21:35:22 -05:00
|
|
|
end
|
2016-12-12 08:28:26 -06:00
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06: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 15:51:32 -05:00
|
|
|
|
|
|
|
it "triggers :notify_mailing_list_subscribers" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
2017-07-27 20:20:09 -05:00
|
|
|
expect(events).to include(event_name: :notify_mailing_list_subscribers, params: [[mailing_list_user], post])
|
2017-07-19 15:51:32 -05:00
|
|
|
end
|
2016-12-12 08:28:26 -06:00
|
|
|
end
|
2016-03-22 23:08:34 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "when mailing list mode is globally disabled" do
|
|
|
|
before { SiteSetting.disable_mailing_list_mode = true }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-03-22 23:08:34 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "when mailing list mode is globally enabled" do
|
|
|
|
before { SiteSetting.disable_mailing_list_mode = false }
|
2016-03-22 23:08:34 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with an invalid post_id" do
|
2017-05-08 14:08:29 -05:00
|
|
|
before { post.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
2016-03-22 23:08:34 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with a deleted post" do
|
|
|
|
before { post.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-07-05 05:20:07 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with a user_deleted post" do
|
|
|
|
before { post.update(user_deleted: true) }
|
|
|
|
include_examples "no emails"
|
2016-07-05 05:20:07 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with a deleted topic" do
|
|
|
|
before { post.topic.update(deleted_at: Time.now) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2016-03-22 23:08:34 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with a valid post from another user" do
|
2016-03-22 23:08:34 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an inactive user" do
|
|
|
|
before { mailing_list_user.update(active: false) }
|
|
|
|
include_examples "no emails"
|
2016-03-22 23:08:34 -05:00
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
context "to a silenced user" do
|
2017-11-13 12:41:36 -06:00
|
|
|
before { mailing_list_user.update(silenced_till: 1.year.from_now) }
|
2016-12-12 08:28:26 -06:00
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2015-08-21 13:39:21 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to a suspended user" do
|
|
|
|
before { mailing_list_user.update(suspended_till: 1.day.from_now) }
|
|
|
|
include_examples "no emails"
|
2016-10-05 18:28:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an anonymous user" do
|
|
|
|
let(:mailing_list_user) { Fabricate(:anonymous) }
|
|
|
|
include_examples "no emails"
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
2016-05-21 08:17:54 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has disabled mailing list mode" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode: false) }
|
|
|
|
include_examples "no emails"
|
2016-10-05 18:28:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has frequency set to 'always'" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 1) }
|
|
|
|
include_examples "one email"
|
2016-10-05 18:28:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has frequency set to 'no echo'" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 2) }
|
|
|
|
include_examples "one email"
|
2016-10-05 18:28:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "from a muted user" do
|
|
|
|
before { MutedUser.create(user: mailing_list_user, muted_user: user) }
|
|
|
|
include_examples "no emails"
|
2016-05-21 08:17:54 -05:00
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "from a muted topic" do
|
|
|
|
before { TopicUser.create(user: mailing_list_user, topic: post.topic, notification_level: TopicUser.notification_levels[:muted]) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "from a muted category" do
|
|
|
|
before { CategoryUser.create(user: mailing_list_user, category: post.topic.category, notification_level: CategoryUser.notification_levels[:muted]) }
|
|
|
|
include_examples "no emails"
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "max emails per day was reached" do
|
|
|
|
before { SiteSetting.max_emails_per_day_per_user = 2 }
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
it "doesn't send any emails" do
|
|
|
|
(SiteSetting.max_emails_per_day_per_user + 1).times {
|
|
|
|
mailing_list_user.email_logs.create(email_type: 'foobar', to_address: mailing_list_user.email)
|
|
|
|
}
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).never
|
2015-08-21 13:39:21 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
expect(EmailLog.where(user: mailing_list_user, skipped: true).count).to eq(1)
|
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "bounce score was reached" do
|
2015-08-21 13:39:21 -05:00
|
|
|
|
2016-12-12 08:28:26 -06: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
|
|
|
|
|
|
|
|
expect(EmailLog.where(user: mailing_list_user, skipped: true).count).to eq(1)
|
|
|
|
end
|
2015-08-21 13:39:21 -05:00
|
|
|
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
2016-12-12 08:28:26 -06:00
|
|
|
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "with a valid post from same user" do
|
|
|
|
let(:post) { Fabricate(:post, user: mailing_list_user) }
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has frequency set to 'daily'" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 0) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2015-08-21 13:39:21 -05:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has frequency set to 'always'" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 1) }
|
|
|
|
include_examples "one email"
|
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
|
2016-12-12 08:28:26 -06:00
|
|
|
context "to an user who has frequency set to 'no echo'" do
|
|
|
|
before { mailing_list_user.user_option.update(mailing_list_mode_frequency: 2) }
|
|
|
|
include_examples "no emails"
|
|
|
|
end
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
2016-12-12 08:28:26 -06:00
|
|
|
|
2014-11-24 10:40:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|