First sync test, checking that a user is created correctly.
This commit is contained in:
parent
0771ec55fb
commit
58d0a82108
|
@ -83,8 +83,11 @@ async function syncUsersToAllApps(userIds: string[]) {
|
|||
const users = (await getGlobalUsers(userIds)) as User[]
|
||||
const finalUsers: (User | DeletedUser)[] = []
|
||||
for (let userId of userIds) {
|
||||
if (!users.find(user => user._id === userId)) {
|
||||
const user = users.find(user => user._id === userId)
|
||||
if (!user) {
|
||||
finalUsers.push({ _id: userId, deleted: true })
|
||||
} else {
|
||||
finalUsers.push(user)
|
||||
}
|
||||
}
|
||||
const devAppIds = await dbCore.getDevAppIDs()
|
||||
|
@ -103,7 +106,7 @@ async function syncUsersToAllApps(userIds: string[]) {
|
|||
}
|
||||
}
|
||||
|
||||
export function initUserGroupSync() {
|
||||
export function initUserGroupSync(updateCb?: () => void) {
|
||||
const types = [constants.DocumentType.USER, constants.DocumentType.GROUP]
|
||||
docUpdates.process(types, async update => {
|
||||
const docId = update.id
|
||||
|
@ -118,6 +121,10 @@ export function initUserGroupSync() {
|
|||
if (userIds.length > 0) {
|
||||
await syncUsersToAllApps(userIds)
|
||||
}
|
||||
// used to tracking when updates have occurred
|
||||
if (updateCb) {
|
||||
updateCb()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
|
||||
import { events, context, roles, db as dbCore } from "@budibase/backend-core"
|
||||
import { initUserGroupSync } from "../sync"
|
||||
import { rawUserMetadata } from "../../../users/utils"
|
||||
import EventEmitter from "events"
|
||||
import { UserMetadata, UserRoles } from "@budibase/types"
|
||||
|
||||
const config = new TestConfiguration()
|
||||
let app
|
||||
const ROLE_ID = roles.BUILTIN_ROLE_IDS.BASIC
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
function updateCb() {
|
||||
emitter.emit("update")
|
||||
}
|
||||
|
||||
function waitForUpdate() {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject()
|
||||
}, 5000)
|
||||
emitter.on("update", () => {
|
||||
clearTimeout(timeout)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await config.init("syncApp")
|
||||
initUserGroupSync(updateCb)
|
||||
})
|
||||
|
||||
async function createUser(email: string, roles: UserRoles, appId?: string) {
|
||||
const user = await config.createUser({ email, roles })
|
||||
await context.doInContext(appId || config.appId!, async () => {
|
||||
await events.user.created(user)
|
||||
})
|
||||
}
|
||||
|
||||
async function getUserMetadata(appId?: string): Promise<UserMetadata[]> {
|
||||
return context.doInContext(appId || config.appId!, async () => {
|
||||
return await rawUserMetadata()
|
||||
})
|
||||
}
|
||||
|
||||
function buildRoles(appId?: string) {
|
||||
const prodAppId = dbCore.getProdAppID(appId || config.appId!)
|
||||
return { [prodAppId]: ROLE_ID }
|
||||
}
|
||||
|
||||
describe("app user/group sync", () => {
|
||||
it("should be able to sync a new user", async () => {
|
||||
const email = "test@test.com"
|
||||
await createUser(email, buildRoles())
|
||||
await waitForUpdate()
|
||||
const metadata = await getUserMetadata()
|
||||
expect(metadata.find(data => data.email === email)).toBeDefined()
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue