mocked correctly
This commit is contained in:
parent
b6fd3b7cf7
commit
6bd950717c
|
@ -4,8 +4,11 @@ jest.mock("../../context", () => ({
|
||||||
isMultiTenant: jest.fn(() => true),
|
isMultiTenant: jest.fn(() => true),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
import { TenantResolutionStrategy } from "@budibase/types"
|
||||||
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
|
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
|
||||||
import { isMultiTenant, getTenantIDFromAppID } from "../../context"
|
import { isMultiTenant, getTenantIDFromAppID } from "../../context"
|
||||||
|
import { any } from "joi"
|
||||||
|
import { DEFAULT_TENANT_ID } from "../../../src/constants/misc"
|
||||||
const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction<
|
const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction<
|
||||||
typeof isMultiTenant
|
typeof isMultiTenant
|
||||||
>
|
>
|
||||||
|
@ -26,8 +29,6 @@ describe("addTenantToUrl", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should not append tenantId parameter to the URL if isMultiTenant is false", () => {
|
it("should not append tenantId parameter to the URL if isMultiTenant is false", () => {
|
||||||
// mock the `isMultiTenant` function to return false
|
|
||||||
|
|
||||||
mockedIsMultiTenant.mockImplementation(() => false)
|
mockedIsMultiTenant.mockImplementation(() => false)
|
||||||
|
|
||||||
const url = "https://budibase.com"
|
const url = "https://budibase.com"
|
||||||
|
@ -69,97 +70,95 @@ describe("isUserInAppTenant", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const mockCtx = {
|
let mockOpts: any = {}
|
||||||
user: { tenantId: "123" },
|
|
||||||
request: {
|
|
||||||
headers: { "X-Tenant-ID": "456" },
|
|
||||||
query: { tenantId: "789" },
|
|
||||||
},
|
|
||||||
host: "tenant.budibase.app",
|
|
||||||
originalUrl: "/tenant/123",
|
|
||||||
matched: [
|
|
||||||
{
|
|
||||||
paramNames: [{ name: "tenantId" }],
|
|
||||||
params: (url: any, captures: any, ctx: any) => ({ tenantId: "456" }),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
const mockOpts = {
|
|
||||||
allowNoTenant: false,
|
|
||||||
includeStrategies: ["USER", "HEADER", "QUERY", "SUBDOMAIN", "PATH"],
|
|
||||||
excludeStrategies: ["QUERY"],
|
|
||||||
}
|
|
||||||
// mock the `getTenantId` and `isMultiTenant` functions
|
// mock the `getTenantId` and `isMultiTenant` functions
|
||||||
jest.mock("../../context", () => ({
|
jest.mock("../../context", () => ({
|
||||||
isMultiTenant: jest.fn(() => true),
|
isMultiTenant: jest.fn(() => true),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
jest.mock("../../../src/constants/misc", () => ({
|
||||||
|
DEFAULT_TENANT_ID: "default",
|
||||||
|
}))
|
||||||
|
|
||||||
|
function createCtx(opts: {
|
||||||
|
originalUrl?: string
|
||||||
|
headers?: string[]
|
||||||
|
qsTenantId?: string
|
||||||
|
userTenantId?: string
|
||||||
|
}) {
|
||||||
|
const createdCtx: any = {
|
||||||
|
originalUrl: opts.originalUrl || "budibase.com",
|
||||||
|
matched: [{ name: "name" }],
|
||||||
|
throw: jest.fn(),
|
||||||
|
request: { headers: {} },
|
||||||
|
}
|
||||||
|
if (opts.headers) {
|
||||||
|
createdCtx.request.headers = opts.headers
|
||||||
|
}
|
||||||
|
if (opts.qsTenantId) {
|
||||||
|
createdCtx.request.query = { tenantId: opts.qsTenantId }
|
||||||
|
}
|
||||||
|
if (opts.userTenantId) {
|
||||||
|
createdCtx.user = { tenantId: opts.userTenantId }
|
||||||
|
}
|
||||||
|
|
||||||
|
return createdCtx as any
|
||||||
|
}
|
||||||
describe("getTenantIDFromCtx", () => {
|
describe("getTenantIDFromCtx", () => {
|
||||||
describe("when isMultiTenant() returns true", () => {
|
describe("when tenant can be found", () => {
|
||||||
beforeEach(() => {
|
it("returns the tenant ID from the user object", () => {
|
||||||
jest.spyOn(global, "isMultiTenant").mockReturnValue(true)
|
const ctx = createCtx({ userTenantId: "budibase" })
|
||||||
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
it("returns the tenant ID from the header", () => {
|
||||||
jest.restoreAllMocks()
|
const ctx = createCtx({ headers: ["TENANT_ID:budibase"] })
|
||||||
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.HEADERS] }
|
||||||
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("when tenant can be found", () => {
|
it("returns the tenant ID from the query param", () => {
|
||||||
it("returns the tenant ID from the user object", () => {
|
const ctx = createCtx({ qsTenantId: "budibase" })
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("123")
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the tenant ID from the header", () => {
|
|
||||||
mockCtx.user = null
|
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("456")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the tenant ID from the query param", () => {
|
|
||||||
mockCtx.user = null
|
|
||||||
mockCtx.request.headers = {}
|
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("789")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the tenant ID from the subdomain", () => {
|
|
||||||
mockCtx.user = null
|
|
||||||
mockCtx.request.headers = {}
|
|
||||||
mockCtx.request.query = {}
|
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("tenant")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the tenant ID from the path", () => {
|
|
||||||
mockCtx.user = null
|
|
||||||
mockCtx.request.headers = {}
|
|
||||||
mockCtx.request.query = {}
|
|
||||||
mockCtx.host = "budibase.app"
|
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("123")
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("when tenant cannot be found", () => {
|
it("returns the tenant ID from the subdomain", () => {
|
||||||
it("throws a 403 error if allowNoTenant is false", () => {
|
const ctx = createCtx({ host: "bb" })
|
||||||
mockCtx.user = null
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.SUBDOMAIN] }
|
||||||
mockCtx.request.headers = {}
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb")
|
||||||
mockCtx.request.query = {}
|
})
|
||||||
mockCtx.host = "budibase.app"
|
|
||||||
mockOpts.allowNoTenant = false
|
|
||||||
expect(() => getTenantIDFromCtx(mockCtx, mockOpts)).toThrowError(
|
|
||||||
"Tenant id not set"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns null if allowNoTenant is true", () => {
|
it("returns the tenant ID from the path", () => {
|
||||||
mockCtx.user = null
|
const ctx = createCtx({ host: "bb" })
|
||||||
mockCtx.request.headers = {}
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.PATH] }
|
||||||
mockCtx.request.query = {}
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("123")
|
||||||
mockCtx.host = "budibase.app"
|
})
|
||||||
mockOpts.allowNoTenant = true
|
})
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toBeNull()
|
|
||||||
})
|
describe("when tenant cannot be found", () => {
|
||||||
|
it("throws a 403 error if allowNoTenant is false", () => {
|
||||||
|
mockCtx.user = null
|
||||||
|
mockCtx.request.headers = {}
|
||||||
|
mockCtx.request.query = {}
|
||||||
|
mockCtx.host = "budibase.app"
|
||||||
|
mockOpts.allowNoTenant = false
|
||||||
|
expect(() => getTenantIDFromCtx(mockCtx, mockOpts)).toThrowError(
|
||||||
|
"Tenant id not set"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns null if allowNoTenant is true", () => {
|
||||||
|
mockedIsMultiTenant.mockImplementation(() => false)
|
||||||
|
const ctx = createCtx({})
|
||||||
|
mockOpts = { allowNoTenant: true }
|
||||||
|
console.log(mockOpts)
|
||||||
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toBeNull()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns the default tenant ID when isMultiTenant() returns false", () => {
|
it("returns the default tenant ID when isMultiTenant() returns false", () => {
|
||||||
mockedIsMultiTenant.mockImplementation(() => false)
|
mockedIsMultiTenant.mockImplementation(() => false)
|
||||||
expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("default")
|
const ctx = createCtx({})
|
||||||
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("default")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue