From be396dc3c0cd0160a06fdfc7bdae39961f003024 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 14 Sep 2022 14:22:53 +0100 Subject: [PATCH] fixing tests --- packages/backend-core/src/tenancy/tenancy.ts | 9 ++++++--- packages/types/src/documents/platform/users.ts | 9 +++++++++ .../worker/src/api/routes/global/tests/users.spec.ts | 2 +- packages/worker/src/sdk/users/users.ts | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/tenancy/tenancy.ts b/packages/backend-core/src/tenancy/tenancy.ts index b28d098a0d..1c71935eb0 100644 --- a/packages/backend-core/src/tenancy/tenancy.ts +++ b/packages/backend-core/src/tenancy/tenancy.ts @@ -9,6 +9,7 @@ import { getTenantIDFromAppID, } from "../context" import env from "../environment" +import { PlatformUser, PlatformUserByEmail } from "@budibase/types" const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name @@ -108,13 +109,15 @@ export const lookupTenantId = async (userId: string) => { } // lookup, could be email or userId, either will return a doc -export const getTenantUser = async (identifier: string) => { +export const getTenantUser = async ( + identifier: string +): Promise => { // use the view here and allow to find anyone regardless of casing // Use lowercase to ensure email login is case insensitive - const response = await queryPlatformView(ViewName.PLATFORM_USERS_LOWERCASE, { + const response = queryPlatformView(ViewName.PLATFORM_USERS_LOWERCASE, { keys: [identifier.toLowerCase()], include_docs: true, - }) + }) as Promise return response } diff --git a/packages/types/src/documents/platform/users.ts b/packages/types/src/documents/platform/users.ts index 1b65ea42f2..46cc44b31d 100644 --- a/packages/types/src/documents/platform/users.ts +++ b/packages/types/src/documents/platform/users.ts @@ -7,3 +7,12 @@ export interface PlatformUserByEmail extends Document { tenantId: string userId: string } + +/** + * doc id is userId + */ +export interface PlatformUserById extends Document { + tenantId: string +} + +export type PlatformUser = PlatformUserByEmail | PlatformUserById diff --git a/packages/worker/src/api/routes/global/tests/users.spec.ts b/packages/worker/src/api/routes/global/tests/users.spec.ts index 170320dc7e..fd9ef7ff9f 100644 --- a/packages/worker/src/api/routes/global/tests/users.spec.ts +++ b/packages/worker/src/api/routes/global/tests/users.spec.ts @@ -248,7 +248,7 @@ describe("/api/global/users", () => { await api.users.saveUser(user) user.email = user.email.toUpperCase() - await api.users.saveUser(user) + await api.users.saveUser(user, 400) expect(events.user.created).toBeCalledTimes(1) }) diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 33d78f350c..83b0510333 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -164,7 +164,7 @@ const buildUser = async ( const validateUniqueUser = async (email: string, tenantId: string) => { // check budibase users in other tenants if (env.MULTI_TENANCY) { - const tenantUser = (await tenancy.getTenantUser(email)) as User | undefined + const tenantUser = await tenancy.getTenantUser(email) if (tenantUser != null && tenantUser.tenantId !== tenantId) { throw `Unavailable` }