From 5cd3b9dc88aac5d3792947c52119d860e1c4f78a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 26 Aug 2024 17:26:59 +0200 Subject: [PATCH] Add tests --- .../src/api/controllers/rowAction/crud.ts | 2 +- .../src/api/routes/tests/rowAction.spec.ts | 68 +++++++++---------- packages/server/src/sdk/app/rowActions.ts | 2 +- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index 9b090a9549..c9b7756987 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -26,7 +26,7 @@ export async function find(ctx: Ctx) { return } - const { actions } = await sdk.rowActions.get(table._id!) + const { actions } = await sdk.rowActions.getAll(table._id!) const result: RowActionsResponse = { actions: Object.entries(actions).reduce>( (acc, [key, action]) => ({ diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 0cf41cb36f..d61ea55e3b 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -687,32 +687,24 @@ describe("/rowsActions", () => { rowAction = await createRowAction(tableId, createRowActionRequest()) await config.publish() + tk.travel(Date.now() + 100) }) - unauthorisedTests((expectations, testConfig) => - config.api.rowAction.trigger( - tableId, - rowAction.id, - { - rowId: row._id!, - }, - expectations, - { ...testConfig, useProdApp: true } - ) - ) - - it("can trigger an automation given valid data", async () => { - await config.api.rowAction.trigger(tableId, rowAction.id, { - rowId: row._id!, - }) - + async function getAutomationLogs() { const { data: automationLogs } = await config.doInContext( config.getProdAppId(), async () => - automations.logs.logSearch({ - startDate: await automations.logs.oldestLogDate(), - }) + automations.logs.logSearch({ startDate: new Date().toISOString() }) ) + return automationLogs + } + + it("can trigger an automation given valid data", async () => { + await config.api.rowAction.trigger(tableId, rowAction.id, { + rowId: row._id!, + }) + + const automationLogs = await getAutomationLogs() expect(automationLogs).toEqual([ expect.objectContaining({ automationId: rowAction.automationId, @@ -743,28 +735,36 @@ describe("/rowsActions", () => { { status: 403, body: { - message: `Row action '${rowAction.id}' is not enabled for view '${viewId}'"`, + message: `Row action '${rowAction.id}' is not enabled for view '${viewId}'`, }, } ) - const { data: automationLogs } = await config.doInContext( - config.getProdAppId(), - async () => - automations.logs.logSearch({ - startDate: await automations.logs.oldestLogDate(), - }) + const automationLogs = await getAutomationLogs() + expect(automationLogs).toEqual([]) + }) + + it("triggers from an allowed view", async () => { + const viewId = ( + await config.api.viewV2.create( + setup.structures.viewV2.createRequest(tableId) + ) + ).id + + await config.api.rowAction.setViewPermission( + tableId, + viewId, + rowAction.id ) + + await config.api.rowAction.trigger(tableId, rowAction.id, { + rowId: row._id!, + }) + + const automationLogs = await getAutomationLogs() expect(automationLogs).toEqual([ expect.objectContaining({ automationId: rowAction.automationId, - trigger: expect.objectContaining({ - outputs: { - fields: {}, - row: await config.api.row.get(tableId, row._id!), - table: await config.api.table.get(tableId), - }, - }), }), ]) }) diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index e8fa3d41f0..3d558f5345 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -99,7 +99,7 @@ export async function get(tableId: string, rowActionId: string) { return rowAction } -async function getAll(tableId: string) { +export async function getAll(tableId: string) { const db = context.getAppDB() const rowActionsId = generateRowActionsID(tableId) return await db.get(rowActionsId)