112 lines
2.7 KiB
JavaScript
112 lines
2.7 KiB
JavaScript
|
const User = require('../../models/User')
|
||
|
const crypto = require('crypto')
|
||
|
|
||
|
const testUserName = 'jestTestUser'
|
||
|
const password = 'Beans1234!!!'
|
||
|
const secondPassword = 'Rice1234!!!'
|
||
|
|
||
|
let testUserId = null
|
||
|
let masterKey = null
|
||
|
|
||
|
beforeAll(() => {
|
||
|
|
||
|
// Find and Delete Previous Test user
|
||
|
return User.getByUserName(testUserName)
|
||
|
.then((user) => {
|
||
|
return User.deleteUser(user?.id, password)
|
||
|
})
|
||
|
.then((results) => {
|
||
|
|
||
|
return results
|
||
|
})
|
||
|
|
||
|
})
|
||
|
|
||
|
test('Test User Registration', () => {
|
||
|
|
||
|
return User.register(testUserName, password)
|
||
|
.then((({ token, userId }) => {
|
||
|
|
||
|
testUserId = userId
|
||
|
|
||
|
expect(token).toBeDefined()
|
||
|
expect(userId).toBeGreaterThan(0)
|
||
|
}))
|
||
|
})
|
||
|
|
||
|
test('Test decrypting user masterKey', () => {
|
||
|
|
||
|
return User.getMasterKey(testUserId, password)
|
||
|
.then((newMasterKey) => {
|
||
|
masterKey = newMasterKey
|
||
|
|
||
|
expect(masterKey).toBeDefined()
|
||
|
})
|
||
|
})
|
||
|
|
||
|
test('Test generating public and private key pair', () => {
|
||
|
|
||
|
return User.generateKeypair(testUserId, masterKey)
|
||
|
.then(({publicKey, privateKey}) => {
|
||
|
|
||
|
const publicKeyMessage = 'Test: Public key decrypt - Pass'
|
||
|
const privateKeyMessage = 'Test: Private key decrypt - Pass'
|
||
|
|
||
|
//Encrypt Message with private Key
|
||
|
const privateKeyEncrypted = crypto.privateEncrypt(privateKey, Buffer.from(privateKeyMessage, 'utf8')).toString('base64')
|
||
|
const decryptedPrivate = crypto.publicDecrypt(publicKey, Buffer.from(privateKeyEncrypted, 'base64'))
|
||
|
//Conver back to a string
|
||
|
expect(decryptedPrivate.toString('utf8')).toMatch(privateKeyMessage)
|
||
|
|
||
|
//Encrypt with public key
|
||
|
const pubEncrMsc = crypto.publicEncrypt(publicKey, Buffer.from(publicKeyMessage, 'utf8')).toString('base64')
|
||
|
const publicDeccryptMessage = crypto.privateDecrypt(privateKey, Buffer.from(pubEncrMsc, 'base64') )
|
||
|
//Convert it back to string
|
||
|
expect(publicDeccryptMessage.toString('utf8')).toMatch(publicKeyMessage)
|
||
|
|
||
|
})
|
||
|
})
|
||
|
|
||
|
test('Test Logging in User', () => {
|
||
|
|
||
|
return User.login(testUserName, password)
|
||
|
.then(({token, userId}) => {
|
||
|
expect(token).toBeDefined()
|
||
|
expect(userId).toBeGreaterThan(0)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
test('Test Changing Password', () => {
|
||
|
return User.changePassword(testUserId, password, secondPassword)
|
||
|
.then((passwordChangeResults) => {
|
||
|
|
||
|
expect(passwordChangeResults).toBe(true)
|
||
|
|
||
|
})
|
||
|
})
|
||
|
|
||
|
test('Test Login with wrong password', () => {
|
||
|
|
||
|
return User.login(testUserName, password)
|
||
|
.then(({token, userId}) => {
|
||
|
|
||
|
expect(token).toBeNull()
|
||
|
expect(userId).toBeNull()
|
||
|
})
|
||
|
})
|
||
|
|
||
|
test('Test decrypting masterKey with new Password', () => {
|
||
|
return User.getMasterKey(testUserId, secondPassword)
|
||
|
.then((newMasterKey) => {
|
||
|
|
||
|
expect(newMasterKey).toBeDefined()
|
||
|
expect(newMasterKey.length).toBe(28)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
afterAll(done => {
|
||
|
// Close Database
|
||
|
const db = require('../../config/database')
|
||
|
db.end()
|
||
|
done()
|
||
|
})
|