Fix unit tests

This commit is contained in:
Mel O'Hagan 2022-11-29 15:13:58 +00:00
parent 2ac638fc26
commit 85c4095656
2 changed files with 27 additions and 11 deletions

View File

@ -23,8 +23,11 @@ const MAX_USERS_UPLOAD_LIMIT = 1000
export const save = async (ctx: any) => { export const save = async (ctx: any) => {
try { try {
if (!ctx.request.body._id && !ctx.internal && if (
(!ctx.user || !ctx.user.admin || !ctx.user.admin.global)) { !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.throw(403, "Only admin user can create new user.")
} }
ctx.body = await sdk.users.save(ctx.request.body) ctx.body = await sdk.users.save(ctx.request.body)

View File

@ -72,12 +72,24 @@ class TestConfiguration {
// UTILS // UTILS
async _req(config: any, params: any, controlFunc: any) { async _req(config: any, params: any, controlFunc: any, opts: { force?: boolean } = {}) {
const request: any = {} const request: any = {}
// fake cookies, we don't need them // fake cookies, we don't need them
request.cookies = { set: () => {}, get: () => {} } request.cookies = { set: () => {}, get: () => {} }
request.config = { jwtSecret: env.JWT_SECRET } 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.query = {}
request.request = { request.request = {
body: config, body: config,
@ -129,7 +141,7 @@ class TestConfiguration {
email: "test@test.com", email: "test@test.com",
password: "test", password: "test",
}) })
this.defaultUser = await this.createUser(user) this.defaultUser = await this.createUser(user, { force: true })
} }
async createTenant1User() { async createTenant1User() {
@ -137,15 +149,16 @@ class TestConfiguration {
email: "tenant1@test.com", email: "tenant1@test.com",
password: "test", password: "test",
}) })
this.tenant1User = await this.createUser(user) this.tenant1User = await this.createUser(user, { force: true })
} }
async createSession(user: User) { async createSession(user: User) {
await sessions.createASession(user._id!, { const session: any = {
sessionId: "sessionid", sessionId: "sessionid",
tenantId: user.tenantId, tenantId: user.tenantId,
csrfToken: CSRF_TOKEN, csrfToken: CSRF_TOKEN,
}) }
await sessions.createASession(user._id!, session)
} }
cookieHeader(cookies: any) { cookieHeader(cookies: any) {
@ -185,11 +198,11 @@ class TestConfiguration {
}) })
} }
async createUser(user?: User) { async createUser(user?: User, opts: any = {}) {
if (!user) { if (!user) {
user = structures.users.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 const body = response as CreateUserResponse
return this.getUser(body.email) return this.getUser(body.email)
} }