DEV: Allow site setting errors to be displayed as html if they are safe (#21342)

This is only useful for plugins currently

Co-authored-by: Penar Musaraj <pmusaraj@gmail.com>
This commit is contained in:
Daniel Waterworth 2023-05-04 15:01:19 -05:00 committed by GitHub
parent 2cc644cec7
commit 0a96579cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View File

@ -214,8 +214,15 @@ export default Mixin.create({
this.afterSave();
}
} catch (e) {
if (e.jqXHR?.responseJSON?.errors) {
this.set("validationMessage", e.jqXHR.responseJSON.errors[0]);
const json = e.jqXHR?.responseJSON;
if (json?.errors) {
let errorString = json.errors[0];
if (json.html_message) {
errorString = htmlSafe(errorString);
}
this.set("validationMessage", errorString);
} else {
this.set("validationMessage", I18n.t("generic_error"));
}

View File

@ -1,8 +1,9 @@
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers";
import { click, fillIn, render } from "@ember/test-helpers";
import { query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Integration | Component | site-setting", function (hooks) {
setupRenderingTest(hooks);
@ -18,4 +19,44 @@ module("Integration | Component | site-setting", function (hooks) {
assert.strictEqual(query(".formatted-selection").innerText, "a.com, b.com");
});
test("Error response with html_message is rendered as HTML", async function (assert) {
this.set("setting", {
setting: "test_setting",
value: "",
type: "input-setting-string",
});
const message = "<h1>Unable to update site settings</h1>";
pretender.put("/admin/site_settings/test_setting", () => {
return response(422, { html_message: true, errors: [message] });
});
await render(hbs`<SiteSetting @setting={{this.setting}} />`);
await fillIn(query(".setting input"), "value");
await click(query(".setting .d-icon-check"));
assert.strictEqual(query(".validation-error h1").outerHTML, message);
});
test("Error response without html_message is not rendered as HTML", async function (assert) {
this.set("setting", {
setting: "test_setting",
value: "",
type: "input-setting-string",
});
const message = "<h1>Unable to update site settings</h1>";
pretender.put("/admin/site_settings/test_setting", () => {
return response(422, { errors: [message] });
});
await render(hbs`<SiteSetting @setting={{this.setting}} />`);
await fillIn(query(".setting input"), "value");
await click(query(".setting .d-icon-check"));
assert.strictEqual(query(".validation-error h1"), null);
});
});