From 3c684d8cb5ca2d4af9065462c6987b54712fa752 Mon Sep 17 00:00:00 2001 From: Dean Date: Mon, 4 Mar 2024 15:11:26 +0000 Subject: [PATCH] Further refactoring of the tests to bring them up to date --- .../src/api/routes/tests/application.spec.ts | 65 ++++++++++++------- .../src/tests/utilities/TestConfiguration.ts | 11 ++++ .../src/tests/utilities/api/application.ts | 19 ------ 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 5a6c30d7a8..33585366c8 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -271,53 +271,70 @@ describe("/applications", () => { await config.createApp("to-dupe") const sourceAppId = config.getProdAppId() - const resp = await config.api.application.duplicateApp(sourceAppId, { + const resp = await config.duplicateApp(sourceAppId, { name: "to-dupe copy", url: "/to-dupe-copy", }) - expect(resp.status).toEqual("200") - expect(events.app.duplicated).toBeCalled() - expect(resp.body.duplicateAppId).toBeDefined() - expect(resp.body.sourceAppId).toEqual(sourceAppId) - expect(resp.body.duplicateAppId).not.toEqual(sourceAppId) + expect(resp.duplicateAppId).toBeDefined() + expect(resp.sourceAppId).toEqual(sourceAppId) + expect(resp.duplicateAppId).not.toEqual(sourceAppId) }) it("should reject an unknown app id with a 404", async () => { - const resp = await config.api.application.duplicateApp( - "app_1234_not_real", - { + let dupeError + try { + await config.duplicateApp("app_fake", { name: "to-dupe copy", url: "/to-dupe-copy", - } - ) + }) + } catch (err: any) { + dupeError = err + } - expect(resp.status).toEqual("404") + expect(dupeError).toBeDefined() + expect(dupeError.message).toEqual("Error 404 - Source app not found") }) it("should reject with a known name", async () => { await config.createApp("known name") const sourceAppId = config.getProdAppId() - const resp = await config.api.application.duplicateApp(sourceAppId, { - name: "known name", - url: "/known-name", - }) - expect(resp.status).toEqual("400") - expect(resp.body.message).toEqual("App name is already in use.") + let dupeError + try { + await config.duplicateApp(sourceAppId, { + name: "known name", + url: "/known-name", + }) + } catch (err: any) { + dupeError = err + } + + expect(dupeError).toBeDefined() + expect(dupeError.message).toEqual( + "Error 400 - App name is already in use." + ) }) it("should reject with a known url", async () => { await config.createApp("known-url") const sourceAppId = config.getProdAppId() - const resp = await config.api.application.duplicateApp(sourceAppId, { - name: "this is fine", - url: "/known-url", - }) - expect(resp.status).toEqual("400") - expect(resp.body.message).toEqual("App URL is already in use.") + let dupeError + try { + await config.duplicateApp(sourceAppId, { + name: "this is fine", + url: "/known-url", + }) + } catch (err: any) { + dupeError = err + } + + expect(dupeError).toBeDefined() + expect(dupeError.message).toEqual( + "Error 400 - App URL is already in use." + ) }) }) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index f7b8a703bb..aebb9e437d 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -575,6 +575,7 @@ export default class TestConfiguration { async () => (await this._req(appController.create, { name: appName, + url, })) as App ) this.appId = this.app.appId @@ -589,6 +590,16 @@ export default class TestConfiguration { }) } + async duplicateApp(appId: string, fields: object) { + return context.doInTenant( + this.tenantId!, + async () => + await this._req(appController.duplicateApp, fields, { + appId, + }) + ) + } + async publish() { await this._req(deployController.publishApp) // @ts-ignore diff --git a/packages/server/src/tests/utilities/api/application.ts b/packages/server/src/tests/utilities/api/application.ts index 5728483b82..3951bba667 100644 --- a/packages/server/src/tests/utilities/api/application.ts +++ b/packages/server/src/tests/utilities/api/application.ts @@ -160,25 +160,6 @@ export class ApplicationAPI extends TestAPI { } } - duplicateApp = async (appId: string, fields: object): Promise => { - let headers = { - ...this.config.defaultHeaders(), - [constants.Header.APP_ID]: appId, - } - const req = this.request - .post(`/api/applications/${appId}/duplicate`) - .set(headers) - .expect("Content-Type", /json/) - - for (const [key, value] of Object.entries(fields)) { - req.field(key, value) - } - - const response = await req - - return response - } - revertClient = async (appId: string): Promise => { // While the revertClient endpoint does take an :appId parameter, it doesn't // use it. It uses the appId from the context.