Fixing test and updating middleware to match requirements.

This commit is contained in:
mike12345567 2023-08-14 12:10:16 +01:00
parent 3b7a5c0392
commit 0fb6f8312a
2 changed files with 12 additions and 13 deletions

View File

@ -102,12 +102,13 @@ describe("trimViewRowInfo middleware", () => {
address: generator.address(),
})
it("when no columns are defined, same data is returned", async () => {
it("when no columns are defined, don't allow anything", async () => {
mockGetView.mockResolvedValue({
version: 2,
id: viewId,
name: generator.guid(),
tableId: table._id!,
schema: {},
})
const data = getRandomData()
@ -116,7 +117,9 @@ describe("trimViewRowInfo middleware", () => {
...data,
})
expect(config.request?.body).toEqual(data)
expect(config.request?.body).toEqual({
_id: data._id,
})
expect(config.params.sourceId).toEqual(table._id)
expect(config.next).toBeCalledTimes(1)
@ -129,7 +132,10 @@ describe("trimViewRowInfo middleware", () => {
id: viewId,
name: generator.guid(),
tableId: table._id!,
columns: ["name", "address"],
schema: {
name: {},
address: {},
},
})
const data = getRandomData()

View File

@ -23,7 +23,7 @@ export default async (ctx: Ctx<Row>, next: Next) => {
// don't need to trim delete requests
if (ctx?.method?.toLowerCase() !== "delete") {
await trimViewFields(ctx.request.body, viewId, tableId)
await trimViewFields(ctx.request.body, viewId)
}
ctx.params.sourceId = tableId
@ -34,18 +34,11 @@ export default async (ctx: Ctx<Row>, next: Next) => {
// have to mutate the koa context, can't return
export async function trimViewFields<T extends Row>(
body: Row,
viewId: string,
tableId: string
viewId: string
): Promise<void> {
const view = await sdk.views.get(viewId)
if (!view?.schema || !Object.keys(view.schema).length) {
return
}
const table = await sdk.tables.getTable(tableId)
const { schema } = sdk.views.enrichSchema(view!, table.schema)
const allowedKeys = [
...Object.keys(schema),
...Object.keys(view?.schema || {}),
...db.CONSTANT_EXTERNAL_ROW_COLS,
...db.CONSTANT_INTERNAL_ROW_COLS,
]