2019-07-30 15:05:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe EmailStyle do
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "ERB evaluation" do
|
2020-05-23 00:56:13 -04:00
|
|
|
it "does not evaluate ERB outside of the email itself" do
|
2020-05-25 12:46:44 -04:00
|
|
|
SiteSetting.email_custom_template = "<hello>%{email_content}</hello><%= (111 * 333) %>"
|
2020-05-23 00:56:13 -04:00
|
|
|
html = Email::Renderer.new(UserNotifications.signup(Fabricate(:user))).html
|
2020-07-20 05:04:04 -04:00
|
|
|
expect(html).not_to include("36963")
|
|
|
|
expect(html).to include("<hello>")
|
2019-10-23 19:21:24 -04:00
|
|
|
end
|
2020-05-23 00:56:13 -04:00
|
|
|
end
|
2019-10-23 19:21:24 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
context "with a custom template" do
|
|
|
|
before do
|
|
|
|
SiteSetting.email_custom_template = "<body><h1>FOR YOU</h1><div>%{email_content}</div></body>"
|
|
|
|
SiteSetting.email_custom_css = "h1 { color: red; } div.body { color: #FAB; }"
|
|
|
|
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
after do
|
|
|
|
SiteSetting.remove_override!(:email_custom_template)
|
|
|
|
SiteSetting.remove_override!(:email_custom_css)
|
2020-05-22 23:25:56 -04:00
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with invite" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:mail_html) { Email::Renderer.new(invite_mail).html }
|
|
|
|
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:invite)
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:invite_mail) { InviteMailer.send_invite(invite) }
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
it "applies customizations" do
|
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
|
|
|
expect(mail_html).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")
|
|
|
|
end
|
2020-05-22 23:25:56 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
it "applies customizations if compiled is missing" do
|
|
|
|
SiteSetting.remove_override!(:email_custom_css_compiled)
|
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
|
|
|
expect(mail_html).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")
|
|
|
|
end
|
2020-05-22 23:25:56 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
it "can apply RTL attrs" do
|
|
|
|
SiteSetting.default_locale = "he"
|
|
|
|
body_attrs = mail_html.match(/<body ([^>])+/)
|
|
|
|
expect(body_attrs[0]&.downcase).to match(/text-align:\s*right/)
|
|
|
|
expect(body_attrs[0]&.downcase).to include('dir="rtl"')
|
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user_replied" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:mail_html) { Email::Renderer.new(mail).html }
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
|
|
|
|
let(:category) { Fabricate(:category, name: "India") }
|
|
|
|
let(:topic) { Fabricate(:topic, category: category, title: "Super cool topic") }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic, raw: "This is My super duper cool topic") }
|
|
|
|
let(:response) { Fabricate(:basic_reply, topic: post.topic, user: response_by_user) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:notification) { Fabricate(:replied_notification, user: user, post: response) }
|
|
|
|
|
|
|
|
let(:mail) do
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
2019-07-30 15:05:08 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
it "customizations are applied to html part of emails" do
|
2020-07-05 20:56:19 -04:00
|
|
|
SiteSetting.default_email_in_reply_to = true
|
|
|
|
|
2020-05-21 14:26:03 -04:00
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
2022-04-20 14:00:04 -04:00
|
|
|
matches = mail_html.match(/<div style="([^"]+)" dm=\"body\">#{post.raw}/)
|
2020-05-23 00:56:13 -04:00
|
|
|
expect(matches[1]).to include("color: #FAB;") # custom
|
|
|
|
expect(matches[1]).to include("padding-top:5px;") # div.body
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
2020-05-23 00:56:13 -04:00
|
|
|
|
|
|
|
# TODO: translation override
|
2020-05-22 23:25:56 -04:00
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with signup" do
|
2020-05-23 00:56:13 -04:00
|
|
|
subject(:mail_html) { Email::Renderer.new(signup_mail).html }
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:signup_mail) { UserNotifications.signup(Fabricate(:user)) }
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
it "customizations are applied to html part of emails" do
|
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
2020-05-22 23:25:56 -04:00
|
|
|
expect(mail_html).to include("activate-account")
|
2020-05-21 14:26:03 -04:00
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with translation override" do
|
2020-05-23 00:56:13 -04:00
|
|
|
before do
|
|
|
|
TranslationOverride.upsert!(
|
2020-08-28 18:11:45 -04:00
|
|
|
SiteSetting.default_locale,
|
2020-05-23 00:56:13 -04:00
|
|
|
"user_notifications.signup.text_body_template",
|
|
|
|
"CLICK THAT LINK: %{base_url}/u/activate-account/%{email_token}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2020-08-28 18:11:45 -04:00
|
|
|
TranslationOverride.revert!(
|
|
|
|
SiteSetting.default_locale,
|
|
|
|
["user_notifications.signup.text_body_template"],
|
|
|
|
)
|
2020-05-23 00:56:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "applies customizations when translation override exists" do
|
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
|
|
|
expect(mail_html.scan("CLICK THAT LINK").count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2020-05-21 14:26:03 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
context "with some bad css" do
|
|
|
|
before do
|
|
|
|
SiteSetting.email_custom_css = '@import "nope.css"; h1 {{{ size: really big; '
|
|
|
|
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can render the html" do
|
|
|
|
expect(mail_html.scan(/<h1\s*(?:style=""){0,1}>FOR YOU<\/h1>/).count).to eq(1)
|
|
|
|
expect(mail_html).to include("activate-account")
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 23:25:56 -04:00
|
|
|
end
|
2020-05-21 14:26:03 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with digest" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:mail_html) { Email::Renderer.new(summary_email).html }
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
fab!(:popular_topic) do
|
|
|
|
Fabricate(:topic, user: Fabricate(:coding_horror), created_at: 1.hour.ago)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2023-06-21 10:00:19 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
let(:summary_email) { UserNotifications.digest(Fabricate(:user)) }
|
|
|
|
|
|
|
|
it "customizations are applied to html part of emails" do
|
|
|
|
expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)
|
|
|
|
expect(mail_html).to include(popular_topic.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't apply customizations if apply_custom_styles_to_digest is disabled" do
|
|
|
|
SiteSetting.apply_custom_styles_to_digest = false
|
|
|
|
expect(mail_html).to_not include('<h1 style="color: red;">FOR YOU</h1>')
|
|
|
|
expect(mail_html).to_not include("FOR YOU")
|
|
|
|
expect(mail_html).to include(popular_topic.title)
|
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|