Fixing tenancy.spec.ts - mocking was a bit messed up, moving it all around to work as expected.
This commit is contained in:
parent
de613d2b28
commit
c4a4bdc9da
|
@ -43,6 +43,10 @@ export function baseGlobalDBName(tenantId: string | undefined | null) {
|
|||
}
|
||||
}
|
||||
|
||||
export function getPlatformURL() {
|
||||
return env.PLATFORM_URL
|
||||
}
|
||||
|
||||
export function isMultiTenant() {
|
||||
return !!env.MULTI_TENANCY
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ import {
|
|||
getTenantId,
|
||||
getTenantIDFromAppID,
|
||||
isMultiTenant,
|
||||
getPlatformURL,
|
||||
} from "../context"
|
||||
import env from "../environment"
|
||||
import {
|
||||
BBContext,
|
||||
TenantResolutionStrategy,
|
||||
|
@ -93,7 +93,7 @@ export const getTenantIDFromCtx = (
|
|||
// subdomain
|
||||
if (isAllowed(TenantResolutionStrategy.SUBDOMAIN)) {
|
||||
// e.g. budibase.app or local.com:10000
|
||||
const platformHost = new URL(env.PLATFORM_URL).host.split(":")[0]
|
||||
const platformHost = new URL(getPlatformURL()).host.split(":")[0]
|
||||
// e.g. tenant.budibase.app or tenant.local.com
|
||||
const requestHost = ctx.host
|
||||
// parse the tenant id from the difference
|
||||
|
|
|
@ -1,23 +1,22 @@
|
|||
import { TenantResolutionStrategy } from "@budibase/types"
|
||||
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
|
||||
import {
|
||||
isMultiTenant,
|
||||
getTenantIDFromAppID,
|
||||
DEFAULT_TENANT_ID,
|
||||
getTenantId,
|
||||
} from "../../context"
|
||||
import { any } from "joi"
|
||||
import { isMultiTenant, getTenantIDFromAppID } from "../../context"
|
||||
|
||||
jest.mock("../../context", () => ({
|
||||
getTenantId: jest.fn(() => "budibase"),
|
||||
isMultiTenant: jest.fn(() => true),
|
||||
getTenantIDFromAppID: jest.fn(),
|
||||
getPlatformURL: jest.fn(() => "https://app.com"),
|
||||
DEFAULT_TENANT_ID: "default",
|
||||
}))
|
||||
|
||||
const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction<
|
||||
typeof isMultiTenant
|
||||
>
|
||||
const mockedGetTenantIDFromAppID = getTenantIDFromAppID as jest.MockedFunction<
|
||||
typeof getTenantIDFromAppID
|
||||
>
|
||||
// mock the `getTenantId` and `isMultiTenant` functions
|
||||
jest.mock("../../context", () => ({
|
||||
getTenantId: jest.fn(() => "budibase"),
|
||||
isMultiTenant: jest.fn(() => true),
|
||||
}))
|
||||
|
||||
describe("addTenantToUrl", () => {
|
||||
it("should append tenantId parameter to the URL", () => {
|
||||
const url = "https://budibase.com"
|
||||
|
@ -40,11 +39,8 @@ describe("addTenantToUrl", () => {
|
|||
})
|
||||
})
|
||||
|
||||
jest.mock("../../context", () => ({
|
||||
getTenantId: jest.fn(() => "budibase"),
|
||||
getTenantIDFromAppID: jest.fn(() => "budibase"),
|
||||
}))
|
||||
describe("isUserInAppTenant", () => {
|
||||
mockedGetTenantIDFromAppID.mockImplementation(() => "budibase")
|
||||
const mockUser = { tenantId: "budibase" }
|
||||
|
||||
it("returns true if user tenant ID matches app tenant ID", () => {
|
||||
|
@ -74,16 +70,13 @@ describe("isUserInAppTenant", () => {
|
|||
})
|
||||
|
||||
let mockOpts: any = {}
|
||||
jest.mock("../../context", () => ({
|
||||
isMultiTenant: jest.fn(() => true),
|
||||
}))
|
||||
|
||||
function createCtx(opts: {
|
||||
originalUrl?: string
|
||||
headers?: string[]
|
||||
headers?: Record<string, string>
|
||||
qsTenantId?: string
|
||||
userTenantId?: string
|
||||
host?: string
|
||||
path?: string
|
||||
}) {
|
||||
const createdCtx: any = {
|
||||
originalUrl: opts.originalUrl || "budibase.com",
|
||||
|
@ -103,41 +96,53 @@ function createCtx(opts: {
|
|||
if (opts.host) {
|
||||
createdCtx.host = opts.host
|
||||
}
|
||||
if (opts.path) {
|
||||
createdCtx.matched = [
|
||||
{
|
||||
paramNames: [{ name: "tenantId" }],
|
||||
params: () => ({ tenantId: opts.path }),
|
||||
captures: jest.fn(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return createdCtx as any
|
||||
}
|
||||
jest.mock("../../../src/constants/misc", () => ({
|
||||
DEFAULT_TENANT_ID: "default",
|
||||
}))
|
||||
|
||||
describe("getTenantIDFromCtx", () => {
|
||||
describe("when tenant can be found", () => {
|
||||
it("returns the tenant ID from the user object", () => {
|
||||
mockedIsMultiTenant.mockImplementation(() => true)
|
||||
const ctx = createCtx({ userTenantId: "budibase" })
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||
})
|
||||
|
||||
it("returns the tenant ID from the header", () => {
|
||||
const ctx = createCtx({ headers: ["TENANT_ID=budibase"] })
|
||||
mockedIsMultiTenant.mockImplementation(() => true)
|
||||
const ctx = createCtx({ headers: { "x-budibase-tenant-id": "budibase" } })
|
||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.HEADER] }
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||
})
|
||||
|
||||
it("returns the tenant ID from the query param", () => {
|
||||
mockedIsMultiTenant.mockImplementation(() => true)
|
||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.QUERY] }
|
||||
const ctx = createCtx({ qsTenantId: "budibase" })
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||
})
|
||||
|
||||
it("returns the tenant ID from the subdomain", () => {
|
||||
mockedIsMultiTenant.mockImplementation(() => true)
|
||||
const ctx = createCtx({ host: "bb.app.com" })
|
||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.SUBDOMAIN] }
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb")
|
||||
})
|
||||
|
||||
it("returns the tenant ID from the path", () => {
|
||||
const ctx = createCtx({ host: "bb" })
|
||||
mockedIsMultiTenant.mockImplementation(() => true)
|
||||
const ctx = createCtx({ path: "bb" })
|
||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.PATH] }
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("123")
|
||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb")
|
||||
})
|
||||
})
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue