2019-07-30 15:05:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::EmailStylesController do
|
2019-07-30 15:05:08 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-11-02 23:42:44 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2019-07-30 15:05:08 -04:00
|
|
|
let(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }
|
|
|
|
let(:default_css) { "" }
|
|
|
|
|
|
|
|
after do
|
|
|
|
SiteSetting.remove_override!(:email_custom_template)
|
|
|
|
SiteSetting.remove_override!(:email_custom_css)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'show' do
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it 'returns default values' do
|
|
|
|
get '/admin/customize/email_style.json'
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = response.parsed_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 = response.parsed_body['email_style']
|
|
|
|
expect(json['html']).to eq("For you: %{email_content}")
|
|
|
|
expect(json['css']).to eq(".user-name { font-size: 24px; }")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "email styles inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get '/admin/customize/email_style.json'
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "email styles inaccessible"
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "email styles inaccessible"
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'update' do
|
|
|
|
let(:valid_params) do
|
|
|
|
{
|
|
|
|
html: 'For you: %{email_content}',
|
|
|
|
css: '.user-name { color: purple; }'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2019-07-30 15:05:08 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
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 = response.parsed_body
|
|
|
|
expect(json['errors']).to include(
|
|
|
|
I18n.t(
|
|
|
|
'email_style.html_missing_placeholder',
|
|
|
|
placeholder: '%{email_content}'
|
|
|
|
)
|
2019-07-30 15:05:08 -04:00
|
|
|
)
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "email style update not allowed" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
put '/admin/customize/email_style.json', params: { email_style: valid_params }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "email style update not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "email style update not allowed"
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|