authenzify
Version:
server to manage authentication authorization of users and more
95 lines (78 loc) • 2.58 kB
JavaScript
import getPort from 'get-port'
import * as assert from 'assert'
import { before, after } from 'mocha'
import { getConfig } from '../../util/settings.js'
import { dropDatabase } from '../../util/mongodb-util.js'
import { usersManagementServer } from '../../../src/app.js'
import { SIGN_UP_SUCCEEDED } from '../../../src/api/responses.js'
import { initVerifyToken } from '../../../src/index.js'
import { USER_ACTIONS } from '../../../src/errors/error-codes.js'
describe('Update user', async () => {
let server
let config
before(async () => {
const port = await getPort()
config = await getConfig({ port })
const storageConfig = config.storage
await dropDatabase(storageConfig)
server = (await usersManagementServer(config)).server
})
after(async () => {
await server?.close()
})
describe(`Verify user extra data`, () => {
it('Should test sign up user with extra data', async () => {
const { USER_EMAIL, USER_PASSWORD } = process.env
const profile = {
pet: 'Dog',
lastName: 'Rubin',
firstName: 'Nisim',
address: '3006 denver Ave. Bridgeport CT',
}
const credentials = {
email: USER_EMAIL,
password: USER_PASSWORD,
...profile,
}
const res = await server
.inject()
.body(credentials)
.post('/v1/users/sign-up')
const { statusCode } = res
const msg = res.json()
assert.deepEqual(
{ statusCode, msg },
{
statusCode: SIGN_UP_SUCCEEDED.httpStatusCode,
msg: SIGN_UP_SUCCEEDED.httpResponse,
},
)
const signInResponse = await server
.inject()
.body(credentials)
.post('/v1/users/sign-in')
.body({ email: USER_EMAIL, password: USER_PASSWORD })
const tokenResponse = await signInResponse.json()
const { verifyToken } = initVerifyToken({
publicKey: config.publicKey,
jwtOptions: config.jwtOptions,
})
const encoded = verifyToken(tokenResponse.token)
const cookies = { [config.authorizationCookieKey]: tokenResponse.token }
const profileUpdated = {
...profile,
firstName: '',
}
const updateProfileResponse = await server
.inject()
.body(profileUpdated)
.cookies(cookies)
.put(`/v1/users/${encoded.id}/profile`)
const userData = await updateProfileResponse.json()
assert.equal(
userData.code,
USER_ACTIONS.ITS_INVALID_TO_OVERRIDE_PROFILE_PROPS_TO_BLANK.code,
)
})
})
})