2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
2013-10-01 15:04:02 +08:00
|
|
|
require_dependency 'jobs/base'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Jobs::UserEmail do
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:email_time_window_mins).returns(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) }
|
2013-11-08 02:53:32 +08:00
|
|
|
let(:suspended) { Fabricate(:user, last_seen_at: 10.minutes.ago, suspended_at: 5.minutes.ago, suspended_till: 7.days.from_now ) }
|
2015-05-11 07:10:10 +08:00
|
|
|
let(:anonymous) { Fabricate(:anonymous, last_seen_at: 11.minutes.ago ) }
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:mailer) { Mail::Message.new(to: user.email) }
|
|
|
|
|
|
|
|
it "raises an error when there is no user" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect { Jobs::UserEmail.new.execute(type: :digest) }.to raise_error(Discourse::InvalidParameters)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when there is no type" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect { Jobs::UserEmail.new.execute(user_id: user.id) }.to raise_error(Discourse::InvalidParameters)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the type doesn't exist" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect { Jobs::UserEmail.new.execute(type: :no_method, user_id: user.id) }.to raise_error(Discourse::InvalidParameters)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't call the mailer when the user is missing" do
|
|
|
|
UserNotifications.expects(:digest).never
|
|
|
|
Jobs::UserEmail.new.execute(type: :digest, user_id: 1234)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
context 'to_address' do
|
|
|
|
it 'overwrites a to_address when present' do
|
|
|
|
UserNotifications.expects(:authorize_email).returns(mailer)
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-06 03:16:51 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :authorize_email, user_id: user.id, to_address: 'jake@adventuretime.ooo')
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(mailer.to).to eq(['jake@adventuretime.ooo'])
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "recently seen" do
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "doesn't send an email to a user that's been recently seen" do
|
|
|
|
user.update_column(:last_seen_at, 9.minutes.ago)
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-10-01 15:04:02 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :user_replied, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does send an email to a user that's been recently seen but has email_always set" do
|
|
|
|
user.update_attributes(last_seen_at: 9.minutes.ago, email_always: true)
|
|
|
|
Email::Sender.any_instance.expects(:send)
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_replied, user_id: user.id, post_id: post.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'args' do
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'passes a token as an argument when a token is present' do
|
|
|
|
UserNotifications.expects(:forgot_password).with(user, {email_token: 'asdfasdf'}).returns(mailer)
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-06 03:16:51 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :forgot_password, user_id: user.id, email_token: 'asdfasdf')
|
|
|
|
end
|
|
|
|
|
|
|
|
context "post" do
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
|
|
|
|
|
|
|
it 'passes a post as an argument when a post_id is present' do
|
|
|
|
UserNotifications.expects(:private_message).with(user, {post: post}).returns(mailer)
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-06 03:16:51 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send the email if you've seen the post" do
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-02-06 03:16:51 +08:00
|
|
|
PostTiming.record_timing(topic_id: post.topic_id, user_id: user.id, post_number: post.post_number, msecs: 6666)
|
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
2013-03-26 01:39:57 +08:00
|
|
|
it "doesn't send the email if the user deleted the post" do
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-03-26 01:39:57 +08:00
|
|
|
post.update_column(:user_deleted, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
context 'user is suspended' do
|
2013-10-31 03:31:16 +08:00
|
|
|
it "doesn't send email for a pm from a regular user" do
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-11-08 02:53:32 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: suspended.id, post_id: post.id)
|
2013-10-31 03:31:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send email for a pm from a staff user" do
|
|
|
|
pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
2013-11-08 02:53:32 +08:00
|
|
|
pm_from_staff.topic.topic_allowed_users.create!(user_id: suspended.id)
|
2013-10-31 03:31:16 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-11-08 02:53:32 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: suspended.id, post_id: pm_from_staff.id)
|
2013-10-31 03:31:16 +08:00
|
|
|
end
|
|
|
|
end
|
2015-05-11 07:10:10 +08:00
|
|
|
|
|
|
|
context 'user is anonymous' do
|
|
|
|
before { SiteSetting.stubs(:allow_anonymous_posting).returns(true) }
|
|
|
|
|
|
|
|
it "doesn't send email for a pm from a regular user" do
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: anonymous.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send email for a pm from a staff user" do
|
|
|
|
pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
|
|
|
pm_from_staff.topic.topic_allowed_users.create!(user_id: anonymous.id)
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: anonymous.id, post_id: pm_from_staff.id)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context 'notification' do
|
2013-06-11 00:02:04 +08:00
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2014-02-04 09:56:28 +08:00
|
|
|
let!(:notification) {
|
|
|
|
Fabricate(:notification,
|
|
|
|
user: user,
|
|
|
|
topic: post.topic,
|
|
|
|
post_number: post.post_number,
|
|
|
|
data: {
|
|
|
|
original_post_id: post.id
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
}
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'passes a notification as an argument when a notification_id is present' do
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-06-11 00:02:04 +08:00
|
|
|
UserNotifications.expects(:user_mentioned).with(user, notification: notification, post: post).returns(mailer)
|
2013-02-06 03:16:51 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send the email if the notification has been seen" do
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-02-06 03:16:51 +08:00
|
|
|
notification.update_column(:read, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
2014-06-10 11:36:20 +08:00
|
|
|
it "does send the email if the notification has been seen but the user is set for email_always" do
|
2014-06-09 19:09:36 +08:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
|
|
|
notification.update_column(:read, true)
|
2014-06-10 11:36:20 +08:00
|
|
|
user.update_column(:email_always, true)
|
2014-06-09 19:09:36 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
2013-06-11 00:02:04 +08:00
|
|
|
it "doesn't send the email if the post has been user deleted" do
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-06-11 00:02:04 +08:00
|
|
|
post.update_column(:user_deleted, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
context 'user is suspended' do
|
2013-10-31 03:31:16 +08:00
|
|
|
it "doesn't send email for a pm from a regular user" do
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-11-08 02:53:32 +08:00
|
|
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, notification_id: notification.id)
|
2013-10-31 03:31:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'pm from staff' do
|
|
|
|
before do
|
|
|
|
@pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
2013-11-08 02:53:32 +08:00
|
|
|
@pm_from_staff.topic.topic_allowed_users.create!(user_id: suspended.id)
|
2014-02-04 09:56:28 +08:00
|
|
|
@pm_notification = Fabricate(:notification,
|
|
|
|
user: suspended,
|
|
|
|
topic: @pm_from_staff.topic,
|
|
|
|
post_number: @pm_from_staff.post_number,
|
|
|
|
data: { original_post_id: @pm_from_staff.id }.to_json
|
|
|
|
)
|
2013-11-08 02:53:32 +08:00
|
|
|
UserNotifications.expects(:user_private_message).with(suspended, notification: @pm_notification, post: @pm_from_staff).returns(mailer)
|
2013-10-31 03:31:16 +08:00
|
|
|
end
|
|
|
|
|
2014-02-04 09:56:28 +08:00
|
|
|
subject(:execute_user_email_job) {
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, notification_id: @pm_notification.id) }
|
2013-10-31 03:31:16 +08:00
|
|
|
|
|
|
|
it "sends an email" do
|
|
|
|
execute_user_email_job
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends an email even if user was last seen recently" do
|
2013-11-08 02:53:32 +08:00
|
|
|
suspended.update_column(:last_seen_at, 1.minute.ago)
|
2013-10-31 03:31:16 +08:00
|
|
|
execute_user_email_job
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-11 07:10:10 +08:00
|
|
|
|
|
|
|
context 'user is anonymous' do
|
|
|
|
before { SiteSetting.stubs(:allow_anonymous_posting).returns(true) }
|
|
|
|
|
|
|
|
it "doesn't send email for a pm from a regular user" do
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send email for a pm from staff" do
|
|
|
|
pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
|
|
|
pm_from_staff.topic.topic_allowed_users.create!(user_id: anonymous.id)
|
|
|
|
pm_notification = Fabricate(:notification,
|
|
|
|
user: anonymous,
|
|
|
|
topic: pm_from_staff.topic,
|
|
|
|
post_number: pm_from_staff.post_number,
|
|
|
|
data: { original_post_id: pm_from_staff.id }.to_json
|
|
|
|
)
|
|
|
|
Email::Sender.any_instance.expects(:send).never
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: pm_notification.id)
|
|
|
|
end
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|