diff --git a/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts new file mode 100644 index 0000000000..beeaab3608 --- /dev/null +++ b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts @@ -0,0 +1,122 @@ +import { generator } from "@budibase/backend-core/tests" +import { BBRequest, FieldType, Row, Table } from "@budibase/types" +import * as utils from "../../db/utils" +import trimViewRowInfoMiddleware from "../trimViewRowInfo" + +jest.mock("../../sdk", () => ({ + views: { + ...jest.requireActual("../../sdk/app/views"), + get: jest.fn(), + }, + tables: { + getTable: jest.fn(), + }, +})) + +import sdk from "../../sdk" +import { BaseContext, Next } from "koa" + +const mockGetView = sdk.views.get as jest.MockedFunction +const mockGetTable = sdk.tables.getTable as jest.MockedFunction< + typeof sdk.tables.getTable +> + +class TestConfiguration { + next: Next + throw: (status: number, message: string) => never + middleware: typeof trimViewRowInfoMiddleware + params: Record + request?: Pick, "body"> + + constructor() { + this.next = jest.fn() + this.throw = (_status: any, message: any) => { + throw new Error(message) + } + this.params = {} + + this.middleware = trimViewRowInfoMiddleware + } + + executeMiddleware(viewId: string, ctxRequestBody: Row) { + this.request = { + body: ctxRequestBody, + } + this.params.viewId = viewId + return this.middleware( + { + request: this.request as any, + next: this.next, + throw: this.throw as any, + params: this.params, + } as any, + this.next + ) + } + + afterEach() { + jest.clearAllMocks() + } +} + +describe("trimViewRowInfo middleware", () => { + let config: TestConfiguration + + beforeEach(() => { + config = new TestConfiguration() + }) + + afterEach(() => { + config.afterEach() + }) + + const table: Table = { + _id: utils.generateTableID(), + name: generator.word(), + type: "table", + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + age: { + name: "age", + type: FieldType.NUMBER, + }, + address: { + name: "address", + type: FieldType.STRING, + }, + }, + } + + beforeEach(() => { + jest.resetAllMocks() + }) + + it("when no columns are defined, same data is returned", async () => { + mockGetView.mockResolvedValue({ + version: 2, + id: generator.guid(), + name: generator.guid(), + tableId: generator.guid(), + }) + mockGetTable.mockResolvedValue(table) + + const viewId = utils.generateViewID(table._id!) + const data = { + _id: generator.guid(), + name: generator.name(), + age: generator.age(), + address: generator.address(), + } + + await config.executeMiddleware(viewId, { + _viewId: viewId, + ...data, + }) + + expect(config.request?.body).toEqual(data) + expect(config.params.tableId).toEqual(table._id) + }) +}) diff --git a/packages/server/src/middleware/trimViewRowInfo.ts b/packages/server/src/middleware/trimViewRowInfo.ts index 66d6d32ed6..5dbe7a6da0 100644 --- a/packages/server/src/middleware/trimViewRowInfo.ts +++ b/packages/server/src/middleware/trimViewRowInfo.ts @@ -2,22 +2,23 @@ import { Ctx, Row } from "@budibase/types" import * as utils from "../db/utils" import sdk from "../sdk" import { db } from "@budibase/backend-core" +import { Next } from "koa" -export default () => async (ctx: Ctx, next: any) => { +export default async (ctx: Ctx, next: Next) => { const { body } = ctx.request const { _viewId: viewId } = body if (!viewId) { ctx.throw(400, "_viewId is required") } - const { tableId } = utils.extractViewInfoFromID(viewId) + const { tableId } = utils.extractViewInfoFromID(ctx.params.viewId) const { _viewId, ...trimmedView } = await trimViewFields( viewId, tableId, body ) ctx.request.body = trimmedView - ctx.params.tableId = body.tableId + ctx.params.tableId = tableId return next() }