authenzify
Version:
server to manage authentication authorization of users and more
92 lines (75 loc) • 2.55 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'
describe('Sign up', 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 update user profile 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,
address: '430 Hillcrest Rd. Bridgeport CT',
}
const updateProfileResponse = await server
.inject()
.body(profileUpdated)
.cookies(cookies)
.put(`/v1/users/${encoded.id}/profile`)
const userData = await updateProfileResponse.json()
assert.equal(encoded.profile.pet, credentials.pet)
assert.equal(userData.profile.address, profileUpdated.address)
})
})
})