2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-02 08:52:37 -04:00
|
|
|
RSpec.describe Admin::EmailTemplatesController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-10-31 08:02:26 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-10-02 08:52:37 -04:00
|
|
|
|
|
|
|
def original_text(key)
|
|
|
|
I18n.overrides_disabled { I18n.t(key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:original_subject) { original_text("user_notifications.admin_login.subject_template") }
|
|
|
|
let(:original_body) { original_text("user_notifications.admin_login.text_body_template") }
|
|
|
|
let(:headers) { { ACCEPT: "application/json" } }
|
|
|
|
|
|
|
|
after do
|
|
|
|
TranslationOverride.delete_all
|
|
|
|
I18n.reload!
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#index" do
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
it "should work if you are an admin" do
|
|
|
|
get "/admin/customize/email_templates.json"
|
2022-10-31 08:02:26 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["email_templates"]).to be_present
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
it "returns overridden = true if subject or body has translation_overrides record" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: original_subject,
|
|
|
|
body: original_body,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers: headers
|
|
|
|
expect(response.status).to eq(200)
|
2019-07-01 21:53:16 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
get "/admin/customize/email_templates.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
templates = response.parsed_body["email_templates"]
|
|
|
|
template = templates.find { |t| t["id"] == "user_notifications.admin_login" }
|
|
|
|
expect(template["can_revert"]).to eq(true)
|
2019-07-01 21:53:16 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
TranslationOverride.destroy_all
|
2019-07-01 21:53:16 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
get "/admin/customize/email_templates.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
templates = response.parsed_body["email_templates"]
|
|
|
|
template = templates.find { |t| t["id"] == "user_notifications.admin_login" }
|
|
|
|
expect(template["can_revert"]).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2019-07-01 21:53:16 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "email templates inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/customize/email_templates.json"
|
2019-07-01 21:53:16 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2019-07-01 21:53:16 -04:00
|
|
|
end
|
2017-10-02 08:52:37 -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 templates inaccessible"
|
2017-10-02 08:52:37 -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) }
|
|
|
|
|
|
|
|
include_examples "email templates inaccessible"
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when not logged in" do
|
|
|
|
include_examples "email templates inaccessible"
|
2022-10-31 08:02:26 -04:00
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
2022-10-31 08:02:26 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
describe "#update" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2017-10-02 08:52:37 -04:00
|
|
|
|
|
|
|
it "returns 'not found' when an unknown email template id is used" do
|
|
|
|
put "/admin/customize/email_templates/non_existent_template",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: "Foo",
|
|
|
|
body: "Bar",
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
expect(response).not_to be_successful
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(json["error_type"]).to eq("not_found")
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "invalid email template" do
|
|
|
|
it "returns the right error messages" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
errors = json["errors"]
|
|
|
|
expect(errors).to be_present
|
|
|
|
expect(errors).to eq(expected_errors)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create translation overrides" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(I18n.t("user_notifications.admin_login.subject_template")).to eq(original_subject)
|
|
|
|
expect(I18n.t("user_notifications.admin_login.text_body_template")).to eq(original_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create entries in the Staff Log" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2017-10-02 08:52:37 -04:00
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.subject_template")
|
|
|
|
expect(log).to be_nil
|
|
|
|
|
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.text_body_template")
|
|
|
|
expect(log).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when subject is invalid" do
|
2017-11-20 03:10:06 -05:00
|
|
|
let(:email_subject) { "%{email_wrongfix} Foo" }
|
|
|
|
let(:email_body) { "Body with missing interpolation keys" }
|
|
|
|
|
|
|
|
let(:expected_errors) do
|
|
|
|
[
|
|
|
|
"<b>Subject</b>: #{
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "email_wrongfix",
|
|
|
|
)
|
|
|
|
}",
|
|
|
|
]
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
|
|
|
|
include_examples "invalid email template"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when body is invalid" do
|
2017-11-20 03:10:06 -05:00
|
|
|
let(:email_subject) { "Subject with missing interpolation key" }
|
|
|
|
let(:email_body) { "Body with %{invalid} interpolation key" }
|
|
|
|
|
|
|
|
let(:expected_errors) do
|
|
|
|
[
|
|
|
|
"<b>Body</b>: #{
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "invalid",
|
|
|
|
)
|
|
|
|
}",
|
|
|
|
]
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
|
|
|
|
include_examples "invalid email template"
|
|
|
|
end
|
|
|
|
|
2019-07-01 21:53:16 -04:00
|
|
|
context "when subject and body are invalid" do
|
2017-11-20 03:10:06 -05:00
|
|
|
let(:email_subject) { "Subject with %{invalid} interpolation key" }
|
|
|
|
let(:email_body) { "Body with some invalid interpolation keys: %{invalid}" }
|
|
|
|
|
2017-10-02 08:52:37 -04:00
|
|
|
let(:expected_errors) do
|
2017-11-20 03:10:06 -05:00
|
|
|
[
|
|
|
|
"<b>Subject</b>: #{
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "invalid",
|
|
|
|
)
|
|
|
|
}",
|
|
|
|
"<b>Body</b>: #{
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "invalid",
|
|
|
|
)
|
|
|
|
}",
|
|
|
|
]
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "invalid email template"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when subject and body contain all required interpolation keys" do
|
|
|
|
let(:email_subject) { "%{email_prefix} Foo" }
|
|
|
|
let(:email_body) { "The body contains [%{site_name}](%{base_url}) and %{email_token}." }
|
|
|
|
|
|
|
|
it "returns the successfully updated email template" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
template = json["email_template"]
|
|
|
|
expect(template).to be_present
|
|
|
|
|
|
|
|
expect(template["id"]).to eq("user_notifications.admin_login")
|
|
|
|
expect(template["title"]).to eq("Admin Login")
|
|
|
|
expect(template["subject"]).to eq(email_subject)
|
|
|
|
expect(template["body"]).to eq(email_body)
|
|
|
|
expect(template["can_revert"]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates translation overrides" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(I18n.t("user_notifications.admin_login.subject_template")).to eq(email_subject)
|
|
|
|
expect(I18n.t("user_notifications.admin_login.text_body_template")).to eq(email_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates entries in the Staff Log" do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
|
|
|
|
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.subject_template")
|
|
|
|
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
expect(log.previous_value).to eq(original_subject)
|
|
|
|
expect(log.new_value).to eq(email_subject)
|
|
|
|
|
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.text_body_template")
|
|
|
|
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
expect(log.previous_value).to eq(original_body)
|
|
|
|
expect(log.new_value).to eq(email_body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 10:49:40 -05:00
|
|
|
context "when subject has plural keys" do
|
|
|
|
it "doesn't update the subject" do
|
|
|
|
old_subject = I18n.t("system_messages.pending_users_reminder.subject_template")
|
|
|
|
expect(old_subject).to be_a(Hash)
|
|
|
|
|
|
|
|
put "/admin/customize/email_templates/system_messages.pending_users_reminder",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: "",
|
|
|
|
body: "Lorem ipsum",
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2019-03-06 10:49:40 -05:00
|
|
|
headers: headers
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(I18n.t("system_messages.pending_users_reminder.subject_template")).to eq(
|
|
|
|
old_subject,
|
|
|
|
)
|
|
|
|
expect(I18n.t("system_messages.pending_users_reminder.text_body_template")).to eq(
|
|
|
|
"Lorem ipsum",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "email template update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
put "/admin/customize/email_templates/some_id",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: "Subject",
|
|
|
|
body: "Body",
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2022-11-02 23:42:44 -04:00
|
|
|
headers: headers
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "email template update not allowed"
|
2017-10-02 08:52:37 -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) }
|
|
|
|
|
|
|
|
include_examples "email template update not allowed"
|
2022-10-31 08:02:26 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when not logged in" do
|
|
|
|
include_examples "email template update not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#revert" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2017-10-02 08:52:37 -04:00
|
|
|
|
|
|
|
it "returns 'not found' when an unknown email template id is used" do
|
|
|
|
delete "/admin/customize/email_templates/non_existent_template", headers: headers
|
2018-06-05 03:29:17 -04:00
|
|
|
expect(response).not_to be_successful
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(json["error_type"]).to eq("not_found")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when email template has translation overrides" do
|
|
|
|
let(:email_subject) { "%{email_prefix} Foo" }
|
|
|
|
let(:email_body) { "The body contains [%{site_name}](%{base_url}) and %{email_token}." }
|
|
|
|
|
|
|
|
before do
|
|
|
|
put "/admin/customize/email_templates/user_notifications.admin_login",
|
|
|
|
params: {
|
|
|
|
email_template: {
|
|
|
|
subject: email_subject,
|
|
|
|
body: email_body,
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2017-10-02 08:52:37 -04:00
|
|
|
headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it "restores the original subject and body" do
|
|
|
|
expect(I18n.t("user_notifications.admin_login.subject_template")).to eq(email_subject)
|
|
|
|
expect(I18n.t("user_notifications.admin_login.text_body_template")).to eq(email_body)
|
|
|
|
|
|
|
|
delete "/admin/customize/email_templates/user_notifications.admin_login", headers: headers
|
|
|
|
|
|
|
|
expect(I18n.t("user_notifications.admin_login.subject_template")).to eq(original_subject)
|
|
|
|
expect(I18n.t("user_notifications.admin_login.text_body_template")).to eq(original_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the restored email template" do
|
|
|
|
delete "/admin/customize/email_templates/user_notifications.admin_login", headers: headers
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-10-02 08:52:37 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-10-02 08:52:37 -04:00
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
template = json["email_template"]
|
|
|
|
expect(template).to be_present
|
|
|
|
|
|
|
|
expect(template["id"]).to eq("user_notifications.admin_login")
|
|
|
|
expect(template["title"]).to eq("Admin Login")
|
|
|
|
expect(template["subject"]).to eq(original_subject)
|
|
|
|
expect(template["body"]).to eq(original_body)
|
|
|
|
expect(template["can_revert"]).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates entries in the Staff Log" do
|
|
|
|
UserHistory.delete_all
|
|
|
|
delete "/admin/customize/email_templates/user_notifications.admin_login", headers: headers
|
|
|
|
|
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.subject_template")
|
|
|
|
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
expect(log.previous_value).to eq(email_subject)
|
|
|
|
expect(log.new_value).to eq(original_subject)
|
|
|
|
|
|
|
|
log = UserHistory.find_by_subject("user_notifications.admin_login.text_body_template")
|
|
|
|
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
expect(log.previous_value).to eq(email_body)
|
|
|
|
expect(log.new_value).to eq(original_body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "email template reversal not allowed" do
|
|
|
|
it "prevents reversals with a 404 response" do
|
|
|
|
delete "/admin/customize/email_templates/some_id", headers: headers
|
|
|
|
|
|
|
|
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 template reversal not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "email template reversal not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when not logged in" do
|
|
|
|
include_examples "email template reversal not allowed"
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|
|
|
|
|
2018-07-24 11:26:11 -04:00
|
|
|
it "uses only existing email templates" do
|
|
|
|
Admin::EmailTemplatesController.email_keys.each do |key|
|
|
|
|
expect(I18n.t(key)).to_not include("translation missing")
|
|
|
|
end
|
|
|
|
end
|
2017-10-02 08:52:37 -04:00
|
|
|
end
|