diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index d76c1741a3..df1797132e 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -23,9 +23,12 @@ const MAX_USERS_UPLOAD_LIMIT = 1000 export const save = async (ctx: any) => { try { - if (!ctx.request.body._id && !ctx.internal && - (!ctx.user || !ctx.user.admin || !ctx.user.admin.global)) { - ctx.throw(403, "Only admin user can create new user.") + if ( + !ctx.request.body._id && + !ctx.internal && + (!ctx.user || !ctx.user.admin || !ctx.user.admin.global) + ) { + ctx.throw(403, "Only admin user can create new user.") } ctx.body = await sdk.users.save(ctx.request.body) } catch (err: any) { diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index 746e1ccf1b..b6da8693da 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -72,12 +72,24 @@ class TestConfiguration { // UTILS - async _req(config: any, params: any, controlFunc: any) { + async _req(config: any, params: any, controlFunc: any, opts: { force?: boolean } = {}) { const request: any = {} // fake cookies, we don't need them request.cookies = { set: () => {}, get: () => {} } request.config = { jwtSecret: env.JWT_SECRET } - request.user = { tenantId: this.getTenantId() } + if (opts.force) { + request.user = { + tenantId: this.getTenantId(), + admin: { global: true }, + builder: { global: true }, + } + } else if (this.defaultUser) { + request.user = this.defaultUser + } else { + request.user = { + tenantId: this.getTenantId() + } + } request.query = {} request.request = { body: config, @@ -129,7 +141,7 @@ class TestConfiguration { email: "test@test.com", password: "test", }) - this.defaultUser = await this.createUser(user) + this.defaultUser = await this.createUser(user, { force: true }) } async createTenant1User() { @@ -137,15 +149,16 @@ class TestConfiguration { email: "tenant1@test.com", password: "test", }) - this.tenant1User = await this.createUser(user) + this.tenant1User = await this.createUser(user, { force: true }) } async createSession(user: User) { - await sessions.createASession(user._id!, { + const session: any = { sessionId: "sessionid", tenantId: user.tenantId, csrfToken: CSRF_TOKEN, - }) + } + await sessions.createASession(user._id!, session) } cookieHeader(cookies: any) { @@ -185,11 +198,11 @@ class TestConfiguration { }) } - async createUser(user?: User) { + async createUser(user?: User, opts: any = {}) { if (!user) { user = structures.users.user() } - const response = await this._req(user, null, controllers.users.save) + const response = await this._req(user, null, controllers.users.save, opts) const body = response as CreateUserResponse return this.getUser(body.email) }