2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe UserEmailObserver do
|
|
|
|
|
2016-01-18 18:39:57 -06:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
2015-09-23 00:24:30 -05:00
|
|
|
# something is off with fabricator
|
2015-11-24 09:58:26 -06:00
|
|
|
def create_notification(type, user=nil)
|
2015-09-23 00:24:30 -05:00
|
|
|
user ||= Fabricate(:user)
|
2016-01-26 19:19:49 -06:00
|
|
|
Notification.create(data: "{\"a\": 1}", user: user, notification_type: type, topic: topic, post_number: post.post_number)
|
2015-09-23 00:24:30 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
shared_examples "enqueue" do
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-01-26 19:19:49 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
it "enqueues a job for the email" do
|
2016-01-26 19:19:49 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-08-25 23:55:35 -05:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context "inactive user" do
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
before { notification.user.active = false }
|
|
|
|
|
|
|
|
it "doesn't enqueue a job" do
|
2015-11-27 12:09:35 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-26 19:19:49 -06:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-11-24 09:58:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "enqueues a job if the user is staged" do
|
|
|
|
notification.user.staged = true
|
2016-01-26 19:19:49 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-11-24 09:58:26 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-08-31 00:54:37 -05:00
|
|
|
end
|
|
|
|
|
2016-01-18 18:39:57 -06: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-26 19:19:49 -06:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2016-01-18 18:39:57 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
shared_examples "enqueue_public" do
|
|
|
|
include_examples "enqueue"
|
2013-02-27 14:38:44 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
it "doesn't enqueue a job if the user has mention emails disabled" do
|
|
|
|
notification.user.expects(:email_direct?).returns(false)
|
2015-11-27 12:09:35 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-26 19:19:49 -06:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-02-27 14:38:44 -06:00
|
|
|
end
|
2015-11-24 09:58:26 -06:00
|
|
|
end
|
2013-02-27 14:38:44 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
shared_examples "enqueue_private" do
|
|
|
|
include_examples "enqueue"
|
2013-02-27 14:38:44 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
it "doesn't enqueue a job if the user has private message emails disabled" do
|
|
|
|
notification.user.expects(:email_private_messages?).returns(false)
|
2015-11-27 12:09:35 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
|
2016-01-26 19:19:49 -06:00
|
|
|
UserEmailObserver.process_notification(notification)
|
2015-08-31 00:54:37 -05:00
|
|
|
end
|
2013-02-27 14:38:44 -06:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context 'user_mentioned' do
|
|
|
|
let(:type) { :user_mentioned }
|
2015-11-27 12:09:35 -06:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2015-11-24 09:58:26 -06:00
|
|
|
let!(:notification) { create_notification(1) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
it "enqueue a delayed job for users that are online" do
|
|
|
|
notification.user.last_seen_at = 1.minute.ago
|
2016-01-26 19:19:49 -06:00
|
|
|
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
|
|
|
|
UserEmailObserver.process_notification(notification)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context 'user_replied' do
|
|
|
|
let(:type) { :user_replied }
|
2015-11-27 12:09:35 -06:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2015-11-24 09:58:26 -06:00
|
|
|
let!(:notification) { create_notification(2) }
|
2015-08-31 00:54:37 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'user_quoted' do
|
2015-11-24 09:58:26 -06:00
|
|
|
let(:type) { :user_quoted }
|
2015-11-27 12:09:35 -06:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2015-09-23 00:24:30 -05:00
|
|
|
let!(:notification) { create_notification(3) }
|
2015-08-31 00:54:37 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context 'user_posted' do
|
|
|
|
let(:type) { :user_posted }
|
2015-11-27 12:09:35 -06:00
|
|
|
let(:delay) { SiteSetting.email_time_window_mins.minutes }
|
2015-11-24 09:58:26 -06:00
|
|
|
let!(:notification) { create_notification(9) }
|
2015-08-31 00:54:37 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context 'user_private_message' do
|
|
|
|
let(:type) { :user_private_message }
|
2016-02-01 03:14:30 -06:00
|
|
|
let(:delay) { 20.seconds }
|
2015-09-23 00:24:30 -05:00
|
|
|
let!(:notification) { create_notification(6) }
|
2015-06-09 02:35:26 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_private"
|
|
|
|
end
|
2015-06-09 02:35:26 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
context 'user_invited_to_private_message' do
|
|
|
|
let(:type) { :user_invited_to_private_message }
|
2016-02-01 03:14:30 -06:00
|
|
|
let(:delay) { 20.seconds }
|
2015-11-24 09:58:26 -06:00
|
|
|
let!(:notification) { create_notification(7) }
|
2015-08-31 00:54:37 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2015-06-09 02:35:26 -05:00
|
|
|
end
|
|
|
|
|
2015-03-30 13:36:47 -05:00
|
|
|
context 'user_invited_to_topic' do
|
2015-11-24 09:58:26 -06:00
|
|
|
let(:type) { :user_invited_to_topic }
|
2016-02-01 03:14:30 -06:00
|
|
|
let(:delay) { 20.seconds }
|
2015-09-23 00:24:30 -05:00
|
|
|
let!(:notification) { create_notification(13) }
|
2015-08-31 00:54:37 -05:00
|
|
|
|
2015-11-24 09:58:26 -06:00
|
|
|
include_examples "enqueue_public"
|
2015-03-30 13:36:47 -05:00
|
|
|
end
|
|
|
|
|
2013-08-25 23:55:35 -05:00
|
|
|
end
|