From bb6bd1711af275de743d2d77497e46ab1eb818d2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 4 Oct 2024 13:58:32 +0200 Subject: [PATCH] Add extra tests --- .../src/api/controllers/rowAction/crud.ts | 3 - .../src/api/routes/tests/rowAction.spec.ts | 82 +++++++++++++++++++ .../src/tests/utilities/api/rowAction.ts | 36 ++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index 9a10147b09..67b6d9383e 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -178,9 +178,6 @@ function flattenAllowedSources( viewId => permissions.views[viewId].runAllowed ) ) - if (!allowedPermissions.length) { - return undefined - } return allowedPermissions } diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index aad9074b31..bc21d199f7 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -524,6 +524,88 @@ describe("/rowsActions", () => { }) }) + describe("set/unsetTablePermission", () => { + describe.each([ + ["setTablePermission", config.api.rowAction.setTablePermission], + ["unsetTablePermission", config.api.rowAction.unsetTablePermission], + ])("unauthorisedTests for %s", (__, delegateTest) => { + unauthorisedTests((expectations, testConfig) => + delegateTest(tableId, generator.guid(), expectations, testConfig) + ) + }) + + let tableIdForDescribe: string + let actionId1: string, actionId2: string + + beforeAll(async () => { + tableIdForDescribe = tableId + for (const rowAction of createRowActionRequests(3)) { + await createRowAction(tableId, rowAction) + } + const persisted = await config.api.rowAction.find(tableId) + + const actions = _.sampleSize(Object.keys(persisted.actions), 2) + actionId1 = actions[0] + actionId2 = actions[1] + }) + + beforeEach(() => { + // Hack to reuse tables for these given tests + tableId = tableIdForDescribe + }) + + it("can set table permission", async () => { + await config.api.rowAction.unsetTablePermission(tableId, actionId1) + await config.api.rowAction.unsetTablePermission(tableId, actionId2) + const actionResult = await config.api.rowAction.setTablePermission( + tableId, + actionId1 + ) + const expectedAction1 = expect.objectContaining({ + allowedSources: [tableId], + }) + + const expectedActions = expect.objectContaining({ + [actionId1]: expectedAction1, + [actionId2]: expect.objectContaining({ + allowedSources: [], + }), + }) + expect(actionResult).toEqual(expectedAction1) + expect((await config.api.rowAction.find(tableId)).actions).toEqual( + expectedActions + ) + }) + + it("can unset table permission", async () => { + const actionResult = await config.api.rowAction.unsetTablePermission( + tableId, + actionId1 + ) + + const expectedAction = expect.objectContaining({ + allowedSources: [], + }) + expect(actionResult).toEqual(expectedAction) + expect( + (await config.api.rowAction.find(tableId)).actions[actionId1] + ).toEqual(expectedAction) + }) + + it.each([ + ["setTablePermission", config.api.rowAction.setTablePermission], + ["unsetTablePermission", config.api.rowAction.unsetTablePermission], + ])( + "cannot update permission for unexisting tables (%s)", + async (__, delegateTest) => { + const tableId = generator.guid() + await delegateTest(tableId, actionId1, { + status: 404, + }) + } + ) + }) + describe("set/unsetViewPermission", () => { describe.each([ ["setViewPermission", config.api.rowAction.setViewPermission], diff --git a/packages/server/src/tests/utilities/api/rowAction.ts b/packages/server/src/tests/utilities/api/rowAction.ts index 1adb60d235..0fd5936257 100644 --- a/packages/server/src/tests/utilities/api/rowAction.ts +++ b/packages/server/src/tests/utilities/api/rowAction.ts @@ -72,6 +72,42 @@ export class RowActionAPI extends TestAPI { ) } + setTablePermission = async ( + tableId: string, + rowActionId: string, + expectations?: Expectations, + config?: { publicUser?: boolean } + ) => { + return await this._post( + `/api/tables/${tableId}/actions/${rowActionId}/permissions`, + { + expectations: { + status: 200, + ...expectations, + }, + ...config, + } + ) + } + + unsetTablePermission = async ( + tableId: string, + rowActionId: string, + expectations?: Expectations, + config?: { publicUser?: boolean } + ) => { + return await this._delete( + `/api/tables/${tableId}/actions/${rowActionId}/permissions`, + { + expectations: { + status: 200, + ...expectations, + }, + ...config, + } + ) + } + setViewPermission = async ( tableId: string, viewId: string,