From 5f27e3a7427b7e74e6f92e9569dea9339cc62cfd Mon Sep 17 00:00:00 2001 From: Dean Date: Mon, 4 Mar 2024 12:20:34 +0000 Subject: [PATCH] Refactored app duplicate tests --- .../src/api/routes/tests/application.spec.ts | 52 +++++++++---------- .../src/tests/utilities/api/application.ts | 19 +++++++ 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 3a69cf34d0..5a6c30d7a8 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -271,13 +271,12 @@ describe("/applications", () => { await config.createApp("to-dupe") const sourceAppId = config.getProdAppId() - const resp = await request - .post(`/api/applications/${sourceAppId}/duplicate`) - .field("name", "to-dupe copy") - .field("url", "/to-dupe-copy") - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + const resp = await config.api.application.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() @@ -286,39 +285,38 @@ describe("/applications", () => { }) it("should reject an unknown app id with a 404", async () => { - await request - .post(`/api/applications/app_1234_not_real/duplicate`) - .field("name", "to-dupe copy") - .field("url", "/to-dupe-copy") - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(404) + const resp = await config.api.application.duplicateApp( + "app_1234_not_real", + { + name: "to-dupe copy", + url: "/to-dupe-copy", + } + ) + + expect(resp.status).toEqual("404") }) it("should reject with a known name", async () => { await config.createApp("known name") const sourceAppId = config.getProdAppId() - const resp = await request - .post(`/api/applications/${sourceAppId}/duplicate`) - .field("name", "known name") - .field("url", "/known-name") - .set(config.defaultHeaders()) - .expect(400) + 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.") }) 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", + }) - const resp = await request - .post(`/api/applications/${sourceAppId}/duplicate`) - .field("name", "this is fine") - .field("url", "/known-url") - .set(config.defaultHeaders()) - .expect(400) - + expect(resp.status).toEqual("400") expect(resp.body.message).toEqual("App URL is already in use.") }) }) diff --git a/packages/server/src/tests/utilities/api/application.ts b/packages/server/src/tests/utilities/api/application.ts index 3951bba667..5728483b82 100644 --- a/packages/server/src/tests/utilities/api/application.ts +++ b/packages/server/src/tests/utilities/api/application.ts @@ -160,6 +160,25 @@ 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.