Migrate tests to ts

This commit is contained in:
Adria Navarro 2023-08-24 11:05:31 +02:00
parent 9a7a3b9c72
commit 84a6f239a9
1 changed files with 52 additions and 39 deletions

View File

@ -1,34 +1,40 @@
jest.mock("../../environment", () => ({ jest.mock("../../environment", () => ({
prod: false, prod: false,
isTest: () => true, isTest: () => true,
// @ts-ignore
isProd: () => this.prod, isProd: () => this.prod,
_set: function(key, value) { _set: function (key: string, value: string) {
this.prod = value === "production" this.prod = value === "production"
} },
}) }))
) import authorizedMiddleware from "../authorized"
const authorizedMiddleware = require("../authorized").default import env from "../../environment"
const env = require("../../environment") import { PermissionType, PermissionLevel } from "@budibase/types"
const { PermissionType, PermissionLevel } = require("@budibase/types")
const APP_ID = "" const APP_ID = ""
class TestConfiguration { class TestConfiguration {
constructor(role) { middleware: (ctx: any, next: any) => Promise<void>
this.middleware = authorizedMiddleware(role) next: () => void
throw: () => void
headers: Record<string, any>
ctx: any
constructor() {
this.middleware = authorizedMiddleware(PermissionType.APP)
this.next = jest.fn() this.next = jest.fn()
this.throw = jest.fn() this.throw = jest.fn()
this.headers = {} this.headers = {}
this.ctx = { this.ctx = {
headers: {}, headers: {},
request: { request: {
url: "" url: "",
}, },
appId: APP_ID, appId: APP_ID,
auth: {}, auth: {},
next: this.next, next: this.next,
throw: this.throw, throw: this.throw,
get: (name) => this.headers[name], get: (name: string) => this.headers[name],
} }
} }
@ -36,31 +42,32 @@ class TestConfiguration {
return this.middleware(this.ctx, this.next) return this.middleware(this.ctx, this.next)
} }
setUser(user) { setUser(user: any) {
this.ctx.user = user this.ctx.user = user
} }
setMiddlewareRequiredPermission(...perms) { setMiddlewareRequiredPermission(...perms: any[]) {
// @ts-ignore
this.middleware = authorizedMiddleware(...perms) this.middleware = authorizedMiddleware(...perms)
} }
setResourceId(id) { setResourceId(id: string) {
this.ctx.resourceId = id this.ctx.resourceId = id
} }
setAuthenticated(isAuthed) { setAuthenticated(isAuthed: boolean) {
this.ctx.isAuthenticated = isAuthed this.ctx.isAuthenticated = isAuthed
} }
setRequestUrl(url) { setRequestUrl(url: string) {
this.ctx.request.url = url this.ctx.request.url = url
} }
setEnvironment(isProd) { setEnvironment(isProd: boolean) {
env._set("NODE_ENV", isProd ? "production" : "jest") env._set("NODE_ENV", isProd ? "production" : "jest")
} }
setRequestHeaders(headers) { setRequestHeaders(headers: Record<string, any>) {
this.ctx.headers = headers this.ctx.headers = headers
} }
@ -69,10 +76,9 @@ class TestConfiguration {
} }
} }
describe("Authorization middleware", () => { describe("Authorization middleware", () => {
const next = jest.fn() const next = jest.fn()
let config let config: TestConfiguration
afterEach(() => { afterEach(() => {
config.afterEach() config.afterEach()
@ -83,8 +89,6 @@ describe("Authorization middleware", () => {
}) })
describe("non-webhook call", () => { describe("non-webhook call", () => {
let config
beforeEach(() => { beforeEach(() => {
config = new TestConfiguration() config = new TestConfiguration()
config.setEnvironment(true) config.setEnvironment(true)
@ -102,7 +106,7 @@ describe("Authorization middleware", () => {
_id: "user", _id: "user",
role: { role: {
_id: "ADMIN", _id: "ADMIN",
} },
}) })
await config.executeMiddleware() await config.executeMiddleware()
@ -115,8 +119,8 @@ describe("Authorization middleware", () => {
config.setMiddlewareRequiredPermission(PermissionType.BUILDER) config.setMiddlewareRequiredPermission(PermissionType.BUILDER)
config.setUser({ config.setUser({
role: { role: {
_id: "" _id: "",
} },
}) })
await config.executeMiddleware() await config.executeMiddleware()
@ -127,8 +131,8 @@ describe("Authorization middleware", () => {
config.setResourceId(PermissionType.QUERY) config.setResourceId(PermissionType.QUERY)
config.setUser({ config.setUser({
role: { role: {
_id: "" _id: "",
} },
}) })
config.setMiddlewareRequiredPermission(PermissionType.QUERY) config.setMiddlewareRequiredPermission(PermissionType.QUERY)
@ -139,25 +143,34 @@ describe("Authorization middleware", () => {
it("throws if the user session is not authenticated", async () => { it("throws if the user session is not authenticated", async () => {
config.setUser({ config.setUser({
role: { role: {
_id: "" _id: "",
}, },
}) })
config.setAuthenticated(false) config.setAuthenticated(false)
await config.executeMiddleware() await config.executeMiddleware()
expect(config.throw).toHaveBeenCalledWith(403, "Session not authenticated") expect(config.throw).toHaveBeenCalledWith(
403,
"Session not authenticated"
)
}) })
it("throws if the user does not have base permissions to perform the operation", async () => { it("throws if the user does not have base permissions to perform the operation", async () => {
config.setUser({ config.setUser({
role: { role: {
_id: "" _id: "",
}, },
}) })
config.setMiddlewareRequiredPermission(PermissionType.ADMIN, PermissionLevel.BASIC) config.setMiddlewareRequiredPermission(
PermissionType.APP,
PermissionLevel.READ
)
await config.executeMiddleware() await config.executeMiddleware()
expect(config.throw).toHaveBeenCalledWith(403, "User does not have permission") expect(config.throw).toHaveBeenCalledWith(
403,
"User does not have permission"
)
}) })
}) })
}) })