2013-02-05 13:16:51 -06:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe UserNotifications do
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-06-18 14:54:02 -05:00
|
|
|
let(:user) { Fabricate(:admin) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-01-30 23:37:40 -06:00
|
|
|
describe "#get_context_posts" do
|
|
|
|
it "does not include hidden/deleted/user_deleted posts in context" do
|
|
|
|
post = create_post
|
|
|
|
reply1 = create_post(topic: post.topic)
|
|
|
|
reply2 = create_post(topic: post.topic)
|
|
|
|
reply3 = create_post(topic: post.topic)
|
|
|
|
reply4 = create_post(topic: post.topic)
|
|
|
|
|
|
|
|
reply1.trash!
|
|
|
|
|
|
|
|
reply2.user_deleted = true
|
|
|
|
reply2.save
|
|
|
|
|
|
|
|
reply3.hidden = true
|
|
|
|
reply3.save
|
|
|
|
|
|
|
|
UserNotifications.get_context_posts(reply4, nil).count.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe ".signup" do
|
2014-10-29 10:06:50 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
subject { UserNotifications.signup(user) }
|
|
|
|
|
2014-10-29 10:06:50 -05:00
|
|
|
it "works" do
|
|
|
|
subject.to.should == [user.email]
|
|
|
|
subject.subject.should be_present
|
|
|
|
subject.from.should == [SiteSetting.notification_email]
|
|
|
|
subject.body.should be_present
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".forgot_password" do
|
2014-10-29 10:06:50 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
subject { UserNotifications.forgot_password(user) }
|
|
|
|
|
2014-10-29 10:06:50 -05:00
|
|
|
it "works" do
|
|
|
|
subject.to.should == [user.email]
|
|
|
|
subject.subject.should be_present
|
|
|
|
subject.from.should == [SiteSetting.notification_email]
|
|
|
|
subject.body.should be_present
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
describe '.digest' do
|
2014-10-29 10:06:50 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
subject { UserNotifications.digest(user) }
|
|
|
|
|
|
|
|
context "without new topics" do
|
2014-10-29 10:06:50 -05:00
|
|
|
|
|
|
|
it "doesn't send the email" do
|
|
|
|
subject.to.should be_blank
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with new topics" do
|
2014-10-29 10:06:50 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
before do
|
2013-06-03 15:12:24 -05:00
|
|
|
Topic.expects(:for_digest).returns([Fabricate(:topic, user: Fabricate(:coding_horror))])
|
2014-04-17 15:42:40 -05:00
|
|
|
Topic.expects(:new_since_last_seen).returns(Topic.none)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-10-29 10:06:50 -05:00
|
|
|
it "works" do
|
|
|
|
subject.to.should == [user.email]
|
|
|
|
subject.subject.should be_present
|
|
|
|
subject.from.should == [SiteSetting.notification_email]
|
2013-11-28 16:20:56 -06:00
|
|
|
subject.html_part.body.to_s.should be_present
|
2014-10-29 10:06:50 -05:00
|
|
|
subject.text_part.body.to_s.should be_present
|
2013-11-28 16:20:56 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 02:27:46 -05:00
|
|
|
describe '.user_replied' do
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
|
2014-10-03 06:44:08 -05:00
|
|
|
let(:category) { Fabricate(:category, name: 'India') }
|
|
|
|
let(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response) { Fabricate(:post, topic: post.topic, user: response_by_user)}
|
2014-02-11 23:51:26 -06:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:notification) { Fabricate(:notification, user: user) }
|
|
|
|
|
2013-07-26 02:27:46 -05:00
|
|
|
it 'generates a correct email' do
|
2014-10-21 14:14:56 -05:00
|
|
|
SiteSetting.stubs(:enable_email_names).returns(true)
|
2013-07-26 02:27:46 -05:00
|
|
|
mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
|
|
|
|
|
2014-10-21 09:06:55 -05:00
|
|
|
# from should include full user name
|
|
|
|
expect(mail[:from].display_names).to eql(['John Doe'])
|
|
|
|
|
2014-10-03 06:44:08 -05:00
|
|
|
# subject should include category name
|
|
|
|
expect(mail.subject).to match(/India/)
|
|
|
|
|
2013-07-26 02:27:46 -05:00
|
|
|
# 2 respond to links cause we have 1 context post
|
|
|
|
mail.html_part.to_s.scan(/To respond/).count.should == 2
|
|
|
|
|
|
|
|
# 1 unsubscribe
|
|
|
|
mail.html_part.to_s.scan(/To unsubscribe/).count.should == 1
|
|
|
|
|
2013-12-29 20:02:12 -06:00
|
|
|
# side effect, topic user is updated with post number
|
|
|
|
tu = TopicUser.get(post.topic_id, response.user)
|
|
|
|
tu.last_emailed_post_number.should == response.post_number
|
2014-02-11 23:51:26 -06:00
|
|
|
|
|
|
|
# in mailing list mode user_replies is not sent through
|
|
|
|
response.user.mailing_list_mode = true
|
|
|
|
mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
|
|
|
|
|
2014-10-15 04:09:56 -05:00
|
|
|
if rails_master?
|
|
|
|
mail.message.class.should == ActionMailer::Base::NullMail
|
|
|
|
else
|
|
|
|
mail.class.should == ActionMailer::Base::NullMail
|
|
|
|
end
|
2014-02-11 23:51:26 -06:00
|
|
|
|
|
|
|
response.user.mailing_list_mode = nil
|
|
|
|
mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
|
|
|
|
|
2014-10-15 04:09:56 -05:00
|
|
|
if rails_master?
|
|
|
|
mail.message.class.should_not == ActionMailer::Base::NullMail
|
|
|
|
else
|
|
|
|
mail.class.should_not == ActionMailer::Base::NullMail
|
|
|
|
end
|
2013-07-26 02:27:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-29 00:16:55 -05:00
|
|
|
describe '.user_posted' do
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
|
2014-09-29 00:16:55 -05:00
|
|
|
let(:post) { Fabricate(:post) }
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response) { Fabricate(:post, topic: post.topic, user: response_by_user)}
|
2014-09-29 00:16:55 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:notification) { Fabricate(:notification, user: user) }
|
|
|
|
|
|
|
|
it 'generates a correct email' do
|
2014-10-21 14:14:56 -05:00
|
|
|
SiteSetting.stubs(:enable_email_names).returns(false)
|
2014-09-29 00:16:55 -05:00
|
|
|
mail = UserNotifications.user_posted(response.user, post: response, notification: notification)
|
|
|
|
|
2014-10-21 09:06:55 -05:00
|
|
|
# from should not include full user name if "show user full names" is disabled
|
|
|
|
expect(mail[:from].display_names).to_not eql(['John Doe'])
|
|
|
|
|
2014-10-03 06:44:08 -05:00
|
|
|
# subject should not include category name
|
|
|
|
expect(mail.subject).not_to match(/Uncategorized/)
|
|
|
|
|
2014-09-29 00:16:55 -05:00
|
|
|
# 2 respond to links cause we have 1 context post
|
|
|
|
mail.html_part.to_s.scan(/To respond/).count.should == 2
|
|
|
|
|
|
|
|
# 1 unsubscribe link
|
|
|
|
mail.html_part.to_s.scan(/To unsubscribe/).count.should == 1
|
|
|
|
|
|
|
|
# side effect, topic user is updated with post number
|
|
|
|
tu = TopicUser.get(post.topic_id, response.user)
|
|
|
|
tu.last_emailed_post_number.should == response.post_number
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.user_private_message' do
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
|
2014-09-29 00:16:55 -05:00
|
|
|
let(:topic) { Fabricate(:private_message_topic) }
|
2014-10-21 09:06:55 -05:00
|
|
|
let(:response) { Fabricate(:post, topic: topic, user: response_by_user)}
|
2014-09-29 00:16:55 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:notification) { Fabricate(:notification, user: user) }
|
|
|
|
|
|
|
|
it 'generates a correct email' do
|
2014-10-21 14:14:56 -05:00
|
|
|
SiteSetting.stubs(:enable_email_names).returns(true)
|
2014-09-29 00:16:55 -05:00
|
|
|
mail = UserNotifications.user_private_message(response.user, post: response, notification: notification)
|
|
|
|
|
2014-10-21 09:06:55 -05:00
|
|
|
# from should include full user name
|
|
|
|
expect(mail[:from].display_names).to eql(['John Doe'])
|
|
|
|
|
2014-09-29 00:16:55 -05:00
|
|
|
# subject should include "[PM]"
|
|
|
|
expect(mail.subject).to match("[PM]")
|
|
|
|
|
|
|
|
# 1 respond to link
|
|
|
|
mail.html_part.to_s.scan(/To respond/).count.should == 1
|
|
|
|
|
|
|
|
# 1 unsubscribe link
|
|
|
|
mail.html_part.to_s.scan(/To unsubscribe/).count.should == 1
|
|
|
|
|
|
|
|
# side effect, topic user is updated with post number
|
|
|
|
tu = TopicUser.get(topic.id, response.user)
|
|
|
|
tu.last_emailed_post_number.should == response.post_number
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
def expects_build_with(condition)
|
|
|
|
UserNotifications.any_instance.expects(:build_email).with(user.email, condition)
|
2014-10-15 02:14:32 -05:00
|
|
|
mailer = UserNotifications.send(mail_type, user, notification: notification, post: notification.post)
|
|
|
|
|
|
|
|
if rails_master?
|
|
|
|
# Starting from Rails 4.2, calling MyMailer.some_method no longer result
|
|
|
|
# in an immediate call to MyMailer#some_method. Instead, a "lazy proxy" is
|
|
|
|
# returned (this is changed to support #deliver_later). As a quick hack to
|
|
|
|
# fix the test, calling #message (or anything, really) would force the
|
|
|
|
# Mailer object to be created and the method invoked.
|
|
|
|
mailer.message
|
|
|
|
end
|
2013-06-12 15:35:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "supports reply by email" do
|
|
|
|
context "reply_by_email" do
|
|
|
|
it "should have allow_reply_by_email set when that feature is enabled" do
|
|
|
|
expects_build_with(has_entry(:allow_reply_by_email, true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "no reply by email" do
|
|
|
|
context "reply_by_email" do
|
2013-06-13 09:56:16 -05:00
|
|
|
it "doesn't support reply by email" do
|
2013-06-12 15:35:46 -05:00
|
|
|
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "notification email building" do
|
2013-02-05 13:16:51 -06:00
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-06-12 15:35:46 -05:00
|
|
|
let(:mail_type) { "user_#{notification_type}"}
|
2013-02-27 17:30:14 -06:00
|
|
|
let(:username) { "walterwhite"}
|
2013-02-05 13:16:51 -06:00
|
|
|
let(:notification) do
|
2013-06-12 15:35:46 -05:00
|
|
|
Fabricate(:notification,
|
|
|
|
user: user,
|
|
|
|
topic: post.topic,
|
|
|
|
notification_type: Notification.types[notification_type],
|
|
|
|
post_number: post.post_number,
|
2014-02-03 19:56:28 -06:00
|
|
|
data: {original_username: username}.to_json )
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
describe '.user_mentioned' do
|
|
|
|
it "has a username" do
|
|
|
|
expects_build_with(has_entry(:username, username))
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
it "has a url" do
|
|
|
|
expects_build_with(has_key(:url))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a template" do
|
|
|
|
expects_build_with(has_entry(:template, "user_notifications.#{mail_type}"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a message" do
|
2013-07-22 14:06:37 -05:00
|
|
|
expects_build_with(has_key(:message))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a context" do
|
|
|
|
expects_build_with(has_key(:context))
|
2013-06-12 15:35:46 -05:00
|
|
|
end
|
2013-02-27 17:30:14 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
it "has an unsubscribe link" do
|
|
|
|
expects_build_with(has_key(:add_unsubscribe_link))
|
|
|
|
end
|
|
|
|
|
2013-06-13 17:11:10 -05:00
|
|
|
it "has an post_id" do
|
|
|
|
expects_build_with(has_key(:post_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has an topic_id" do
|
|
|
|
expects_build_with(has_key(:topic_id))
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
it "has a from alias" do
|
2014-10-21 14:14:56 -05:00
|
|
|
SiteSetting.stubs(:enable_email_names).returns(true)
|
2014-10-21 09:06:55 -05:00
|
|
|
expects_build_with(has_entry(:from_alias, "#{user.name}"))
|
2013-06-12 15:35:46 -05:00
|
|
|
end
|
2014-05-06 14:01:19 -05:00
|
|
|
|
|
|
|
it "should explain how to respond" do
|
|
|
|
expects_build_with(Not(has_entry(:include_respond_instructions, false)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not explain how to respond if the user is suspended" do
|
|
|
|
User.any_instance.stubs(:suspended?).returns(true)
|
|
|
|
expects_build_with(has_entry(:include_respond_instructions, false))
|
|
|
|
end
|
2013-02-27 17:30:14 -06:00
|
|
|
end
|
2013-06-12 15:35:46 -05:00
|
|
|
end
|
2013-02-27 17:30:14 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
describe "user mentioned email" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :mentioned }
|
|
|
|
include_examples "supports reply by email"
|
|
|
|
end
|
|
|
|
end
|
2013-02-27 17:30:14 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
describe "user replied" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :replied }
|
|
|
|
include_examples "supports reply by email"
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
describe "user quoted" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :quoted }
|
|
|
|
include_examples "supports reply by email"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "user posted" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :posted }
|
|
|
|
include_examples "supports reply by email"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "user invited to a private message" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :invited_to_private_message }
|
|
|
|
include_examples "no reply by email"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|