diff --git a/packages/server/src/api/routes/tests/user.spec.ts b/packages/server/src/api/routes/tests/user.spec.ts index e6349099d7..076ee064dc 100644 --- a/packages/server/src/api/routes/tests/user.spec.ts +++ b/packages/server/src/api/routes/tests/user.spec.ts @@ -27,15 +27,17 @@ describe("/users", () => { describe("fetch", () => { it("returns a list of users from an instance db", async () => { - await config.createUser({ id: "uuidx" }) - await config.createUser({ id: "uuidy" }) + const id1 = `us_${utils.newid()}` + const id2 = `us_${utils.newid()}` + await config.createUser({ _id: id1 }) + await config.createUser({ _id: id2 }) const res = await config.api.user.fetch() expect(res.length).toBe(3) const ids = res.map(u => u._id) - expect(ids).toContain(`ro_ta_users_us_uuidx`) - expect(ids).toContain(`ro_ta_users_us_uuidy`) + expect(ids).toContain(`ro_ta_users_${id1}`) + expect(ids).toContain(`ro_ta_users_${id2}`) }) it("should apply authorization to endpoint", async () => { @@ -54,7 +56,7 @@ describe("/users", () => { describe("update", () => { it("should be able to update the user", async () => { const user: UserMetadata = await config.createUser({ - id: `us_update${utils.newid()}`, + _id: `us_update${utils.newid()}`, }) user.roleId = roles.BUILTIN_ROLE_IDS.BASIC delete user._rev diff --git a/packages/server/src/migrations/tests/index.spec.ts b/packages/server/src/migrations/tests/index.spec.ts index c01040593a..236776cd3f 100644 --- a/packages/server/src/migrations/tests/index.spec.ts +++ b/packages/server/src/migrations/tests/index.spec.ts @@ -40,7 +40,7 @@ describe("migrations", () => { describe("backfill", () => { it("runs app db migration", async () => { - await config.doInContext(null, async () => { + await config.doInContext(undefined, async () => { await clearMigrations() await config.createAutomation() await config.createAutomation(structures.newAutomation()) @@ -93,18 +93,18 @@ describe("migrations", () => { }) it("runs global db migration", async () => { - await config.doInContext(null, async () => { + await config.doInContext(undefined, async () => { await clearMigrations() - const appId = config.prodAppId + const appId = config.getProdAppId() const roles = { [appId]: "role_12345" } await config.createUser({ - builder: false, - admin: true, + builder: { global: false }, + admin: { global: true }, roles, }) // admin only await config.createUser({ - builder: false, - admin: false, + builder: { global: false }, + admin: { global: false }, roles, }) // non admin non builder await config.createTable() diff --git a/packages/server/src/sdk/app/applications/tests/sync.spec.ts b/packages/server/src/sdk/app/applications/tests/sync.spec.ts index 1d28ed977c..a53bdb0bd7 100644 --- a/packages/server/src/sdk/app/applications/tests/sync.spec.ts +++ b/packages/server/src/sdk/app/applications/tests/sync.spec.ts @@ -43,8 +43,8 @@ async function createUser(email: string, roles: UserRoles, builder?: boolean) { const user = await config.createUser({ email, roles, - builder: builder || false, - admin: false, + builder: { global: builder || false }, + admin: { global: false }, }) await context.doInContext(config.appId!, async () => { await events.user.created(user) @@ -55,10 +55,10 @@ async function createUser(email: string, roles: UserRoles, builder?: boolean) { async function removeUserRole(user: User) { const final = await config.globalUser({ ...user, - id: user._id, + _id: user._id, roles: {}, - builder: false, - admin: false, + builder: { global: false }, + admin: { global: false }, }) await context.doInContext(config.appId!, async () => { await events.user.updated(final) @@ -69,8 +69,8 @@ async function createGroupAndUser(email: string) { groupUser = await config.createUser({ email, roles: {}, - builder: false, - admin: false, + builder: { global: false }, + admin: { global: false }, }) group = await config.createGroup() await config.addUserToGroup(group._id!, groupUser._id!) diff --git a/packages/server/src/sdk/users/tests/utils.spec.ts b/packages/server/src/sdk/users/tests/utils.spec.ts index efe790d49b..6f1c5afd3d 100644 --- a/packages/server/src/sdk/users/tests/utils.spec.ts +++ b/packages/server/src/sdk/users/tests/utils.spec.ts @@ -22,15 +22,18 @@ describe("syncGlobalUsers", () => { expect(metadata).toHaveLength(1) expect(metadata).toEqual([ expect.objectContaining({ - _id: db.generateUserMetadataID(config.user._id), + _id: db.generateUserMetadataID(config.getUser()._id!), }), ]) }) }) it("admin and builders users are synced", async () => { - const user1 = await config.createUser({ admin: true }) - const user2 = await config.createUser({ admin: false, builder: true }) + const user1 = await config.createUser({ admin: { global: true } }) + const user2 = await config.createUser({ + admin: { global: false }, + builder: { global: true }, + }) await config.doInContext(config.appId, async () => { expect(await rawUserMetadata()).toHaveLength(1) await syncGlobalUsers() @@ -51,7 +54,10 @@ describe("syncGlobalUsers", () => { }) it("app users are not synced if not specified", async () => { - const user = await config.createUser({ admin: false, builder: false }) + const user = await config.createUser({ + admin: { global: false }, + builder: { global: false }, + }) await config.doInContext(config.appId, async () => { await syncGlobalUsers() @@ -68,8 +74,14 @@ describe("syncGlobalUsers", () => { it("app users are added when group is assigned to app", async () => { await config.doInTenant(async () => { const group = await proSdk.groups.save(structures.userGroups.userGroup()) - const user1 = await config.createUser({ admin: false, builder: false }) - const user2 = await config.createUser({ admin: false, builder: false }) + const user1 = await config.createUser({ + admin: { global: false }, + builder: { global: false }, + }) + const user2 = await config.createUser({ + admin: { global: false }, + builder: { global: false }, + }) await proSdk.groups.addUsers(group.id, [user1._id!, user2._id!]) await config.doInContext(config.appId, async () => { @@ -103,8 +115,14 @@ describe("syncGlobalUsers", () => { it("app users are removed when app is removed from user group", async () => { await config.doInTenant(async () => { const group = await proSdk.groups.save(structures.userGroups.userGroup()) - const user1 = await config.createUser({ admin: false, builder: false }) - const user2 = await config.createUser({ admin: false, builder: false }) + const user1 = await config.createUser({ + admin: { global: false }, + builder: { global: false }, + }) + const user2 = await config.createUser({ + admin: { global: false }, + builder: { global: false }, + }) await proSdk.groups.updateGroupApps(group.id, { appsToAdd: [ { appId: config.prodAppId!, roleId: roles.BUILTIN_ROLE_IDS.BASIC }, diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 5333f1ebf2..f6f0992585 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -307,23 +307,28 @@ export default class TestConfiguration { builder = { global: true }, admin = { global: false }, email = generator.email(), - roles, + tenantId = this.getTenantId(), + roles = {}, } = config const db = tenancy.getTenantDB(this.getTenantId()) - let existing + let existing: Partial = {} try { existing = await db.get(_id) } catch (err) { - existing = { email } + // ignore } const user: User = { - _id: _id, + _id, ...existing, - roles: roles || {}, - tenantId: this.getTenantId(), + ...config, + email, + roles, + tenantId, firstName, lastName, + builder, + admin, } await sessions.createASession(_id, { sessionId: "sessionid", @@ -331,7 +336,10 @@ export default class TestConfiguration { csrfToken: this.csrfToken, }) const resp = await db.put(user) - return { _rev: resp.rev, ...user } + return { + _rev: resp.rev, + ...user, + } } async createUser(user: Partial = {}): Promise { @@ -751,7 +759,7 @@ export default class TestConfiguration { if (!automation) { return } - return this._req(automationController.destroy, { + return this._req(automationController.destroy, undefined, { id: automation._id, rev: automation._rev, })