From a0a7b3f9c45d34d53263f708987d3a28edb2260c Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 4 Jul 2022 16:03:53 +0200 Subject: [PATCH] FIX: Correctly display `/admin/emails` errors (#17321) Also removed unused codepath in `admin-badges-show`. --- .../addon/controllers/admin-email-index.js | 4 +-- .../admin/addon/routes/admin-badges-show.js | 10 ------ .../tests/acceptance/admin-emails-test.js | 32 ++++++++++++++----- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-index.js b/app/assets/javascripts/admin/addon/controllers/admin-email-index.js index cc808ad0949..91161bbe604 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-index.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-index.js @@ -43,10 +43,10 @@ export default Controller.extend({ this.set("sentTestEmailMessage", response.sent_test_email_message) ) .catch((e) => { - if (e.responseJSON && e.responseJSON.errors) { + if (e.jqXHR.responseJSON?.errors) { bootbox.alert( I18n.t("admin.email.error", { - server_error: e.responseJSON.errors[0], + server_error: e.jqXHR.responseJSON.errors[0], }) ); } else { diff --git a/app/assets/javascripts/admin/addon/routes/admin-badges-show.js b/app/assets/javascripts/admin/addon/routes/admin-badges-show.js index 7053be216f4..27270467622 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-badges-show.js +++ b/app/assets/javascripts/admin/addon/routes/admin-badges-show.js @@ -33,16 +33,6 @@ export default Route.extend({ }, actions: { - saveError(e) { - let msg = I18n.t("generic_error"); - if (e.responseJSON && e.responseJSON.errors) { - msg = I18n.t("generic_error_with_reason", { - error: e.responseJSON.errors.join(". "), - }); - } - bootbox.alert(msg); - }, - editGroupings() { const model = this.controllerFor("admin-badges").get("badgeGroupings"); showModal("admin-edit-badge-groupings", { model, admin: true }); diff --git a/app/assets/javascripts/discourse/tests/acceptance/admin-emails-test.js b/app/assets/javascripts/discourse/tests/acceptance/admin-emails-test.js index 12af83c2bbe..7d7772422e5 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/admin-emails-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/admin-emails-test.js @@ -1,6 +1,7 @@ -import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers"; +import { acceptance, query } from "discourse/tests/helpers/qunit-helpers"; import { click, fillIn, visit } from "@ember/test-helpers"; import { test } from "qunit"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; const EMAIL = ` From: "somebody" @@ -17,25 +18,40 @@ This part should be elided.`; acceptance("Admin - Emails", function (needs) { needs.user(); - needs.pretender((server, helper) => { - server.post("/admin/email/advanced-test", () => { - return helper.response({ + test("shows selected and elided text", async function (assert) { + pretender.post("/admin/email/advanced-test", () => { + return response({ format: 1, text: "Hello, this is a test!", elided: "---\n\nThis part should be elided.", }); }); - }); - test("shows selected and elided text", async function (assert) { await visit("/admin/email/advanced-test"); await fillIn("textarea.email-body", EMAIL.trim()); await click(".email-advanced-test button"); - assert.strictEqual(queryAll(".text pre").text(), "Hello, this is a test!"); + assert.strictEqual(query(".text pre").innerText, "Hello, this is a test!"); assert.strictEqual( - queryAll(".elided pre").text(), + query(".elided pre").innerText, "---\n\nThis part should be elided." ); }); + + test("displays received errors when testing emails", async function (assert) { + pretender.get("/admin/email.json", () => { + return response({}); + }); + + pretender.post("/admin/email/test", () => { + return response(422, { errors: ["some error"] }); + }); + + await visit("/admin/email"); + await fillIn(".admin-controls input", "test@example.com"); + await click(".btn-primary"); + + assert.ok(query(".bootbox.modal").innerText.includes("some error")); + await click(".bootbox .btn-primary"); + }); });