Add specs for all user notifications, including whether they support

reply by email.
This commit is contained in:
Robin Ward 2013-06-12 16:35:46 -04:00
parent e29f4a3496
commit cf9b6beb13
2 changed files with 158 additions and 37 deletions

View File

@ -65,7 +65,33 @@ class UserNotifications < ActionMailer::Base
end
end
def notification_template(user, opts)
def user_invited_to_private_message(user, opts)
notification_email(user, opts)
end
def user_replied(user, opts)
opts[:allow_reply_by_email] = true
notification_email(user, opts)
end
def user_quoted(user, opts)
opts[:allow_reply_by_email] = true
notification_email(user, opts)
end
def user_mentioned(user, opts)
opts[:allow_reply_by_email] = true
notification_email(user, opts)
end
def user_posted(user, opts)
opts[:allow_reply_by_email] = true
notification_email(user, opts)
end
protected
def notification_email(user, opts)
@notification = opts[:notification]
return unless @notification.present?
@ -84,6 +110,10 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.user_#{notification_type}"
}
if opts[:allow_reply_by_email] && SiteSetting.reply_by_email_enabled?
email_opts[:allow_reply_by_email] = true
end
# If we have a display name, change the from address
if username.present?
email_opts[:from_alias] = I18n.t(:via, username: username, site_name: SiteSetting.title)
@ -92,10 +122,6 @@ class UserNotifications < ActionMailer::Base
build_email(user.email, email_opts)
end
alias :user_invited_to_private_message :notification_template
alias :user_replied :notification_template
alias :user_quoted :notification_template
alias :user_mentioned :notification_template
alias :user_posted :notification_template
end

View File

@ -41,28 +41,123 @@ describe UserNotifications do
end
end
describe '.user_mentioned' do
def expects_build_with(condition)
UserNotifications.any_instance.expects(:build_email).with(user.email, condition)
UserNotifications.send(mail_type, user, notification: notification, post: notification.post)
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
SiteSetting.stubs(:reply_by_email_enabled?).returns(true)
expects_build_with(has_entry(:allow_reply_by_email, true))
end
it "should have not allow_reply_by_email set when that feature is disabled" do
SiteSetting.stubs(:reply_by_email_enabled?).returns(false)
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
end
end
end
shared_examples "no reply by email" do
context "reply_by_email" do
it "doesn't support reply by email, even when that option is enabled" do
SiteSetting.stubs(:reply_by_email_enabled?).returns(true)
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
end
it "should have not allow_reply_by_email set when that feature is disabled" do
SiteSetting.stubs(:reply_by_email_enabled?).returns(false)
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
end
end
end
shared_examples "notification email building" do
let(:post) { Fabricate(:post, user: user) }
let(:mail_type) { "user_#{notification_type}"}
let(:username) { "walterwhite"}
let(:notification) do
Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number, data: {display_username: username}.to_json )
Fabricate(:notification,
user: user,
topic: post.topic,
notification_type: Notification.types[notification_type],
post_number: post.post_number,
data: {display_username: username}.to_json )
end
subject { UserNotifications.user_mentioned(user, notification: notification, post: notification.post) }
describe '.user_mentioned' do
it "has a username" do
expects_build_with(has_entry(:username, username))
end
its(:to) { should == [user.email] }
its(:subject) { should be_present }
its(:from) { should == [SiteSetting.notification_email] }
it "has a url" do
expects_build_with(has_key(:url))
end
it "should have the correct from address" do
subject.header['from'].to_s.should == "#{username} via #{SiteSetting.title} <#{SiteSetting.notification_email}>"
it "has a template" do
expects_build_with(has_entry(:template, "user_notifications.#{mail_type}"))
end
it "has a message" do
expects_build_with(has_entry(:message, post.raw))
end
it "has an unsubscribe link" do
expects_build_with(has_key(:add_unsubscribe_link))
end
it "has a from alias" do
expects_build_with(has_entry(:from_alias, "#{username} via #{SiteSetting.title}"))
end
end
end
describe "user mentioned email" do
include_examples "notification email building" do
let(:notification_type) { :mentioned }
include_examples "supports reply by email"
end
end
describe "user replied" do
include_examples "notification email building" do
let(:notification_type) { :replied }
include_examples "supports reply by email"
end
end
its(:body) { should be_present }
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 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
end