discourse/spec/models/user_email_observer_spec.rb

161 lines
5.1 KiB
Ruby
Raw Normal View History

2016-04-01 05:33:25 +08:00
require 'rails_helper'
2013-02-06 03:16:51 +08:00
describe UserEmailObserver do
2016-04-01 05:33:25 +08:00
let(:topic) { Fabricate(:topic) }
let(:post) { Fabricate(:post, topic: topic) }
2016-04-01 05:33:25 +08:00
# something is off with fabricator
def create_notification(type, user=nil)
user ||= Fabricate(:user)
Notification.create(data: "{\"a\": 1}", user: user, notification_type: type, topic: topic, post_number: post.post_number)
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
shared_examples "enqueue" do
it "enqueues a job for the email" do
2016-04-01 05:33:25 +08:00
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification)
end
2016-04-01 05:33:25 +08:00
context "inactive user" do
before { notification.user.active = false }
2016-04-01 05:33:25 +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
2016-04-01 05:33:25 +08:00
it "enqueues a job if the user is staged" do
notification.user.staged = true
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification)
end
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
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
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +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
UserEmailObserver.process_notification(notification)
end
2013-02-06 03:16:51 +08:00
end
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
shared_examples "enqueue_public" do
include_examples "enqueue"
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
it "doesn't enqueue a job if the user has mention emails disabled" do
notification.user.user_option.update_columns(email_direct: false)
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification)
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
end
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
shared_examples "enqueue_private" do
include_examples "enqueue"
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
it "doesn't enqueue a job if the user has private message emails disabled" do
notification.user.user_option.update_columns(email_private_messages: false)
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification)
end
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
context 'user_mentioned' do
let(:type) { :user_mentioned }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(1) }
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08: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(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification)
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
end
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
context 'user_replied' do
let(:type) { :user_replied }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(2) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
context 'user_quoted' do
let(:type) { :user_quoted }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(3) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
end
2016-04-01 05:33:25 +08:00
context 'user_linked' do
let(:type) { :user_linked }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(11) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
end
2016-04-01 05:33:25 +08:00
context 'user_posted' do
let(:type) { :user_posted }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(9) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
context 'user_private_message' do
let(:type) { :user_private_message }
let(:delay) { SiteSetting.private_email_time_window_seconds }
let!(:notification) { create_notification(6) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_private"
2016-04-01 05:33:25 +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
end
2016-04-01 05:33:25 +08:00
context 'user_invited_to_private_message' do
let(:type) { :user_invited_to_private_message }
let(:delay) { SiteSetting.private_email_time_window_seconds }
let!(:notification) { create_notification(7) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
end
2016-04-01 05:33:25 +08:00
context 'user_invited_to_topic' do
let(:type) { :user_invited_to_topic }
let(:delay) { SiteSetting.private_email_time_window_seconds }
let!(:notification) { create_notification(13) }
2016-04-01 05:33:25 +08:00
include_examples "enqueue_public"
end
end