fixing tests

This commit is contained in:
Martin McKeaveney 2022-09-14 14:22:53 +01:00
parent c4f424cb80
commit be396dc3c0
4 changed files with 17 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import {
getTenantIDFromAppID, getTenantIDFromAppID,
} from "../context" } from "../context"
import env from "../environment" import env from "../environment"
import { PlatformUser, PlatformUserByEmail } from "@budibase/types"
const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants
const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name 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 // lookup, could be email or userId, either will return a doc
export const getTenantUser = async (identifier: string) => { export const getTenantUser = async (
identifier: string
): Promise<PlatformUser | null> => {
// use the view here and allow to find anyone regardless of casing // use the view here and allow to find anyone regardless of casing
// Use lowercase to ensure email login is case insensitive // 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()], keys: [identifier.toLowerCase()],
include_docs: true, include_docs: true,
}) }) as Promise<PlatformUser>
return response return response
} }

View File

@ -7,3 +7,12 @@ export interface PlatformUserByEmail extends Document {
tenantId: string tenantId: string
userId: string userId: string
} }
/**
* doc id is userId
*/
export interface PlatformUserById extends Document {
tenantId: string
}
export type PlatformUser = PlatformUserByEmail | PlatformUserById

View File

@ -248,7 +248,7 @@ describe("/api/global/users", () => {
await api.users.saveUser(user) await api.users.saveUser(user)
user.email = user.email.toUpperCase() user.email = user.email.toUpperCase()
await api.users.saveUser(user) await api.users.saveUser(user, 400)
expect(events.user.created).toBeCalledTimes(1) expect(events.user.created).toBeCalledTimes(1)
}) })

View File

@ -164,7 +164,7 @@ const buildUser = async (
const validateUniqueUser = async (email: string, tenantId: string) => { const validateUniqueUser = async (email: string, tenantId: string) => {
// check budibase users in other tenants // check budibase users in other tenants
if (env.MULTI_TENANCY) { 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) { if (tenantUser != null && tenantUser.tenantId !== tenantId) {
throw `Unavailable` throw `Unavailable`
} }