Test noViewDataMiddleware
This commit is contained in:
parent
d8df917772
commit
b3b4a6a177
|
@ -1,8 +1,8 @@
|
||||||
import { Ctx, Row } from "@budibase/types"
|
import { Ctx, Row } from "@budibase/types"
|
||||||
|
|
||||||
export default () => async (ctx: Ctx<Row>, next: any) => {
|
export default async (ctx: Ctx<Row>, next: any) => {
|
||||||
if (ctx.request.body._viewId) {
|
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()
|
return next()
|
||||||
|
|
|
@ -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<string, any>
|
||||||
|
request?: Pick<BBRequest<Row>, "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()
|
||||||
|
})
|
||||||
|
})
|
|
@ -14,7 +14,7 @@ jest.mock("../../sdk", () => ({
|
||||||
}))
|
}))
|
||||||
|
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
import { BaseContext, Next } from "koa"
|
import { Next } from "koa"
|
||||||
|
|
||||||
const mockGetView = sdk.views.get as jest.MockedFunction<typeof sdk.views.get>
|
const mockGetView = sdk.views.get as jest.MockedFunction<typeof sdk.views.get>
|
||||||
const mockGetTable = sdk.tables.getTable as jest.MockedFunction<
|
const mockGetTable = sdk.tables.getTable as jest.MockedFunction<
|
||||||
|
@ -30,9 +30,7 @@ class TestConfiguration {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.next = jest.fn()
|
this.next = jest.fn()
|
||||||
this.throw = jest.fn().mockImplementation((_status: any, message: any) => {
|
this.throw = jest.fn()
|
||||||
throw new Error(message)
|
|
||||||
})
|
|
||||||
this.params = {}
|
this.params = {}
|
||||||
|
|
||||||
this.middleware = trimViewRowInfoMiddleware
|
this.middleware = trimViewRowInfoMiddleware
|
||||||
|
@ -159,6 +157,7 @@ describe("trimViewRowInfo middleware", () => {
|
||||||
|
|
||||||
expect(config.throw).toBeCalledTimes(1)
|
expect(config.throw).toBeCalledTimes(1)
|
||||||
expect(config.throw).toBeCalledWith(400, "_viewId is required")
|
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 () => {
|
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).toBeCalledTimes(1)
|
||||||
expect(config.throw).toBeCalledWith(400, "viewId path is required")
|
expect(config.throw).toBeCalledWith(400, "viewId path is required")
|
||||||
|
expect(config.next).not.toBeCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue