2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'jobs'
|
|
|
|
|
|
|
|
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 ) }
|
|
|
|
let(:mailer) { Mail::Message.new(to: user.email) }
|
|
|
|
|
|
|
|
it "raises an error when there is no user" do
|
|
|
|
lambda { Jobs::UserEmail.new.execute(type: :digest) }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when there is no type" do
|
|
|
|
lambda { Jobs::UserEmail.new.execute(user_id: user.id) }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the type doesn't exist" do
|
|
|
|
lambda { Jobs::UserEmail.new.execute(type: :no_method, user_id: user.id) }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
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-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
context 'to_address' do
|
|
|
|
it 'overwrites a to_address when present' do
|
|
|
|
UserNotifications.expects(:authorize_email).returns(mailer)
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-05 14:16:51 -05:00
|
|
|
Jobs::UserEmail.new.execute(type: :authorize_email, user_id: user.id, to_address: 'jake@adventuretime.ooo')
|
|
|
|
mailer.to.should == ['jake@adventuretime.ooo']
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "recently seen" do
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05: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-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-02-05 14:16:51 -05:00
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'args' do
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05: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-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-05 14:16:51 -05: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-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-02-05 14:16:51 -05: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-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-02-05 14:16:51 -05: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-25 13:39:57 -04:00
|
|
|
it "doesn't send the email if the user deleted the post" do
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-03-25 13:39:57 -04:00
|
|
|
post.update_column(:user_deleted, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context 'notification' do
|
2013-06-10 12:02:04 -04:00
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
|
|
|
let!(:notification) { Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number)}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'passes a notification as an argument when a notification_id is present' do
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send)
|
2013-06-10 12:02:04 -04:00
|
|
|
UserNotifications.expects(:user_mentioned).with(user, notification: notification, post: post).returns(mailer)
|
2013-02-05 14:16:51 -05: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-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-02-05 14:16:51 -05:00
|
|
|
notification.update_column(:read, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
2013-06-10 12:02:04 -04:00
|
|
|
it "doesn't send the email if the post has been user deleted" do
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.any_instance.expects(:send).never
|
2013-06-10 12:02:04 -04:00
|
|
|
post.update_column(:user_deleted, true)
|
|
|
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|