diff --git a/packages/backend-core/src/tenancy/tests/tenancy.spec.ts b/packages/backend-core/src/tenancy/tests/tenancy.spec.ts index 1d98f760c8..f6dda3037f 100644 --- a/packages/backend-core/src/tenancy/tests/tenancy.spec.ts +++ b/packages/backend-core/src/tenancy/tests/tenancy.spec.ts @@ -4,11 +4,14 @@ jest.mock("../../context", () => ({ isMultiTenant: jest.fn(() => true), })) -import { addTenantToUrl } from "../" -import { isMultiTenant } from "../../context" +import { addTenantToUrl, isUserInAppTenant, getTenantIDFromCtx } from "../" +import { isMultiTenant, getTenantIDFromAppID } from "../../context" const mockedIsMultiTenant = isMultiTenant as jest.MockedFunction< typeof isMultiTenant > +const mockedGetTenantIDFromAppID = getTenantIDFromAppID as jest.MockedFunction< + typeof getTenantIDFromAppID +> describe("addTenantToUrl", () => { it("should append tenantId parameter to the URL", () => { const url = "https://budibase.com" @@ -32,3 +35,38 @@ describe("addTenantToUrl", () => { expect(addTenantToUrl(url)).toEqual(expectedUrl) }) }) + +jest.mock("../../context", () => ({ + getTenantId: jest.fn(() => "budibase"), + getTenantIDFromAppID: jest.fn(() => "budibase"), +})) +describe("isUserInAppTenant", () => { + const mockUser = { tenantId: "budibase" } + + it("returns true if user tenant ID matches app tenant ID", () => { + const appId = "app-budibase" + const result = isUserInAppTenant(appId, mockUser) + expect(result).toBe(true) + }) + + it("uses default tenant ID if user is not provided", () => { + const appId = "app-budibase" + const result = isUserInAppTenant(appId) + expect(result).toBe(true) + }) + + it("uses default tenant ID if app tenant ID is not found", () => { + const appId = "not-budibase-app" + const result = isUserInAppTenant(appId, mockUser) + expect(result).toBe(true) + }) + + it("returns false if user tenant ID does not match app tenant ID", () => { + const appId = "app-budibase" + mockedGetTenantIDFromAppID.mockImplementation(() => "not-budibase") + const result = isUserInAppTenant(appId, mockUser) + expect(result).toBe(false) + }) +}) + +describe("getTenantIDFromCtx", () => {})