mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Add specs for all user notifications, including whether they support
reply by email.
This commit is contained in:
parent
e29f4a3496
commit
cf9b6beb13
@ -8,15 +8,15 @@ class UserNotifications < ActionMailer::Base
|
|||||||
|
|
||||||
def signup(user, opts={})
|
def signup(user, opts={})
|
||||||
build_email(user.email,
|
build_email(user.email,
|
||||||
template: "user_notifications.signup",
|
template: "user_notifications.signup",
|
||||||
email_token: opts[:email_token])
|
email_token: opts[:email_token])
|
||||||
end
|
end
|
||||||
|
|
||||||
def signup_after_approval(user, opts={})
|
def signup_after_approval(user, opts={})
|
||||||
build_email(user.email,
|
build_email(user.email,
|
||||||
template: 'user_notifications.signup_after_approval',
|
template: 'user_notifications.signup_after_approval',
|
||||||
email_token: opts[:email_token],
|
email_token: opts[:email_token],
|
||||||
new_user_tips: SiteContent.content_for(:usage_tips))
|
new_user_tips: SiteContent.content_for(:usage_tips))
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorize_email(user, opts={})
|
def authorize_email(user, opts={})
|
||||||
@ -31,14 +31,14 @@ class UserNotifications < ActionMailer::Base
|
|||||||
post = opts[:post]
|
post = opts[:post]
|
||||||
|
|
||||||
build_email user.email,
|
build_email user.email,
|
||||||
template: "user_notifications.private_message",
|
template: "user_notifications.private_message",
|
||||||
message: post.raw,
|
message: post.raw,
|
||||||
url: post.url,
|
url: post.url,
|
||||||
subject_prefix: "[#{I18n.t('private_message_abbrev')}] #{post.post_number != 1 ? 're: ' : ''}",
|
subject_prefix: "[#{I18n.t('private_message_abbrev')}] #{post.post_number != 1 ? 're: ' : ''}",
|
||||||
topic_title: post.topic.title,
|
topic_title: post.topic.title,
|
||||||
private_message_from: post.user.name,
|
private_message_from: post.user.name,
|
||||||
from_alias: I18n.t(:via, username: post.user.name, site_name: SiteSetting.title),
|
from_alias: I18n.t(:via, username: post.user.name, site_name: SiteSetting.title),
|
||||||
add_unsubscribe_link: true
|
add_unsubscribe_link: true
|
||||||
end
|
end
|
||||||
|
|
||||||
def digest(user, opts={})
|
def digest(user, opts={})
|
||||||
@ -58,14 +58,40 @@ class UserNotifications < ActionMailer::Base
|
|||||||
# Don't send email unless there is content in it
|
# Don't send email unless there is content in it
|
||||||
if @new_topics.present?
|
if @new_topics.present?
|
||||||
build_email user.email,
|
build_email user.email,
|
||||||
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
|
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
|
||||||
subject: I18n.t('user_notifications.digest.subject_template',
|
subject: I18n.t('user_notifications.digest.subject_template',
|
||||||
site_name: @site_name,
|
site_name: @site_name,
|
||||||
date: I18n.l(Time.now, format: :short))
|
date: I18n.l(Time.now, format: :short))
|
||||||
end
|
end
|
||||||
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]
|
@notification = opts[:notification]
|
||||||
return unless @notification.present?
|
return unless @notification.present?
|
||||||
|
|
||||||
@ -84,6 +110,10 @@ class UserNotifications < ActionMailer::Base
|
|||||||
template: "user_notifications.user_#{notification_type}"
|
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 we have a display name, change the from address
|
||||||
if username.present?
|
if username.present?
|
||||||
email_opts[:from_alias] = I18n.t(:via, username: username, site_name: SiteSetting.title)
|
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)
|
build_email(user.email, email_opts)
|
||||||
end
|
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
|
end
|
||||||
|
@ -41,28 +41,123 @@ describe UserNotifications do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.user_mentioned' do
|
|
||||||
|
|
||||||
let(:post) { Fabricate(:post, user: user) }
|
def expects_build_with(condition)
|
||||||
let(:username) { "walterwhite"}
|
UserNotifications.any_instance.expects(:build_email).with(user.email, condition)
|
||||||
|
UserNotifications.send(mail_type, user, notification: notification, post: notification.post)
|
||||||
|
end
|
||||||
|
|
||||||
let(:notification) do
|
shared_examples "supports reply by email" do
|
||||||
Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number, data: {display_username: username}.to_json )
|
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
|
||||||
|
end
|
||||||
|
|
||||||
subject { UserNotifications.user_mentioned(user, notification: notification, post: notification.post) }
|
shared_examples "no reply by email" do
|
||||||
|
context "reply_by_email" do
|
||||||
|
|
||||||
its(:to) { should == [user.email] }
|
it "doesn't support reply by email, even when that option is enabled" do
|
||||||
its(:subject) { should be_present }
|
SiteSetting.stubs(:reply_by_email_enabled?).returns(true)
|
||||||
its(:from) { should == [SiteSetting.notification_email] }
|
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
||||||
|
end
|
||||||
|
|
||||||
it "should have the correct from address" do
|
it "should have not allow_reply_by_email set when that feature is disabled" do
|
||||||
subject.header['from'].to_s.should == "#{username} via #{SiteSetting.title} <#{SiteSetting.notification_email}>"
|
SiteSetting.stubs(:reply_by_email_enabled?).returns(false)
|
||||||
|
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
its(:body) { should be_present }
|
|
||||||
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,
|
||||||
|
notification_type: Notification.types[notification_type],
|
||||||
|
post_number: post.post_number,
|
||||||
|
data: {display_username: username}.to_json )
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.user_mentioned' do
|
||||||
|
it "has a username" do
|
||||||
|
expects_build_with(has_entry(:username, username))
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user