discourse/spec/requests/admin/email_styles_controller_spec.rb
Neil Lalonde 9656a21fdb
FEATURE: customization of html emails (#7934)
This feature adds the ability to customize the HTML part of all emails using a custom HTML template and optionally some CSS to style it. The CSS will be parsed and converted into inline styles because CSS is poorly supported by email clients. When writing the custom HTML and CSS, be aware of what email clients support. Keep customizations very simple.

Customizations can be added and edited in Admin > Customize > Email Style.

Since the summary email is already heavily styled, there is a setting to disable custom styles for summary emails called "apply custom styles to digest" found in Admin > Settings > Email.

As part of this work, RTL locales are now rendered correctly for all emails.
2019-07-30 15:05:08 -04:00

72 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Admin::EmailStylesController do
fab!(:admin) { Fabricate(:admin) }
let(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }
let(:default_css) { "" }
before do
sign_in(admin)
end
after do
SiteSetting.remove_override!(:email_custom_template)
SiteSetting.remove_override!(:email_custom_css)
end
describe 'show' do
it 'returns default values' do
get '/admin/customize/email_style.json'
expect(response.status).to eq(200)
json = ::JSON.parse(response.body)['email_style']
expect(json['html']).to eq(default_html)
expect(json['css']).to eq(default_css)
end
it 'returns customized values' do
SiteSetting.email_custom_template = "For you: %{email_content}"
SiteSetting.email_custom_css = ".user-name { font-size: 24px; }"
get '/admin/customize/email_style.json'
expect(response.status).to eq(200)
json = ::JSON.parse(response.body)['email_style']
expect(json['html']).to eq("For you: %{email_content}")
expect(json['css']).to eq(".user-name { font-size: 24px; }")
end
end
describe 'update' do
let(:valid_params) do
{
html: 'For you: %{email_content}',
css: '.user-name { color: purple; }'
}
end
it 'changes the settings' do
SiteSetting.email_custom_css = ".user-name { font-size: 24px; }"
put '/admin/customize/email_style.json', params: { email_style: valid_params }
expect(response.status).to eq(200)
expect(SiteSetting.email_custom_template).to eq(valid_params[:html])
expect(SiteSetting.email_custom_css).to eq(valid_params[:css])
end
it 'reports errors' do
put '/admin/customize/email_style.json', params: {
email_style: valid_params.merge(html: 'No email content')
}
expect(response.status).to eq(422)
json = JSON.parse(response.body)
expect(json['errors']).to include(
I18n.t(
'email_style.html_missing_placeholder',
placeholder: '%{email_content}'
)
)
end
end
end