From de2938799b67ee11ba6bdcaadf04185c33f266d1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 10 Jul 2024 13:24:25 +0200 Subject: [PATCH] Initial test --- .../src/api/routes/tests/rowAction.spec.ts | 65 +++++++++++++++++++ .../server/src/tests/utilities/api/index.ts | 3 + .../src/tests/utilities/api/rowAction.ts | 17 +++++ 3 files changed, 85 insertions(+) create mode 100644 packages/server/src/api/routes/tests/rowAction.spec.ts create mode 100644 packages/server/src/tests/utilities/api/rowAction.ts diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts new file mode 100644 index 0000000000..46abadd397 --- /dev/null +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -0,0 +1,65 @@ +import _ from "lodash" +import { Table } from "@budibase/types" +import * as setup from "./utilities" +import { generator } from "@budibase/backend-core/tests" + +describe("/rowsActions", () => { + const config = setup.getConfig() + + let table: Table + + beforeAll(async () => { + await config.init() + + table = await config.api.table.save(setup.structures.basicTable()) + }) + + afterAll(setup.afterAll) + + beforeAll(async () => { + table = await config.api.table.save(setup.structures.basicTable()) + }) + + function unauthorisedTests() { + it("returns unauthorised (401) for unauthenticated requests", async () => { + await config.api.rowAction.save( + table._id!, + {}, + { + status: 401, + body: { + message: "Session not authenticated", + }, + }, + { publicUser: true } + ) + }) + + it("returns forbidden (403) for non-builder users", async () => { + const user = await config.createUser({ + builder: {}, + }) + await config.withUser(user, async () => { + await config.api.rowAction.save(generator.guid(), {}, { status: 403 }) + }) + }) + } + + describe("create", () => { + unauthorisedTests() + + it("rejects when using a non-existing table", async () => { + const res = await config.api.rowAction.save( + table._id!, + {}, + { status: 201 } + ) + + expect(res).toEqual({}) + }) + + it("rejects (404) for a non-existing table", async () => { + await config.api.rowAction.save(generator.guid(), {}, { status: 404 }) + }) + }) +}) diff --git a/packages/server/src/tests/utilities/api/index.ts b/packages/server/src/tests/utilities/api/index.ts index 554fa36588..a19b68a872 100644 --- a/packages/server/src/tests/utilities/api/index.ts +++ b/packages/server/src/tests/utilities/api/index.ts @@ -13,6 +13,7 @@ import { UserAPI } from "./user" import { QueryAPI } from "./query" import { RoleAPI } from "./role" import { TemplateAPI } from "./template" +import { RowActionAPI } from "./rowAction" export default class API { table: TableAPI @@ -29,6 +30,7 @@ export default class API { query: QueryAPI roles: RoleAPI templates: TemplateAPI + rowAction: RowActionAPI constructor(config: TestConfiguration) { this.table = new TableAPI(config) @@ -45,5 +47,6 @@ export default class API { this.query = new QueryAPI(config) this.roles = new RoleAPI(config) this.templates = new TemplateAPI(config) + this.rowAction = new RowActionAPI(config) } } diff --git a/packages/server/src/tests/utilities/api/rowAction.ts b/packages/server/src/tests/utilities/api/rowAction.ts new file mode 100644 index 0000000000..c6b8df5d12 --- /dev/null +++ b/packages/server/src/tests/utilities/api/rowAction.ts @@ -0,0 +1,17 @@ +import { CreateRowActionRequest, Row, RowAction } from "@budibase/types" +import { Expectations, TestAPI } from "./base" + +export class RowActionAPI extends TestAPI { + save = async ( + tableId: string, + rowAction: CreateRowActionRequest, + expectations?: Expectations, + config?: { publicUser?: boolean } + ): Promise => { + return await this._post(`/api/tables/${tableId}/actions`, { + body: rowAction, + expectations, + ...config, + }) + } +}