Fixing test and updating middleware to match requirements.
This commit is contained in:
parent
3b7a5c0392
commit
0fb6f8312a
|
@ -102,12 +102,13 @@ describe("trimViewRowInfo middleware", () => {
|
||||||
address: generator.address(),
|
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({
|
mockGetView.mockResolvedValue({
|
||||||
version: 2,
|
version: 2,
|
||||||
id: viewId,
|
id: viewId,
|
||||||
name: generator.guid(),
|
name: generator.guid(),
|
||||||
tableId: table._id!,
|
tableId: table._id!,
|
||||||
|
schema: {},
|
||||||
})
|
})
|
||||||
|
|
||||||
const data = getRandomData()
|
const data = getRandomData()
|
||||||
|
@ -116,7 +117,9 @@ describe("trimViewRowInfo middleware", () => {
|
||||||
...data,
|
...data,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(config.request?.body).toEqual(data)
|
expect(config.request?.body).toEqual({
|
||||||
|
_id: data._id,
|
||||||
|
})
|
||||||
expect(config.params.sourceId).toEqual(table._id)
|
expect(config.params.sourceId).toEqual(table._id)
|
||||||
|
|
||||||
expect(config.next).toBeCalledTimes(1)
|
expect(config.next).toBeCalledTimes(1)
|
||||||
|
@ -129,7 +132,10 @@ describe("trimViewRowInfo middleware", () => {
|
||||||
id: viewId,
|
id: viewId,
|
||||||
name: generator.guid(),
|
name: generator.guid(),
|
||||||
tableId: table._id!,
|
tableId: table._id!,
|
||||||
columns: ["name", "address"],
|
schema: {
|
||||||
|
name: {},
|
||||||
|
address: {},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const data = getRandomData()
|
const data = getRandomData()
|
||||||
|
|
|
@ -23,7 +23,7 @@ export default async (ctx: Ctx<Row>, next: Next) => {
|
||||||
|
|
||||||
// don't need to trim delete requests
|
// don't need to trim delete requests
|
||||||
if (ctx?.method?.toLowerCase() !== "delete") {
|
if (ctx?.method?.toLowerCase() !== "delete") {
|
||||||
await trimViewFields(ctx.request.body, viewId, tableId)
|
await trimViewFields(ctx.request.body, viewId)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.params.sourceId = tableId
|
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
|
// have to mutate the koa context, can't return
|
||||||
export async function trimViewFields<T extends Row>(
|
export async function trimViewFields<T extends Row>(
|
||||||
body: Row,
|
body: Row,
|
||||||
viewId: string,
|
viewId: string
|
||||||
tableId: string
|
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const view = await sdk.views.get(viewId)
|
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 = [
|
const allowedKeys = [
|
||||||
...Object.keys(schema),
|
...Object.keys(view?.schema || {}),
|
||||||
...db.CONSTANT_EXTERNAL_ROW_COLS,
|
...db.CONSTANT_EXTERNAL_ROW_COLS,
|
||||||
...db.CONSTANT_INTERNAL_ROW_COLS,
|
...db.CONSTANT_INTERNAL_ROW_COLS,
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue