From d6fd5933a4e6b096ac7871e7eaa04e0d18400bb2 Mon Sep 17 00:00:00 2001 From: Dean Date: Mon, 11 Mar 2024 14:22:41 +0000 Subject: [PATCH] PR feedback and bug fix for server tests being completely skipped --- .../server/src/api/controllers/application.ts | 1 - .../src/api/routes/tests/application.spec.ts | 196 +++++++++--------- packages/types/src/api/web/application.ts | 1 - 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index 47cc08c738..0a84451bac 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -697,7 +697,6 @@ export async function duplicateApp( } ctx.body = { - message: "app duplicated", duplicateAppId: newApplication?.appId, sourceAppId, } diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 2a9e723423..04edc91bf6 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -34,6 +34,96 @@ describe("/applications", () => { jest.clearAllMocks() }) + // These need to go first for the app totals to make sense + describe("permissions", () => { + it("should only return apps a user has access to", async () => { + let user = await config.createUser({ + builder: { global: false }, + admin: { global: false }, + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(0) + }) + + user = await config.globalUser({ + ...user, + builder: { + apps: [config.getProdAppId()], + }, + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(1) + }) + }) + + it("should only return apps a user has access to through a custom role", async () => { + let user = await config.createUser({ + builder: { global: false }, + admin: { global: false }, + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(0) + }) + + const role = await config.api.roles.save({ + name: "Test", + inherits: "PUBLIC", + permissionId: "read_only", + version: "name", + }) + + user = await config.globalUser({ + ...user, + roles: { + [config.getProdAppId()]: role.name, + }, + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(1) + }) + }) + + it("should only return apps a user has access to through a custom role on a group", async () => { + let user = await config.createUser({ + builder: { global: false }, + admin: { global: false }, + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(0) + }) + + const roleName = uuid.v4().replace(/-/g, "") + const role = await config.api.roles.save({ + name: roleName, + inherits: "PUBLIC", + permissionId: "read_only", + version: "name", + }) + + const group = await config.createGroup(role._id!) + + user = await config.globalUser({ + ...user, + userGroups: [group._id!], + }) + + await config.withUser(user, async () => { + const apps = await config.api.application.fetch() + expect(apps).toHaveLength(1) + }) + }) + }) + describe("create", () => { it("creates empty app", async () => { const app = await config.api.application.create({ name: utils.newid() }) @@ -96,13 +186,16 @@ describe("/applications", () => { }) it("should reject with a known name", async () => { - await config.api.application.create({ name: app.name }, { status: 400 }) + await config.api.application.create( + { name: app.name }, + { body: { message: "App name is already in use." }, status: 400 } + ) }) it("should reject with a known url", async () => { await config.api.application.create( { name: "made up", url: app?.url! }, - { status: 400 } + { body: { message: "App URL is already in use." }, status: 400 } ) }) }) @@ -279,10 +372,9 @@ describe("/applications", () => { name: app.name, url: "/known-name", }, - { status: 400 } + { body: { message: "App name is already in use." }, status: 400 } ) - - expect(resp.message).toEqual("App name is already in use.") + expect(events.app.duplicated).not.toBeCalled() }) it("should reject with a known url", async () => { @@ -292,10 +384,9 @@ describe("/applications", () => { name: "this is fine", url: app.url, }, - { status: 400 } + { body: { message: "App URL is already in use." }, status: 400 } ) - - expect(resp.message).toEqual("App URL is already in use.") + expect(events.app.duplicated).not.toBeCalled() }) }) @@ -319,93 +410,4 @@ describe("/applications", () => { expect(devLogs.data.length).toBe(0) }) }) - - describe("permissions", () => { - it("should only return apps a user has access to", async () => { - let user = await config.createUser({ - builder: { global: false }, - admin: { global: false }, - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(0) - }) - - user = await config.globalUser({ - ...user, - builder: { - apps: [config.getProdAppId()], - }, - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(1) - }) - }) - - it("should only return apps a user has access to through a custom role", async () => { - let user = await config.createUser({ - builder: { global: false }, - admin: { global: false }, - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(0) - }) - - const role = await config.api.roles.save({ - name: "Test", - inherits: "PUBLIC", - permissionId: "read_only", - version: "name", - }) - - user = await config.globalUser({ - ...user, - roles: { - [config.getProdAppId()]: role.name, - }, - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(1) - }) - }) - - it.only("should only return apps a user has access to through a custom role on a group", async () => { - let user = await config.createUser({ - builder: { global: false }, - admin: { global: false }, - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(0) - }) - - const roleName = uuid.v4().replace(/-/g, "") - const role = await config.api.roles.save({ - name: roleName, - inherits: "PUBLIC", - permissionId: "read_only", - version: "name", - }) - - const group = await config.createGroup(role._id!) - - user = await config.globalUser({ - ...user, - userGroups: [group._id!], - }) - - await config.withUser(user, async () => { - const apps = await config.api.application.fetch() - expect(apps).toHaveLength(1) - }) - }) - }) }) diff --git a/packages/types/src/api/web/application.ts b/packages/types/src/api/web/application.ts index b77e81880b..68c349be5a 100644 --- a/packages/types/src/api/web/application.ts +++ b/packages/types/src/api/web/application.ts @@ -22,7 +22,6 @@ export interface DuplicateAppRequest { export interface DuplicateAppResponse { duplicateAppId: string sourceAppId: string - message: string } export interface FetchAppDefinitionResponse {