2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe UserEmailObserver do
|
|
|
|
|
2016-01-19 08:39:57 +08:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
2015-09-23 13:24:30 +08:00
|
|
|
# something is off with fabricator
|
2015-11-24 23:58:26 +08:00
|
|
|
def create_notification(type, user=nil)
|
2015-09-23 13:24:30 +08:00
|
|
|
user ||= Fabricate(:user)
|
2016-07-08 00:23:19 +08:00
|
|
|
Notification.create(data: "{\"a\": 1}",
|
|
|
|
user: user,
|
|
|
|
notification_type: Notification.types[type],
|
|
|
|
topic: topic,
|
|
|
|
post_number: post.post_number)
|
2015-09-23 13:24:30 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
shared_examples "enqueue" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "enqueues a job for the email" do
|
2016-01-27 09:19:49 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-08-26 12:55:35 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context "inactive user" do
|
|
|
|
before { notification.user.active = false }
|
|
|
|
|
|
|
|
it "doesn't enqueue a job" do
|
2015-11-28 02:09:35 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-27 09:19:49 +08:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-11-24 23:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "enqueues a job if the user is staged" do
|
|
|
|
notification.user.staged = true
|
2016-01-27 09:19:49 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-11-24 23:58:26 +08:00
|
|
|
end
|
2016-06-17 00:55:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "active but unapproved user" do
|
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
notification.user.approved = false
|
|
|
|
notification.user.active = true
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-17 00:55:47 +08:00
|
|
|
it "doesn't enqueue a job" do
|
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
|
|
|
UserEmailObserver.process_notification(notification)
|
|
|
|
end
|
2015-08-31 13:54:37 +08:00
|
|
|
end
|
|
|
|
|
2016-01-19 08:39:57 +08:00
|
|
|
context "small action" do
|
|
|
|
|
|
|
|
it "doesn't enqueue a job" do
|
|
|
|
Post.any_instance.expects(:post_type).returns(Post.types[:small_action])
|
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-27 09:19:49 +08:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2016-01-19 08:39:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
shared_examples "enqueue_public" do
|
|
|
|
include_examples "enqueue"
|
2013-02-28 04:38:44 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
it "doesn't enqueue a job if the user has mention emails disabled" do
|
2016-02-17 12:46:19 +08:00
|
|
|
notification.user.user_option.update_columns(email_direct: false)
|
2015-11-28 02:09:35 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-27 09:19:49 +08:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-02-28 04:38:44 +08:00
|
|
|
end
|
2015-11-24 23:58:26 +08:00
|
|
|
end
|
2013-02-28 04:38:44 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
shared_examples "enqueue_private" do
|
|
|
|
include_examples "enqueue"
|
2013-02-28 04:38:44 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
it "doesn't enqueue a job if the user has private message emails disabled" do
|
2016-02-17 12:46:19 +08:00
|
|
|
notification.user.user_option.update_columns(email_private_messages: false)
|
2015-11-28 02:09:35 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-27 09:19:49 +08:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-08-31 13:54:37 +08:00
|
|
|
end
|
2016-02-02 02:12:10 +08:00
|
|
|
|
2013-02-28 04:38:44 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context 'user_mentioned' do
|
|
|
|
let(:type) { :user_mentioned }
|
2015-11-28 02:09:35 +08:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:mentioned) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
it "enqueue a delayed job for users that are online" do
|
|
|
|
notification.user.last_seen_at = 1.minute.ago
|
2016-01-27 09:19:49 +08:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context 'user_replied' do
|
|
|
|
let(:type) { :user_replied }
|
2015-11-28 02:09:35 +08:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:replied) }
|
2015-08-31 13:54:37 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'user_quoted' do
|
2015-11-24 23:58:26 +08:00
|
|
|
let(:type) { :user_quoted }
|
2015-11-28 02:09:35 +08:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:quoted) }
|
2015-08-31 13:54:37 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-02-17 01:29:23 +08:00
|
|
|
context 'user_linked' do
|
|
|
|
let(:type) { :user_linked }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:linked) }
|
2016-02-17 01:29:23 +08:00
|
|
|
|
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context 'user_posted' do
|
|
|
|
let(:type) { :user_posted }
|
2015-11-28 02:09:35 +08:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:posted) }
|
2015-08-31 13:54:37 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context 'user_private_message' do
|
|
|
|
let(:type) { :user_private_message }
|
2016-02-05 00:22:16 +08:00
|
|
|
let(:delay) { SiteSetting.private_email_time_window_seconds }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:private_message) }
|
2015-06-09 15:35:26 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_private"
|
2016-02-02 02:12:10 +08:00
|
|
|
|
|
|
|
it "doesn't enqueue a job for a small action" do
|
|
|
|
notification.data_hash["original_post_type"] = Post.types[:small_action]
|
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
|
|
|
UserEmailObserver.process_notification(notification)
|
|
|
|
end
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
end
|
2015-06-09 15:35:26 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
context 'user_invited_to_private_message' do
|
|
|
|
let(:type) { :user_invited_to_private_message }
|
2016-02-05 00:22:16 +08:00
|
|
|
let(:delay) { SiteSetting.private_email_time_window_seconds }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:invited_to_private_message) }
|
2015-08-31 13:54:37 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2015-06-09 15:35:26 +08:00
|
|
|
end
|
|
|
|
|
2015-03-31 02:36:47 +08:00
|
|
|
context 'user_invited_to_topic' do
|
2015-11-24 23:58:26 +08:00
|
|
|
let(:type) { :user_invited_to_topic }
|
2016-02-05 00:22:16 +08:00
|
|
|
let(:delay) { SiteSetting.private_email_time_window_seconds }
|
2016-07-08 00:23:19 +08:00
|
|
|
let!(:notification) { create_notification(:invited_to_topic) }
|
|
|
|
|
|
|
|
include_examples "enqueue_public"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'watching the first post' do
|
|
|
|
let(:type) { :user_watching_first_post }
|
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
|
|
|
let!(:notification) { create_notification(:watching_first_post) }
|
2015-08-31 13:54:37 +08:00
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
include_examples "enqueue_public"
|
2015-03-31 02:36:47 +08:00
|
|
|
end
|
|
|
|
|
2013-08-26 12:55:35 +08:00
|
|
|
end
|