diff --git a/packages/server/src/middleware/noViewData.ts b/packages/server/src/middleware/noViewData.ts index 5a2bb45012..809424b6bf 100644 --- a/packages/server/src/middleware/noViewData.ts +++ b/packages/server/src/middleware/noViewData.ts @@ -1,8 +1,8 @@ import { Ctx, Row } from "@budibase/types" -export default () => async (ctx: Ctx, next: any) => { +export default async (ctx: Ctx, next: any) => { if (ctx.request.body._viewId) { - ctx.throw(400, "Table row endpoints cannot contain view info") + return ctx.throw(400, "Table row endpoints cannot contain view info") } return next() diff --git a/packages/server/src/middleware/tests/noViewData.spec.ts b/packages/server/src/middleware/tests/noViewData.spec.ts new file mode 100644 index 0000000000..54b0ca8ff8 --- /dev/null +++ b/packages/server/src/middleware/tests/noViewData.spec.ts @@ -0,0 +1,83 @@ +import { generator } from "@budibase/backend-core/tests" +import { BBRequest, FieldType, Row, Table } from "@budibase/types" +import { Next } from "koa" +import * as utils from "../../db/utils" +import noViewDataMiddleware from "../noViewData" + +class TestConfiguration { + next: Next + throw: jest.Mock<(status: number, message: string) => never> + middleware: typeof noViewDataMiddleware + params: Record + request?: Pick, "body"> + + constructor() { + this.next = jest.fn() + this.throw = jest.fn() + this.params = {} + + this.middleware = noViewDataMiddleware + } + + executeMiddleware(ctxRequestBody: Row) { + this.request = { + body: ctxRequestBody, + } + return this.middleware( + { + request: this.request as any, + throw: this.throw as any, + params: this.params, + } as any, + this.next + ) + } + + afterEach() { + jest.clearAllMocks() + } +} + +describe("noViewData middleware", () => { + let config: TestConfiguration + + beforeEach(() => { + config = new TestConfiguration() + }) + + afterEach(() => { + config.afterEach() + }) + + const getRandomData = () => ({ + _id: generator.guid(), + name: generator.name(), + age: generator.age(), + address: generator.address(), + }) + + it("it should pass without view id data", async () => { + const data = getRandomData() + await config.executeMiddleware({ + ...data, + }) + + expect(config.next).toBeCalledTimes(1) + expect(config.throw).not.toBeCalled() + }) + + it("it should throw an error if _viewid is provided", async () => { + const data = getRandomData() + await config.executeMiddleware({ + _viewId: generator.guid(), + ...data, + }) + + expect(config.throw).toBeCalledTimes(1) + expect(config.throw).toBeCalledWith( + 400, + "Table row endpoints cannot contain view info" + ) + expect(config.next).not.toBeCalled() + }) +}) diff --git a/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts index aba6817e5c..89b831b647 100644 --- a/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts +++ b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts @@ -14,7 +14,7 @@ jest.mock("../../sdk", () => ({ })) import sdk from "../../sdk" -import { BaseContext, Next } from "koa" +import { Next } from "koa" const mockGetView = sdk.views.get as jest.MockedFunction const mockGetTable = sdk.tables.getTable as jest.MockedFunction< @@ -30,9 +30,7 @@ class TestConfiguration { constructor() { this.next = jest.fn() - this.throw = jest.fn().mockImplementation((_status: any, message: any) => { - throw new Error(message) - }) + this.throw = jest.fn() this.params = {} this.middleware = trimViewRowInfoMiddleware @@ -159,6 +157,7 @@ describe("trimViewRowInfo middleware", () => { expect(config.throw).toBeCalledTimes(1) expect(config.throw).toBeCalledWith(400, "_viewId is required") + expect(config.next).not.toBeCalled() }) it("it should throw an error if no viewid is provided on the parameters", async () => { @@ -170,5 +169,6 @@ describe("trimViewRowInfo middleware", () => { expect(config.throw).toBeCalledTimes(1) expect(config.throw).toBeCalledWith(400, "viewId path is required") + expect(config.next).not.toBeCalled() }) })