From 4bbb1b0289b423ce5c6c2800813f61548d11aefe Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 12 Jul 2023 18:01:46 +0200 Subject: [PATCH] Refactor paths --- .../server/src/api/controllers/view/views.ts | 11 +++++--- .../server/src/api/routes/tests/view.spec.ts | 27 ++++++++++++++----- packages/server/src/api/routes/view.ts | 4 +-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/view/views.ts b/packages/server/src/api/controllers/view/views.ts index b2aa389776..772bb2140b 100644 --- a/packages/server/src/api/controllers/view/views.ts +++ b/packages/server/src/api/controllers/view/views.ts @@ -7,12 +7,17 @@ export async function fetch(ctx: Ctx) { } export async function find(ctx: Ctx) { - const viewId = `${DocumentType.VIEW}${SEPARATOR}${ctx.params.viewId}` - ctx.body = await sdk.views.get(viewId) + const { tableId, viewId } = ctx.params + + const result = await sdk.views.get(viewId) + if (result?.tableId !== tableId) { + ctx.throw(404) + } + ctx.body = result } export async function findByTable(ctx: Ctx) { - const tableId = `${DocumentType.TABLE}${SEPARATOR}${ctx.params.tableId}` + const { tableId } = ctx.params ctx.body = { views: await sdk.views.findByTable(tableId) } } diff --git a/packages/server/src/api/routes/tests/view.spec.ts b/packages/server/src/api/routes/tests/view.spec.ts index 9691a418bc..e6e841f850 100644 --- a/packages/server/src/api/routes/tests/view.spec.ts +++ b/packages/server/src/api/routes/tests/view.spec.ts @@ -1,6 +1,6 @@ import * as setup from "./utilities" import { FieldType, Table, ViewV2 } from "@budibase/types" -import { generator } from "@budibase/backend-core/tests" +import { generator, structures } from "@budibase/backend-core/tests" import sdk from "../../../sdk" function priceTable(): Table { @@ -45,12 +45,17 @@ describe("/views/v2", () => { .expect(200) } - const getView = async (viewId: string) => { + const getView = ({ + tableId, + viewId, + }: { + tableId: string + viewId: string + }) => { return request - .get(`/api/views/v2/${viewId}`) + .get(`/api/views/v2/${tableId}/${viewId}`) .set(config.defaultHeaders()) .expect("Content-Type", /json/) - .expect(200) } function createView(tableId: string): ViewV2 { @@ -114,8 +119,11 @@ describe("/views/v2", () => { view = (await saveView(createView(table._id!))).body }) - it("persist the view when the view is successfully created", async () => { - const res = await getView(view._id) + it("can fetch the expected view", async () => { + const res = await getView({ + tableId: view.tableId, + viewId: view._id, + }).expect(200) expect(res.status).toBe(200) expect(res.body._id).toBeDefined() @@ -127,6 +135,13 @@ describe("/views/v2", () => { updatedAt: expect.any(String), }) }) + + it("will return 404 if the wrong table id is provided", async () => { + await getView({ + tableId: structures.generator.guid(), + viewId: view._id, + }).expect(404) + }) }) describe("create", () => { diff --git a/packages/server/src/api/routes/view.ts b/packages/server/src/api/routes/view.ts index 837306b92b..4eb138e120 100644 --- a/packages/server/src/api/routes/view.ts +++ b/packages/server/src/api/routes/view.ts @@ -14,12 +14,12 @@ router viewController.v2.fetch ) .get( - `/api/views/v2/${DocumentType.TABLE}${SEPARATOR}:tableId`, + `/api/views/v2/:tableId`, authorized(permissions.BUILDER), viewController.v2.findByTable ) .get( - `/api/views/v2/${DocumentType.VIEW}${SEPARATOR}:viewId`, + `/api/views/v2/:tableId/:viewId`, authorized(permissions.BUILDER), viewController.v2.find )