From 6bd950717cb7d7cf38231a00b67072ef6bd348ff Mon Sep 17 00:00:00 2001 From: Mateus Badan de Pieri Date: Wed, 5 Apr 2023 14:14:55 +0100 Subject: [PATCH] mocked correctly --- .../src/tenancy/tests/tenancy.spec.ts | 153 +++++++++--------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/packages/backend-core/src/tenancy/tests/tenancy.spec.ts b/packages/backend-core/src/tenancy/tests/tenancy.spec.ts index 3f249d5164..9608580522 100644 --- a/packages/backend-core/src/tenancy/tests/tenancy.spec.ts +++ b/packages/backend-core/src/tenancy/tests/tenancy.spec.ts @@ -4,8 +4,11 @@ jest.mock("../../context", () => ({ isMultiTenant: jest.fn(() => true), })) +import { TenantResolutionStrategy } from "@budibase/types" import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../" import { isMultiTenant, getTenantIDFromAppID } from "../../context" +import { any } from "joi" +import { DEFAULT_TENANT_ID } from "../../../src/constants/misc" const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction< typeof isMultiTenant > @@ -26,8 +29,6 @@ describe("addTenantToUrl", () => { }) it("should not append tenantId parameter to the URL if isMultiTenant is false", () => { - // mock the `isMultiTenant` function to return false - mockedIsMultiTenant.mockImplementation(() => false) const url = "https://budibase.com" @@ -69,97 +70,95 @@ describe("isUserInAppTenant", () => { }) }) -const mockCtx = { - 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"], -} +let mockOpts: any = {} // mock the `getTenantId` and `isMultiTenant` functions jest.mock("../../context", () => ({ 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("when isMultiTenant() returns true", () => { - beforeEach(() => { - jest.spyOn(global, "isMultiTenant").mockReturnValue(true) + describe("when tenant can be found", () => { + it("returns the tenant ID from the user object", () => { + const ctx = createCtx({ userTenantId: "budibase" }) + expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase") }) - afterEach(() => { - jest.restoreAllMocks() + it("returns the tenant ID from the header", () => { + 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 user object", () => { - expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("123") - }) - - 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") - }) + it("returns the tenant ID from the query param", () => { + const ctx = createCtx({ qsTenantId: "budibase" }) + expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("budibase") }) - 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 the tenant ID from the subdomain", () => { + const ctx = createCtx({ host: "bb" }) + mockOpts = { includeStrategies: [TenantResolutionStrategy.SUBDOMAIN] } + expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("bb") + }) - it("returns null if allowNoTenant is true", () => { - mockCtx.user = null - mockCtx.request.headers = {} - mockCtx.request.query = {} - mockCtx.host = "budibase.app" - mockOpts.allowNoTenant = true - expect(getTenantIDFromCtx(mockCtx, mockOpts)).toBeNull() - }) + it("returns the tenant ID from the path", () => { + const ctx = createCtx({ host: "bb" }) + mockOpts = { includeStrategies: [TenantResolutionStrategy.PATH] } + expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("123") + }) + }) + + 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", () => { mockedIsMultiTenant.mockImplementation(() => false) - expect(getTenantIDFromCtx(mockCtx, mockOpts)).toEqual("default") + const ctx = createCtx({}) + expect(getTenantIDFromCtx(ctx, mockOpts)).toEqual("default") }) })