discourse/spec/models/user_email_observer_spec.rb

187 lines
8.0 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require 'spec_helper'
describe UserEmailObserver do
2015-09-23 01:24:30 -04:00
# something is off with fabricator
def create_notification(type=nil, user=nil)
user ||= Fabricate(:user)
type ||= Notification.types[:mentioned]
Notification.create(data: '', user: user, notification_type: type)
end
2013-02-05 14:16:51 -05:00
2015-09-23 01:24:30 -04:00
context 'user_mentioned' do
let!(:notification) do
create_notification
end
2013-02-05 14:16:51 -05:00
it "enqueues a job for the email" do
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_mentioned, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
it "enqueue a delayed job for users that are online" do
2015-09-23 01:24:30 -04:00
notification.user.last_seen_at = 1.minute.ago
2013-02-05 14:16:51 -05:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_mentioned, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user has mention emails disabled" do
2015-09-23 01:24:30 -04:00
notification.user.expects(:email_direct?).returns(false)
2013-02-05 14:16:51 -05:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_mentioned)).never
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user account is deactivated" do
2015-09-23 01:24:30 -04:00
notification.user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_mentioned)).never
UserEmailObserver.send(:new).after_commit(notification)
end
2013-02-05 14:16:51 -05:00
end
context 'posted' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(9) }
let(:user) { notification.user }
it "enqueues a job for the email" do
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_posted, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user has mention emails disabled" do
user.expects(:email_direct?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_posted)).never
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_posted)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
2013-02-05 14:16:51 -05:00
context 'user_replied' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(2) }
let(:user) { notification.user }
2013-02-05 14:16:51 -05:00
it "enqueues a job for the email" do
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_replied, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user has mention emails disabled" do
user.expects(:email_direct?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_replied)).never
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_replied)).never
UserEmailObserver.send(:new).after_commit(notification)
end
2013-02-05 14:16:51 -05:00
end
context 'user_quoted' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(3) }
let(:user) { notification.user }
2013-02-05 14:16:51 -05:00
it "enqueues a job for the email" do
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_quoted, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user has mention emails disabled" do
user.expects(:email_direct?).returns(false)
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_quoted)).never
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_quoted)).never
UserEmailObserver.send(:new).after_commit(notification)
end
2013-02-05 14:16:51 -05:00
end
context 'email_user_invited_to_private_message' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(7) }
let(:user) { notification.user }
2013-02-05 14:16:51 -05:00
it "enqueues a job for the email" do
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_invited_to_private_message, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user has mention emails disabled" do
user.expects(:email_direct?).returns(false)
2013-08-26 21:52:09 -04:00
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_private_message)).never
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_private_message)).never
UserEmailObserver.send(:new).after_commit(notification)
end
2013-02-05 14:16:51 -05:00
end
context 'private_message' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(6) }
let(:user) { notification.user }
it "enqueues a job for the email" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_private_message, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user has private message emails disabled" do
user.expects(:email_private_messages?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_private_message)).never
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_private_message)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
context 'user_invited_to_topic' do
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(13) }
let(:user) { notification.user }
it "enqueues a job for the email" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_invited_to_topic, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user has mention emails disabled" do
user.expects(:email_direct?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_topic)).never
UserEmailObserver.send(:new).after_commit(notification)
end
it "doesn't enqueue an email if the user account is deactivated" do
user.active = false
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_topic)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
end