tweaks on some tests

This commit is contained in:
Mateus Badan de Pieri 2023-04-05 16:13:39 +01:00
parent 6bd950717c
commit b0df710a2a
1 changed files with 34 additions and 19 deletions

View File

@ -6,9 +6,12 @@ jest.mock("../../context", () => ({
import { TenantResolutionStrategy } from "@budibase/types" import { TenantResolutionStrategy } from "@budibase/types"
import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../" import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../"
import { isMultiTenant, getTenantIDFromAppID } from "../../context" import {
isMultiTenant,
getTenantIDFromAppID,
DEFAULT_TENANT_ID,
} from "../../context"
import { any } from "joi" 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
> >
@ -71,20 +74,16 @@ describe("isUserInAppTenant", () => {
}) })
let mockOpts: any = {} let mockOpts: any = {}
// 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: { function createCtx(opts: {
originalUrl?: string originalUrl?: string
headers?: string[] headers?: string[]
qsTenantId?: string qsTenantId?: string
userTenantId?: string userTenantId?: string
host?: string
}) { }) {
const createdCtx: any = { const createdCtx: any = {
originalUrl: opts.originalUrl || "budibase.com", originalUrl: opts.originalUrl || "budibase.com",
@ -101,9 +100,15 @@ function createCtx(opts: {
if (opts.userTenantId) { if (opts.userTenantId) {
createdCtx.user = { tenantId: opts.userTenantId } createdCtx.user = { tenantId: opts.userTenantId }
} }
if (opts.host) {
createdCtx.host = opts.host
}
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", () => {
@ -112,18 +117,19 @@ describe("getTenantIDFromCtx", () => {
}) })
it("returns the tenant ID from the header", () => { it("returns the tenant ID from the header", () => {
const ctx = createCtx({ headers: ["TENANT_ID:budibase"] }) const ctx = createCtx({ headers: ["TENANT_ID=budibase"] })
mockOpts = { includeStrategies: [TenantResolutionStrategy.HEADERS] } 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", () => {
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", () => {
const ctx = createCtx({ host: "bb" }) 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")
}) })
@ -137,21 +143,30 @@ describe("getTenantIDFromCtx", () => {
describe("when tenant cannot be found", () => { describe("when tenant cannot be found", () => {
it("throws a 403 error if allowNoTenant is false", () => { it("throws a 403 error if allowNoTenant is false", () => {
mockCtx.user = null const ctx = createCtx({})
mockCtx.request.headers = {} mockOpts = {
mockCtx.request.query = {} allowNoTenant: false,
mockCtx.host = "budibase.app" excludeStrategies: [
mockOpts.allowNoTenant = false TenantResolutionStrategy.QUERY,
expect(() => getTenantIDFromCtx(mockCtx, mockOpts)).toThrowError( TenantResolutionStrategy.SUBDOMAIN,
TenantResolutionStrategy.PATH,
],
}
expect(() => getTenantIDFromCtx(ctx, mockOpts)).rejects.toThrowError(
"Tenant id not set" "Tenant id not set"
) )
}) })
it("returns null if allowNoTenant is true", () => { it("returns null if allowNoTenant is true", () => {
mockedIsMultiTenant.mockImplementation(() => false)
const ctx = createCtx({}) const ctx = createCtx({})
mockOpts = { allowNoTenant: true } mockOpts = {
console.log(mockOpts) allowNoTenant: true,
excludeStrategies: [
TenantResolutionStrategy.QUERY,
TenantResolutionStrategy.SUBDOMAIN,
TenantResolutionStrategy.PATH,
],
}
expect(getTenantIDFromCtx(ctx, mockOpts)).toBeNull() expect(getTenantIDFromCtx(ctx, mockOpts)).toBeNull()
}) })
}) })