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() {
|
export function isMultiTenant() {
|
||||||
return !!env.MULTI_TENANCY
|
return !!env.MULTI_TENANCY
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,8 @@ import {
|
||||||
getTenantId,
|
getTenantId,
|
||||||
getTenantIDFromAppID,
|
getTenantIDFromAppID,
|
||||||
isMultiTenant,
|
isMultiTenant,
|
||||||
|
getPlatformURL,
|
||||||
} from "../context"
|
} from "../context"
|
||||||
import env from "../environment"
|
|
||||||
import {
|
import {
|
||||||
BBContext,
|
BBContext,
|
||||||
TenantResolutionStrategy,
|
TenantResolutionStrategy,
|
||||||
|
@ -93,7 +93,7 @@ export const getTenantIDFromCtx = (
|
||||||
// subdomain
|
// subdomain
|
||||||
if (isAllowed(TenantResolutionStrategy.SUBDOMAIN)) {
|
if (isAllowed(TenantResolutionStrategy.SUBDOMAIN)) {
|
||||||
// e.g. budibase.app or local.com:10000
|
// 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
|
// e.g. tenant.budibase.app or tenant.local.com
|
||||||
const requestHost = ctx.host
|
const requestHost = ctx.host
|
||||||
// parse the tenant id from the difference
|
// parse the tenant id from the difference
|
||||||
|
|
|
@ -1,23 +1,22 @@
|
||||||
import { TenantResolutionStrategy } from "@budibase/types"
|
import { TenantResolutionStrategy } from "@budibase/types"
|
||||||
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
|
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
|
||||||
import {
|
import { isMultiTenant, getTenantIDFromAppID } from "../../context"
|
||||||
isMultiTenant,
|
|
||||||
getTenantIDFromAppID,
|
jest.mock("../../context", () => ({
|
||||||
DEFAULT_TENANT_ID,
|
getTenantId: jest.fn(() => "budibase"),
|
||||||
getTenantId,
|
isMultiTenant: jest.fn(() => true),
|
||||||
} from "../../context"
|
getTenantIDFromAppID: jest.fn(),
|
||||||
import { any } from "joi"
|
getPlatformURL: jest.fn(() => "https://app.com"),
|
||||||
|
DEFAULT_TENANT_ID: "default",
|
||||||
|
}))
|
||||||
|
|
||||||
const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction<
|
const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction<
|
||||||
typeof isMultiTenant
|
typeof isMultiTenant
|
||||||
>
|
>
|
||||||
const mockedGetTenantIDFromAppID = getTenantIDFromAppID as jest.MockedFunction<
|
const mockedGetTenantIDFromAppID = getTenantIDFromAppID as jest.MockedFunction<
|
||||||
typeof getTenantIDFromAppID
|
typeof getTenantIDFromAppID
|
||||||
>
|
>
|
||||||
// mock the `getTenantId` and `isMultiTenant` functions
|
|
||||||
jest.mock("../../context", () => ({
|
|
||||||
getTenantId: jest.fn(() => "budibase"),
|
|
||||||
isMultiTenant: jest.fn(() => true),
|
|
||||||
}))
|
|
||||||
describe("addTenantToUrl", () => {
|
describe("addTenantToUrl", () => {
|
||||||
it("should append tenantId parameter to the URL", () => {
|
it("should append tenantId parameter to the URL", () => {
|
||||||
const url = "https://budibase.com"
|
const url = "https://budibase.com"
|
||||||
|
@ -40,11 +39,8 @@ describe("addTenantToUrl", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
jest.mock("../../context", () => ({
|
|
||||||
getTenantId: jest.fn(() => "budibase"),
|
|
||||||
getTenantIDFromAppID: jest.fn(() => "budibase"),
|
|
||||||
}))
|
|
||||||
describe("isUserInAppTenant", () => {
|
describe("isUserInAppTenant", () => {
|
||||||
|
mockedGetTenantIDFromAppID.mockImplementation(() => "budibase")
|
||||||
const mockUser = { tenantId: "budibase" }
|
const mockUser = { tenantId: "budibase" }
|
||||||
|
|
||||||
it("returns true if user tenant ID matches app tenant ID", () => {
|
it("returns true if user tenant ID matches app tenant ID", () => {
|
||||||
|
@ -74,16 +70,13 @@ describe("isUserInAppTenant", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
let mockOpts: any = {}
|
let mockOpts: any = {}
|
||||||
jest.mock("../../context", () => ({
|
|
||||||
isMultiTenant: jest.fn(() => true),
|
|
||||||
}))
|
|
||||||
|
|
||||||
function createCtx(opts: {
|
function createCtx(opts: {
|
||||||
originalUrl?: string
|
originalUrl?: string
|
||||||
headers?: string[]
|
headers?: Record<string, string>
|
||||||
qsTenantId?: string
|
qsTenantId?: string
|
||||||
userTenantId?: string
|
userTenantId?: string
|
||||||
host?: string
|
host?: string
|
||||||
|
path?: string
|
||||||
}) {
|
}) {
|
||||||
const createdCtx: any = {
|
const createdCtx: any = {
|
||||||
originalUrl: opts.originalUrl || "budibase.com",
|
originalUrl: opts.originalUrl || "budibase.com",
|
||||||
|
@ -103,41 +96,53 @@ function createCtx(opts: {
|
||||||
if (opts.host) {
|
if (opts.host) {
|
||||||
createdCtx.host = 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
|
return createdCtx as any
|
||||||
}
|
}
|
||||||
jest.mock("../../../src/constants/misc", () => ({
|
|
||||||
DEFAULT_TENANT_ID: "default",
|
|
||||||
}))
|
|
||||||
describe("getTenantIDFromCtx", () => {
|
describe("getTenantIDFromCtx", () => {
|
||||||
describe("when tenant can be found", () => {
|
describe("when tenant can be found", () => {
|
||||||
it("returns the tenant ID from the user object", () => {
|
it("returns the tenant ID from the user object", () => {
|
||||||
|
mockedIsMultiTenant.mockImplementation(() => true)
|
||||||
const ctx = createCtx({ userTenantId: "budibase" })
|
const ctx = createCtx({ userTenantId: "budibase" })
|
||||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns the tenant ID from the header", () => {
|
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] }
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.HEADER] }
|
||||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns the tenant ID from the query param", () => {
|
it("returns the tenant ID from the query param", () => {
|
||||||
|
mockedIsMultiTenant.mockImplementation(() => true)
|
||||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.QUERY] }
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.QUERY] }
|
||||||
const ctx = createCtx({ qsTenantId: "budibase" })
|
const ctx = createCtx({ qsTenantId: "budibase" })
|
||||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns the tenant ID from the subdomain", () => {
|
it("returns the tenant ID from the subdomain", () => {
|
||||||
|
mockedIsMultiTenant.mockImplementation(() => true)
|
||||||
const ctx = createCtx({ host: "bb.app.com" })
|
const ctx = createCtx({ host: "bb.app.com" })
|
||||||
mockOpts = { includeStrategies: [TenantResolutionStrategy.SUBDOMAIN] }
|
mockOpts = { includeStrategies: [TenantResolutionStrategy.SUBDOMAIN] }
|
||||||
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb")
|
expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns the tenant ID from the path", () => {
|
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] }
|
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