From cf9b6beb13cd4563f0f0a43e269f9e3ea4376333 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 12 Jun 2013 16:35:46 -0400 Subject: [PATCH] Add specs for all user notifications, including whether they support reply by email. --- app/mailers/user_notifications.rb | 72 +++++++++----- spec/mailers/user_notifications_spec.rb | 123 +++++++++++++++++++++--- 2 files changed, 158 insertions(+), 37 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 1d36c0a8185..85ef806c9d9 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -8,15 +8,15 @@ class UserNotifications < ActionMailer::Base def signup(user, opts={}) build_email(user.email, - template: "user_notifications.signup", - email_token: opts[:email_token]) + template: "user_notifications.signup", + email_token: opts[:email_token]) end def signup_after_approval(user, opts={}) build_email(user.email, - template: 'user_notifications.signup_after_approval', - email_token: opts[:email_token], - new_user_tips: SiteContent.content_for(:usage_tips)) + template: 'user_notifications.signup_after_approval', + email_token: opts[:email_token], + new_user_tips: SiteContent.content_for(:usage_tips)) end def authorize_email(user, opts={}) @@ -31,14 +31,14 @@ class UserNotifications < ActionMailer::Base post = opts[:post] build_email user.email, - template: "user_notifications.private_message", - message: post.raw, - url: post.url, - subject_prefix: "[#{I18n.t('private_message_abbrev')}] #{post.post_number != 1 ? 're: ' : ''}", - topic_title: post.topic.title, - private_message_from: post.user.name, - from_alias: I18n.t(:via, username: post.user.name, site_name: SiteSetting.title), - add_unsubscribe_link: true + template: "user_notifications.private_message", + message: post.raw, + url: post.url, + subject_prefix: "[#{I18n.t('private_message_abbrev')}] #{post.post_number != 1 ? 're: ' : ''}", + topic_title: post.topic.title, + private_message_from: post.user.name, + from_alias: I18n.t(:via, username: post.user.name, site_name: SiteSetting.title), + add_unsubscribe_link: true end def digest(user, opts={}) @@ -58,14 +58,40 @@ class UserNotifications < ActionMailer::Base # Don't send email unless there is content in it if @new_topics.present? build_email user.email, - from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title), - subject: I18n.t('user_notifications.digest.subject_template', - site_name: @site_name, - date: I18n.l(Time.now, format: :short)) + from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title), + subject: I18n.t('user_notifications.digest.subject_template', + site_name: @site_name, + date: I18n.l(Time.now, format: :short)) 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 diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 6093eb7faaa..31afe29879c 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -41,28 +41,123 @@ describe UserNotifications do end end - describe '.user_mentioned' do - let(:post) { Fabricate(:post, user: user) } - let(:username) { "walterwhite"} + 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 - let(:notification) do - Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number, data: {display_username: username}.to_json ) + 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 - 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] } - its(:subject) { should be_present } - its(:from) { should == [SiteSetting.notification_email] } + 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 the correct from address" do - subject.header['from'].to_s.should == "#{username} via #{SiteSetting.title} <#{SiteSetting.notification_email}>" + 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 - - - its(:body) { should be_present } 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