From b179fb98b135ad4b74cc050dd9fe15636e6a949a Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 25 Jul 2022 02:31:52 +0200 Subject: [PATCH] DEV: Consistently use `response` helper (#17627) --- .../dismiss-notification-modal-test.js | 14 +++--- .../acceptance/second-factor-auth-test.js | 50 +++++++------------ .../components/admin-report-test.js | 24 ++++----- .../components/avatar-uploader-test.js | 6 +-- .../components/badge-title-test.js | 8 +-- .../integration/components/cook-text-test.js | 24 ++++----- .../components/invite-panel-test.js | 20 +++----- .../components/site-header-test.js | 4 +- .../user-menu/notifications-list-test.js | 14 ++---- .../tests/unit/controllers/topic-test.js | 16 ++---- .../tests/unit/lib/link-mentions-test.js | 12 ++--- .../tests/unit/models/post-stream-test.js | 6 +-- ...ivate-message-topic-tracking-state-test.js | 24 ++++----- .../widgets/discourse-poll-test.js | 50 +++++++++---------- 14 files changed, 108 insertions(+), 164 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/acceptance/dismiss-notification-modal-test.js b/app/assets/javascripts/discourse/tests/acceptance/dismiss-notification-modal-test.js index 656cba95ae1..0e446fc8f58 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/dismiss-notification-modal-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/dismiss-notification-modal-test.js @@ -7,15 +7,15 @@ import { } from "discourse/tests/helpers/qunit-helpers"; import I18n from "I18n"; import { test } from "qunit"; -import pretender from "../helpers/create-pretender"; +import pretender, { response } from "../helpers/create-pretender"; acceptance("Dismiss notification confirmation", function (needs) { needs.user(); test("does not show modal when no high priority notifications", async function (assert) { - pretender.put("/notifications/mark-read", () => { - return [200, { "Content-Type": "application/json" }, { success: true }]; - }); + pretender.put("/notifications/mark-read", () => + response({ success: true }) + ); await visit("/"); await click(".current-user"); @@ -50,9 +50,9 @@ acceptance("Dismiss notification confirmation", function (needs) { query(".dismiss-notification-confirmation-modal .btn-primary").innerText, I18n.t("notifications.dismiss_confirmation.dismiss") ); - pretender.put("/notifications/mark-read", () => { - return [200, { "Content-Type": "application/json" }, { success: true }]; - }); + pretender.put("/notifications/mark-read", () => + response({ success: true }) + ); await click(".dismiss-notification-confirmation-modal .btn-primary"); diff --git a/app/assets/javascripts/discourse/tests/acceptance/second-factor-auth-test.js b/app/assets/javascripts/discourse/tests/acceptance/second-factor-auth-test.js index 417a31ecf26..f2ca7501553 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/second-factor-auth-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/second-factor-auth-test.js @@ -60,48 +60,36 @@ let callbackCount = 0; acceptance("Second Factor Auth Page", function (needs) { needs.user(); - needs.pretender((server, helpers) => { + needs.pretender((server, { parsePostData, response }) => { server.get("/session/2fa.json", (request) => { - const response = { ...RESPONSES[request.queryParams.nonce] }; - const status = response.status || 200; - delete response.status; - return [status, { "Content-Type": "application/json" }, response]; + const responseBody = { ...RESPONSES[request.queryParams.nonce] }; + const status = responseBody.status || 200; + delete responseBody.status; + return response(status, responseBody); }); server.post("/session/2fa", (request) => { - const params = helpers.parsePostData(request.requestBody); + const params = parsePostData(request.requestBody); if (params.second_factor_token === WRONG_TOTP) { - return [ - 401, - { "Content-Type": "application/json" }, - { - error: "invalid token man", - ok: false, - }, - ]; + return response(401, { + error: "invalid token man", + ok: false, + }); } else { - return [ - 200, - { "Content-Type": "application/json" }, - { - ok: true, - callback_method: "PUT", - callback_path: "/callback-path", - redirect_url: "/", - }, - ]; + return response({ + ok: true, + callback_method: "PUT", + callback_path: "/callback-path", + redirect_url: "/", + }); } }); server.put("/callback-path", () => { callbackCount++; - return [ - 200, - { "Content-Type": "application/json" }, - { - whatever: true, - }, - ]; + return response(200, { + whatever: true, + }); }); }); diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js b/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js index bf2abe505d3..4bd62862f7a 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js @@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { click, render } from "@ember/test-helpers"; import { count, exists, query } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; module("Integration | Component | admin-report", function (hooks) { setupRenderingTest(hooks); @@ -124,19 +124,15 @@ module("Integration | Component | admin-report", function (hooks) { }); test("rate limited", async function (assert) { - pretender.get("/admin/reports/bulk", () => { - return [ - 429, - { "Content-Type": "application/json" }, - { - errors: [ - "You’ve performed this action too many times. Please wait 10 seconds before trying again.", - ], - error_type: "rate_limit", - extras: { wait_seconds: 10 }, - }, - ]; - }); + pretender.get("/admin/reports/bulk", () => + response(429, { + errors: [ + "You’ve performed this action too many times. Please wait 10 seconds before trying again.", + ], + error_type: "rate_limit", + extras: { wait_seconds: 10 }, + }) + ); await render(hbs``); diff --git a/app/assets/javascripts/discourse/tests/integration/components/avatar-uploader-test.js b/app/assets/javascripts/discourse/tests/integration/components/avatar-uploader-test.js index a4ddcc622db..fe7f9f0e7d1 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/avatar-uploader-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/avatar-uploader-test.js @@ -3,15 +3,13 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; import { createFile } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; module("Integration | Component | avatar-uploader", function (hooks) { setupRenderingTest(hooks); hooks.beforeEach(function () { - pretender.post("/uploads.json", () => { - return [200, { "Content-Type": "application/json" }, {}]; - }); + pretender.post("/uploads.json", () => response({})); }); test("default", async function (assert) { diff --git a/app/assets/javascripts/discourse/tests/integration/components/badge-title-test.js b/app/assets/javascripts/discourse/tests/integration/components/badge-title-test.js index 3b421b9f21b..47f7f7d330a 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/badge-title-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/badge-title-test.js @@ -2,7 +2,7 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { click, render } from "@ember/test-helpers"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import EmberObject from "@ember/object"; @@ -23,11 +23,7 @@ module("Integration | Component | badge-title", function (hooks) { }), ]); - pretender.put("/u/eviltrout/preferences/badge_title", () => [ - 200, - { "Content-Type": "application/json" }, - {}, - ]); + pretender.put("/u/eviltrout/preferences/badge_title", () => response({})); await render(hbs` diff --git a/app/assets/javascripts/discourse/tests/integration/components/cook-text-test.js b/app/assets/javascripts/discourse/tests/integration/components/cook-text-test.js index 16d79ac921a..15d648f2cb7 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/cook-text-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/cook-text-test.js @@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; import { query } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import { resetCache } from "pretty-text/upload-short-url"; module("Integration | Component | cook-text", function (hooks) { @@ -21,19 +21,15 @@ module("Integration | Component | cook-text", function (hooks) { }); test("resolves short URLs", async function (assert) { - pretender.post("/uploads/lookup-urls", () => { - return [ - 200, - { "Content-Type": "application/json" }, - [ - { - short_url: "upload://a.png", - url: "/images/avatar.png", - short_path: "/images/d-logo-sketch.png", - }, - ], - ]; - }); + pretender.post("/uploads/lookup-urls", () => + response([ + { + short_url: "upload://a.png", + url: "/images/avatar.png", + short_path: "/images/d-logo-sketch.png", + }, + ]) + ); await render( hbs`` diff --git a/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js b/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js index 81895d8fc6e..bd9a15de80d 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js @@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { click, render } from "@ember/test-helpers"; import { exists, query } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import User from "discourse/models/user"; @@ -11,19 +11,13 @@ module("Integration | Component | invite-panel", function (hooks) { setupRenderingTest(hooks); test("shows the invite link after it is generated", async function (assert) { - pretender.get("/u/search/users", () => { - return [200, { "Content-Type": "application/json" }, { users: [] }]; - }); + pretender.get("/u/search/users", () => response({ users: [] })); - pretender.post("/invites", () => { - return [ - 200, - { "Content-Type": "application/json" }, - { - link: "http://example.com/invites/92c297e886a0ca03089a109ccd6be155", - }, - ]; - }); + pretender.post("/invites", () => + response({ + link: "http://example.com/invites/92c297e886a0ca03089a109ccd6be155", + }) + ); this.currentUser.set("details", { can_invite_via_email: true }); this.set("panel", { diff --git a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js index b1537bc75ba..246fd35357b 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js @@ -2,7 +2,7 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { click, render } from "@ember/test-helpers"; import { count, exists, query } from "discourse/tests/helpers/qunit-helpers"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import { hbs } from "ember-cli-htmlbars"; module("Integration | Component | site-header", function (hooks) { @@ -43,7 +43,7 @@ module("Integration | Component | site-header", function (hooks) { pretender.get("/notifications", () => { assert.ok(false, "it should not try to refresh notifications"); - return [403, { "Content-Type": "application/json" }, {}]; + return response(403, {}); }); // Click anywhere diff --git a/app/assets/javascripts/discourse/tests/integration/components/user-menu/notifications-list-test.js b/app/assets/javascripts/discourse/tests/integration/components/user-menu/notifications-list-test.js index 395cf48eb89..0d39fe23b0b 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/user-menu/notifications-list-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/user-menu/notifications-list-test.js @@ -5,7 +5,7 @@ import { render } from "@ember/test-helpers"; import { cloneJSON } from "discourse-common/lib/object"; import NotificationFixtures from "discourse/tests/fixtures/notification-fixtures"; import { hbs } from "ember-cli-htmlbars"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import I18n from "I18n"; function getNotificationsData() { @@ -22,16 +22,12 @@ module( hooks.beforeEach(() => { pretender.get("/notifications", (request) => { queryParams = request.queryParams; - return [ - 200, - { "Content-Type": "application/json" }, - { notifications: notificationsData }, - ]; + return response({ notifications: notificationsData }); }); - pretender.put("/notifications/mark-read", () => { - return [200, { "Content-Type": "application/json" }, { success: true }]; - }); + pretender.put("/notifications/mark-read", () => + response({ success: true }) + ); }); hooks.afterEach(() => { diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js index af24651b011..9e3da6a4ef4 100644 --- a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js +++ b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js @@ -5,7 +5,7 @@ import Topic from "discourse/models/topic"; import User from "discourse/models/user"; import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; import { next } from "@ember/runloop"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import { settled } from "@ember/test-helpers"; import { test } from "qunit"; @@ -594,13 +594,9 @@ discourseModule("Unit | Controller | topic", function (hooks) { }); test("selectReplies", async function (assert) { - pretender.get("/posts/1/reply-ids.json", () => { - return [ - 200, - { "Content-Type": "application/json" }, - [{ id: 2, level: 1 }], - ]; - }); + pretender.get("/posts/1/reply-ids.json", () => + response([{ id: 2, level: 1 }]) + ); let model = topicWithStream({ posts: [{ id: 1 }, { id: 2 }], @@ -656,9 +652,7 @@ discourseModule("Unit | Controller | topic", function (hooks) { }); test("deletePost - no modal is shown if post does not have replies", function (assert) { - pretender.get("/posts/2/reply-ids.json", () => { - return [200, { "Content-Type": "application/json" }, []]; - }); + pretender.get("/posts/2/reply-ids.json", () => response([])); let destroyed; const post = EmberObject.create({ diff --git a/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js b/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js index 46ddfa7b37b..4eeb1f8d075 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js @@ -3,15 +3,13 @@ import { linkSeenMentions, } from "discourse/lib/link-mentions"; import { module, test } from "qunit"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import domFromString from "discourse-common/lib/dom-from-string"; module("Unit | Utility | link-mentions", function () { test("linkSeenMentions replaces users and groups", async function (assert) { - pretender.get("/u/is_local_username", () => [ - 200, - { "Content-Type": "application/json" }, - { + pretender.get("/u/is_local_username", () => + response({ valid: ["valid_user"], valid_groups: ["valid_group"], mentionable_groups: [ @@ -22,8 +20,8 @@ module("Unit | Utility | link-mentions", function () { ], cannot_see: [], max_users_notified_per_group_mention: 100, - }, - ]); + }) + ); await fetchUnseenMentions([ "valid_user", diff --git a/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js b/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js index a97e9343b96..ea255ffbbd1 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js @@ -5,7 +5,7 @@ import Post from "discourse/models/post"; import { Promise } from "rsvp"; import User from "discourse/models/user"; import createStore from "discourse/tests/helpers/create-store"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import sinon from "sinon"; function buildStream(id, stream) { @@ -873,10 +873,6 @@ module("Unit | Model | post-stream", function () { ); }); - const response = (object) => { - return [200, { "Content-Type": "application/json" }, object]; - }; - pretender.get("/posts/4", () => { return response({ id: 4, post_number: 4 }); }); diff --git a/app/assets/javascripts/discourse/tests/unit/models/private-message-topic-tracking-state-test.js b/app/assets/javascripts/discourse/tests/unit/models/private-message-topic-tracking-state-test.js index c8672ce3aab..e6cae9ea610 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/private-message-topic-tracking-state-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/private-message-topic-tracking-state-test.js @@ -1,5 +1,5 @@ import { test } from "qunit"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import { discourseModule, publishToMessageBus, @@ -10,19 +10,15 @@ import User from "discourse/models/user"; function setupPretender() { pretender.get(`/u/test/private-message-topic-tracking-state`, () => { - return [ - 200, - { "Content-Type": "application/json" }, - [ - { - topic_id: 123, - highest_post_number: 12, - last_read_post_number: 12, - notification_level: 3, - group_ids: [], - }, - ], - ]; + return response([ + { + topic_id: 123, + highest_post_number: 12, + last_read_post_number: 12, + notification_level: 3, + group_ids: [], + }, + ]); }); } diff --git a/plugins/poll/test/javascripts/widgets/discourse-poll-test.js b/plugins/poll/test/javascripts/widgets/discourse-poll-test.js index c9e2a37970c..b75ada49602 100644 --- a/plugins/poll/test/javascripts/widgets/discourse-poll-test.js +++ b/plugins/poll/test/javascripts/widgets/discourse-poll-test.js @@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { click, render } from "@ember/test-helpers"; import { count, exists, query } from "discourse/tests/helpers/qunit-helpers"; import hbs from "htmlbars-inline-precompile"; -import pretender from "discourse/tests/helpers/create-pretender"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import EmberObject from "@ember/object"; import I18n from "I18n"; @@ -15,33 +15,29 @@ module("Integration | Component | Widget | discourse-poll", function (hooks) { hooks.beforeEach(function () { pretender.put("/polls/vote", () => { ++requests; - return [ - 200, - { "Content-Type": "application/json" }, - { - poll: { - name: "poll", - type: "regular", - status: "open", - results: "always", - options: [ - { - id: "1f972d1df351de3ce35a787c89faad29", - html: "yes", - votes: 1, - }, - { - id: "d7ebc3a9beea2e680815a1e4f57d6db6", - html: "no", - votes: 0, - }, - ], - voters: 1, - chart_type: "bar", - }, - vote: ["1f972d1df351de3ce35a787c89faad29"], + return response({ + poll: { + name: "poll", + type: "regular", + status: "open", + results: "always", + options: [ + { + id: "1f972d1df351de3ce35a787c89faad29", + html: "yes", + votes: 1, + }, + { + id: "d7ebc3a9beea2e680815a1e4f57d6db6", + html: "no", + votes: 0, + }, + ], + voters: 1, + chart_type: "bar", }, - ]; + vote: ["1f972d1df351de3ce35a787c89faad29"], + }); }); });