Got JSON RPC unit tests passing

This commit is contained in:
Chris Troutner
2021-04-06 16:45:20 -07:00
parent fbafcf32cd
commit 0abe291a1d
8 changed files with 61 additions and 28 deletions
+13 -3
View File
@@ -11139,6 +11139,11 @@
"requires": {
"glob": "^7.1.3"
}
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
@@ -19220,6 +19225,11 @@
"version": "6.5.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
@@ -21457,9 +21467,9 @@
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"uuid": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz",
"integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ=="
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"v8-compile-cache": {
"version": "2.2.0",
+1
View File
@@ -56,6 +56,7 @@
"mongoose": "^5.11.15",
"nodemailer": "^6.4.17",
"passport-local": "^1.0.0",
"uuid": "^8.3.2",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0"
},
+11 -11
View File
@@ -22,13 +22,12 @@ class AuthRPC {
// methods.
async authRouter (rpcData) {
try {
console.log('authRouter rpcData: ', rpcData)
// console.log('authRouter rpcData: ', rpcData)
// Default return object.
// let retObj = {}
const endpoint = rpcData.payload.params.endpoint
// Route the call based on the value of the method property.
switch (rpcData.payload.method) {
// Route the call based on the requested endpoint.
switch (endpoint) {
case 'authUser':
return await this.authUser(rpcData)
}
@@ -40,7 +39,7 @@ class AuthRPC {
async authUser (rpcData) {
try {
console.log('authUser rpcData: ', rpcData)
// console.log('authUser rpcData: ', rpcData)
if (!rpcData.payload.params.login) {
throw new Error('login must be specified')
@@ -58,11 +57,12 @@ class AuthRPC {
const token = user.generateToken()
const response = {
id: user._id,
type: user.type,
name: user.name,
email: user.email,
token
endpoint: 'authUser',
userId: user._id,
userType: user.type,
userName: user.name,
userEmail: user.email,
apiToken: token
}
return response
+1 -1
View File
@@ -53,7 +53,7 @@ class JSONRPC {
let retObj = _this.defaultResponse()
// Route the command to the appropriate route handler.
switch (parsedData.payload.id) {
switch (parsedData.payload.method) {
case 'users':
retObj = await _this.userController.userRouter(parsedData)
break
+6 -4
View File
@@ -22,11 +22,10 @@ class UserRPC {
try {
// console.log('userRouter rpcData: ', rpcData)
// Default return object.
// let retObj = {}
const endpoint = rpcData.payload.params.endpoint
// Route the call based on the value of the method property.
switch (rpcData.payload.method) {
switch (endpoint) {
case 'getAll':
return await this.getAll()
case 'getUser':
@@ -44,7 +43,10 @@ class UserRPC {
console.log('Executing get all')
const users = await this.userLib.getAllUsers()
return users
return {
endpoint: 'getAll',
users
}
} catch (err) {
console.error('Error in getAll()')
throw err
+8 -4
View File
@@ -6,6 +6,7 @@
const assert = require('chai').assert
const jsonrpc = require('jsonrpc-lite')
const sinon = require('sinon')
const { v4: uid } = require('uuid')
// Set the environment variable to signal this is a test.
process.env.SVC_ENV = 'test'
@@ -42,7 +43,8 @@ describe('#JSON RPC', () => {
})
it('should return default response if routing is not possible', async () => {
const json = jsonrpc.request('unknownId', 'unknownMethod', {})
const id = uid()
const json = jsonrpc.request(id, 'unknownMethod', {})
const str = JSON.stringify(json)
@@ -63,7 +65,7 @@ describe('#JSON RPC', () => {
assert.property(jsonObj.payload.result.value, 'message')
// Assert the expected values exist.
assert.equal(jsonObj.payload.id, 'unknownId')
assert.equal(jsonObj.payload.id, id)
assert.equal(jsonObj.payload.result.value.success, false)
assert.equal(jsonObj.payload.result.value.status, 422)
assert.equal(
@@ -84,7 +86,8 @@ describe('#JSON RPC', () => {
})
it('should route to users handler', async () => {
const userCall = jsonrpc.request('users', 'getAll', {})
const id = uid()
const userCall = jsonrpc.request(id, 'users', { endpoint: 'getAll' })
const jsonStr = JSON.stringify(userCall, null, 2)
// Mock the users controller.
@@ -97,7 +100,8 @@ describe('#JSON RPC', () => {
// console.log('obj: ', obj)
assert.equal(obj.result.value, 'true')
assert.equal(obj.result.method, 'getAll')
assert.equal(obj.result.method, 'users')
assert.equal(obj.id, id)
})
})
})
+15 -4
View File
@@ -7,6 +7,7 @@ const jsonrpc = require('jsonrpc-lite')
const mongoose = require('mongoose')
const sinon = require('sinon')
const assert = require('chai').assert
const { v4: uid } = require('uuid')
const config = require('../../../config')
@@ -49,7 +50,8 @@ describe('#AuthRPC', () => {
// Generate the parsed data that the main router would pass to this
// endpoint.
const authCall = jsonrpc.request('auth', 'authUser', {})
const id = uid()
const authCall = jsonrpc.request(id, 'auth', { endpoint: 'authUser' })
const jsonStr = JSON.stringify(authCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
@@ -63,15 +65,24 @@ describe('#AuthRPC', () => {
it('should return a JWT token if user successfully authenticates', async () => {
// Generate the parsed data that the main router would pass to this
// endpoint.
const authCall = jsonrpc.request('auth', 'authUser', {
const id = uid()
const authCall = jsonrpc.request(id, 'auth', {
endpoint: 'authUser',
login: 'test@test.com',
password: 'password'
})
const jsonStr = JSON.stringify(authCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
console.log(rpcData)
await uut.authUser(rpcData)
const response = await uut.authUser(rpcData)
// console.log('response: ', response)
assert.equal(response.endpoint, 'authUser')
assert.property(response, 'userId')
assert.equal(response.userType, 'user')
assert.property(response, 'userName')
assert.property(response, 'userEmail')
assert.property(response, 'apiToken')
})
})
})
+6 -1
View File
@@ -7,6 +7,7 @@ const jsonrpc = require('jsonrpc-lite')
const mongoose = require('mongoose')
const sinon = require('sinon')
const assert = require('chai').assert
const { v4: uid } = require('uuid')
const config = require('../../../config')
@@ -49,7 +50,8 @@ describe('#UserRPC', () => {
// Generate the parsed data that the main router would pass to this
// endpoint.
const userCall = jsonrpc.request('users', 'getAll', {})
const id = uid()
const userCall = jsonrpc.request(id, 'users', { endpoint: 'getAll' })
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
@@ -63,6 +65,9 @@ describe('#UserRPC', () => {
it('should return all users', async () => {
const result = await uut.getAll()
console.log('result: ', result)
assert.equal(result.endpoint, 'getAll')
assert.property(result, 'users')
})
})
})