Fix tests
This commit is contained in:
parent
ad8fb01657
commit
d16110ca73
|
@ -1,28 +1,20 @@
|
||||||
jest.mock("@budibase/backend-core", () => ({
|
jest.mock("../../sdk/app/permissions", () => ({
|
||||||
...jest.requireActual("@budibase/backend-core"),
|
...jest.requireActual("../../sdk/app/permissions"),
|
||||||
roles: {
|
getResourcePerms: jest.fn().mockResolvedValue([]),
|
||||||
...jest.requireActual("@budibase/backend-core").roles,
|
|
||||||
getRequiredResourceRole: jest.fn().mockResolvedValue([]),
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
jest.mock("../../environment", () => ({
|
|
||||||
prod: false,
|
|
||||||
isTest: () => true,
|
|
||||||
// @ts-ignore
|
|
||||||
isProd: () => this.prod,
|
|
||||||
_set: function (_key: string, value: string) {
|
|
||||||
this.prod = value === "production"
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
import { PermissionType, PermissionLevel } from "@budibase/types"
|
import {
|
||||||
|
PermissionType,
|
||||||
|
PermissionLevel,
|
||||||
|
PermissionSource,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
import authorizedMiddleware from "../authorized"
|
import authorizedMiddleware from "../authorized"
|
||||||
import env from "../../environment"
|
import env from "../../environment"
|
||||||
import { generateTableID, generateViewID } from "../../db/utils"
|
import { generateTableID, generateViewID } from "../../db/utils"
|
||||||
import { roles } from "@budibase/backend-core"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import { mocks } from "@budibase/backend-core/tests"
|
|
||||||
import { initProMocks } from "../../tests/utilities/mocks/pro"
|
import { initProMocks } from "../../tests/utilities/mocks/pro"
|
||||||
|
import { getResourcePerms } from "../../sdk/app/permissions"
|
||||||
|
|
||||||
const APP_ID = ""
|
const APP_ID = ""
|
||||||
|
|
||||||
|
@ -189,23 +181,26 @@ describe("Authorization middleware", () => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("view type", () => {
|
describe("with resource", () => {
|
||||||
const tableId = generateTableID()
|
let resourceId: string
|
||||||
const viewId = generateViewID(tableId)
|
const mockedGetResourcePerms = getResourcePerms as jest.MockedFunction<
|
||||||
|
typeof getResourcePerms
|
||||||
const mockedGetRequiredResourceRole =
|
>
|
||||||
roles.getRequiredResourceRole as jest.MockedFunction<
|
|
||||||
typeof roles.getRequiredResourceRole
|
|
||||||
>
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
config.setMiddlewareRequiredPermission(
|
config.setMiddlewareRequiredPermission(
|
||||||
PermissionType.VIEW,
|
PermissionType.VIEW,
|
||||||
PermissionLevel.READ
|
PermissionLevel.READ
|
||||||
)
|
)
|
||||||
config.setResourceId(viewId)
|
resourceId = generator.guid()
|
||||||
|
config.setResourceId(resourceId)
|
||||||
|
|
||||||
mockedGetRequiredResourceRole.mockResolvedValue(["PUBLIC"])
|
mockedGetResourcePerms.mockResolvedValue({
|
||||||
|
[PermissionLevel.READ]: {
|
||||||
|
role: "PUBLIC",
|
||||||
|
type: PermissionSource.BASE,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
config.setUser({
|
config.setUser({
|
||||||
_id: "user",
|
_id: "user",
|
||||||
|
@ -215,57 +210,14 @@ describe("Authorization middleware", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("will ignore view permissions if flag is off", async () => {
|
it("will fetch resource permissions when resource is set", async () => {
|
||||||
await config.executeMiddleware()
|
await config.executeMiddleware()
|
||||||
|
|
||||||
expect(config.throw).not.toBeCalled()
|
expect(config.throw).not.toBeCalled()
|
||||||
expect(config.next).toHaveBeenCalled()
|
expect(config.next).toHaveBeenCalled()
|
||||||
|
|
||||||
expect(mockedGetRequiredResourceRole).toBeCalledTimes(1)
|
expect(mockedGetResourcePerms).toBeCalledTimes(1)
|
||||||
expect(mockedGetRequiredResourceRole).toBeCalledWith(
|
expect(mockedGetResourcePerms).toBeCalledWith(resourceId)
|
||||||
PermissionLevel.READ,
|
|
||||||
expect.objectContaining({
|
|
||||||
resourceId: tableId,
|
|
||||||
subResourceId: undefined,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("will use view permissions if flag is on", async () => {
|
|
||||||
mocks.licenses.useViewPermissions()
|
|
||||||
await config.executeMiddleware()
|
|
||||||
|
|
||||||
expect(config.throw).not.toBeCalled()
|
|
||||||
expect(config.next).toHaveBeenCalled()
|
|
||||||
|
|
||||||
expect(mockedGetRequiredResourceRole).toBeCalledTimes(1)
|
|
||||||
expect(mockedGetRequiredResourceRole).toBeCalledWith(
|
|
||||||
PermissionLevel.READ,
|
|
||||||
expect.objectContaining({
|
|
||||||
resourceId: tableId,
|
|
||||||
subResourceId: viewId,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("throw an exception if the resource id is not provided", async () => {
|
|
||||||
config.setResourceId(undefined)
|
|
||||||
await config.executeMiddleware()
|
|
||||||
expect(config.throw).toHaveBeenNthCalledWith(
|
|
||||||
1,
|
|
||||||
400,
|
|
||||||
"Cannot obtain the view id"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("throw an exception if the resource id is not a valid view id", async () => {
|
|
||||||
config.setResourceId(tableId)
|
|
||||||
await config.executeMiddleware()
|
|
||||||
expect(config.throw).toHaveBeenNthCalledWith(
|
|
||||||
1,
|
|
||||||
400,
|
|
||||||
`"${tableId}" is not a valid view id`
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue