discourse/spec/models/user_email_observer_spec.rb

114 lines
3.5 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, user=nil)
2015-09-23 01:24:30 -04:00
user ||= Fabricate(:user)
Notification.create(data: '', user: user, notification_type: type)
end
2013-02-05 14:16:51 -05:00
shared_examples "enqueue" do
2013-02-05 14:16:51 -05:00
it "enqueues a job for the email" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
context "inactive user" do
2013-02-05 14:16:51 -05:00
before { notification.user.active = false }
it "doesn't enqueue a job" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
it "enqueues a job if the user is staged" do
notification.user.staged = true
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
2013-02-05 14:16:51 -05:00
end
2013-02-05 14:16:51 -05:00
end
shared_examples "enqueue_public" do
include_examples "enqueue"
it "doesn't enqueue a job if the user has mention emails disabled" do
notification.user.expects(:email_direct?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
shared_examples "enqueue_private" do
include_examples "enqueue"
it "doesn't enqueue a job if the user has private message emails disabled" do
notification.user.expects(:email_private_messages?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
context 'user_mentioned' do
let(:type) { :user_mentioned }
let!(:notification) { create_notification(1) }
2013-02-05 14:16:51 -05:00
include_examples "enqueue_public"
2013-02-05 14:16:51 -05:00
it "enqueue a delayed job for users that are online" do
notification.user.last_seen_at = 1.minute.ago
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
2013-02-05 14:16:51 -05:00
end
end
2013-02-05 14:16:51 -05:00
context 'user_replied' do
let(:type) { :user_replied }
let!(:notification) { create_notification(2) }
include_examples "enqueue_public"
2013-02-05 14:16:51 -05:00
end
context 'user_quoted' do
let(:type) { :user_quoted }
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(3) }
include_examples "enqueue_public"
2013-02-05 14:16:51 -05:00
end
context 'user_posted' do
let(:type) { :user_posted }
let!(:notification) { create_notification(9) }
include_examples "enqueue_public"
2013-02-05 14:16:51 -05:00
end
context 'user_private_message' do
let(:type) { :user_private_message }
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(6) }
include_examples "enqueue_private"
end
context 'user_invited_to_private_message' do
let(:type) { :user_invited_to_private_message }
let!(:notification) { create_notification(7) }
include_examples "enqueue_public"
end
context 'user_invited_to_topic' do
let(:type) { :user_invited_to_topic }
2015-09-23 01:24:30 -04:00
let!(:notification) { create_notification(13) }
include_examples "enqueue_public"
end
end