mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 17:12:03 -07:00
JSON RPC router now handles all JSON specific data manipulation
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
const common = require('./env/common')
|
||||
|
||||
const env = process.env.KOA_ENV || 'development'
|
||||
const env = process.env.SVC_ENV || 'development'
|
||||
const config = require(`./env/${env}`)
|
||||
|
||||
module.exports = Object.assign({}, common, config)
|
||||
|
||||
+4
-4
@@ -6,10 +6,10 @@
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "npm run test:all",
|
||||
"test:all": "export KOA_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/",
|
||||
"test:unit:lib": "export KOA_ENV=test && mocha --exit --timeout 15000 test/unit/biz-logic/",
|
||||
"test:unit:rest": "export KOA_ENV=test && mocha --exit --timeout 15000 test/unit/rest-api/",
|
||||
"test:e2e:auto": "export KOA_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/",
|
||||
"test:all": "export SVC_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/",
|
||||
"test:unit:lib": "export SVC_ENV=test && mocha --exit --timeout 15000 test/unit/biz-logic/",
|
||||
"test:unit:rest": "export SVC_ENV=test && mocha --exit --timeout 15000 test/unit/rest-api/",
|
||||
"test:e2e:auto": "export SVC_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/",
|
||||
"lint": "standard --env mocha --fix",
|
||||
"docs": "./node_modules/.bin/apidoc -i src/ -o docs",
|
||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
|
||||
+8
-8
@@ -44,13 +44,13 @@ const wlogger = winston.createLogger({
|
||||
})
|
||||
|
||||
// This controls the logs to CONSOLE
|
||||
/*
|
||||
wlogger.add(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.simple(),
|
||||
level: "info"
|
||||
})
|
||||
)
|
||||
*/
|
||||
if (config.env !== 'test') {
|
||||
wlogger.add(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.simple(),
|
||||
level: 'info'
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = wlogger
|
||||
|
||||
+14
-1
@@ -39,13 +39,26 @@ class AuthRPC {
|
||||
|
||||
async authUser (rpcData) {
|
||||
try {
|
||||
console.log('authUser rpcData: ', rpcData)
|
||||
|
||||
if (!rpcData.payload.params.login) {
|
||||
throw new Error('login must be specified')
|
||||
}
|
||||
if (!rpcData.payload.params.password) {
|
||||
throw new Error('password must be specified')
|
||||
}
|
||||
|
||||
const login = rpcData.payload.params.login
|
||||
const password = rpcData.payload.params.password
|
||||
|
||||
// const user = await this.passport.authUser(ctx, next)
|
||||
// if (!user) {
|
||||
// // ctx.throw(401)
|
||||
// const retJson =
|
||||
// }
|
||||
|
||||
const user = await this.userLib.authUser()
|
||||
const user = await this.userLib.authUser(login, password)
|
||||
console.log('user: ', user)
|
||||
return user
|
||||
|
||||
// const token = user.generateToken()
|
||||
|
||||
+28
-10
@@ -50,20 +50,31 @@ class JSONRPC {
|
||||
}
|
||||
|
||||
// Default return string
|
||||
let retStr = _this.defaultResponse()
|
||||
let retObj = _this.defaultResponse()
|
||||
|
||||
// Route the command to the appropriate route handler.
|
||||
switch (parsedData.payload.id) {
|
||||
case 'users':
|
||||
retStr = await _this.userController.userRouter(parsedData)
|
||||
retObj = await _this.userController.userRouter(parsedData)
|
||||
break
|
||||
case 'auth':
|
||||
retStr = await _this.authController.authRouter(parsedData)
|
||||
retObj = await _this.authController.authRouter(parsedData)
|
||||
break
|
||||
}
|
||||
|
||||
// console.log('retObj: ', retObj)
|
||||
|
||||
// Convert the returned object into a JSON RPC response string.
|
||||
const retJson = _this.jsonrpc.success(parsedData.payload.id, {
|
||||
method: parsedData.payload.method,
|
||||
reciever: from,
|
||||
value: retObj
|
||||
})
|
||||
const retStr = JSON.stringify(retJson, null, 2)
|
||||
// console.log('retStr: ', retStr)
|
||||
|
||||
// Encrypt and publish the response to the originators private OrbitDB,
|
||||
// if ipfs-coord has been initialized and the peers orbitdb is registered.
|
||||
// if ipfs-coord has been initialized and the peers ID is registered.
|
||||
if (_this.ipfsCoord.ipfs) {
|
||||
await _this.ipfsCoord.ipfs.orbitdb.sendToDb(from, retStr)
|
||||
}
|
||||
@@ -80,13 +91,20 @@ class JSONRPC {
|
||||
// The default JSON RPC response if the incoming command could not be routed.
|
||||
defaultResponse () {
|
||||
try {
|
||||
const errorObj = this.jsonrpc.error(
|
||||
'Can not route',
|
||||
new jsonrpc.JsonRpcError('Input does not match routing rules', 422)
|
||||
)
|
||||
const errorStr = JSON.stringify(errorObj)
|
||||
// const errorObj = this.jsonrpc.error(
|
||||
// 'Can not route',
|
||||
// new jsonrpc.JsonRpcError('Input does not match routing rules', 422)
|
||||
// )
|
||||
// const errorStr = JSON.stringify(errorObj)
|
||||
// return errorStr
|
||||
|
||||
return errorStr
|
||||
const errorObj = {
|
||||
success: false,
|
||||
status: 422,
|
||||
message: 'Input does not match routing rules.'
|
||||
}
|
||||
|
||||
return errorObj
|
||||
} catch (err) {
|
||||
console.error('Error in defaultResponse()')
|
||||
throw err
|
||||
|
||||
+13
-4
@@ -24,10 +24,17 @@ class UserRPC {
|
||||
|
||||
// if (rpcData.payload.method === 'getAll') return await this.getAll()
|
||||
|
||||
// const retObj = {
|
||||
// id: 'users'
|
||||
// }
|
||||
let retObj = {}
|
||||
|
||||
// Route the call based on the value of the method property.
|
||||
switch (rpcData.payload.method) {
|
||||
case 'getAll':
|
||||
return await this.getAll()
|
||||
// retObj.method = 'getAll'
|
||||
retObj = await this.getAll()
|
||||
return retObj
|
||||
case 'getUser':
|
||||
return await this.getUser(rpcData)
|
||||
}
|
||||
@@ -43,10 +50,12 @@ class UserRPC {
|
||||
console.log('Executing get all')
|
||||
const users = await this.userLib.getAllUsers()
|
||||
|
||||
const retJson = this.jsonrpc.success('getAll', users)
|
||||
const retStr = JSON.stringify(retJson, null, 2)
|
||||
return users
|
||||
|
||||
return retStr
|
||||
// const retJson = this.jsonrpc.success('getAll', users)
|
||||
// const retStr = JSON.stringify(retJson, null, 2)
|
||||
//
|
||||
// return retStr
|
||||
} catch (err) {
|
||||
console.error('Error in getAll()')
|
||||
throw err
|
||||
|
||||
@@ -7,6 +7,10 @@ const assert = require('chai').assert
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
const sinon = require('sinon')
|
||||
|
||||
// Set the environment variable to signal this is a test.
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries.
|
||||
const JSONRPC = require('../../../src/rpc')
|
||||
|
||||
describe('#JSON RPC', () => {
|
||||
@@ -53,17 +57,19 @@ describe('#JSON RPC', () => {
|
||||
assert.property(jsonObj, 'type')
|
||||
assert.property(jsonObj.payload, 'jsonrpc')
|
||||
assert.property(jsonObj.payload, 'id')
|
||||
assert.property(jsonObj.payload, 'error')
|
||||
assert.property(jsonObj.payload.error, 'message')
|
||||
assert.property(jsonObj.payload.error, 'code')
|
||||
assert.property(jsonObj.payload, 'result')
|
||||
assert.property(jsonObj.payload.result, 'reciever')
|
||||
assert.property(jsonObj.payload.result.value, 'success')
|
||||
assert.property(jsonObj.payload.result.value, 'message')
|
||||
|
||||
// Assert the expected values exist.
|
||||
assert.equal(jsonObj.payload.id, 'Can not route')
|
||||
assert.equal(jsonObj.payload.id, 'unknownId')
|
||||
assert.equal(jsonObj.payload.result.value.success, false)
|
||||
assert.equal(jsonObj.payload.result.value.status, 422)
|
||||
assert.equal(
|
||||
jsonObj.payload.error.message,
|
||||
'Input does not match routing rules'
|
||||
jsonObj.payload.result.value.message,
|
||||
'Input does not match routing rules.'
|
||||
)
|
||||
assert.equal(jsonObj.payload.error.code, 422)
|
||||
})
|
||||
|
||||
it('should catch and handle errors', async () => {
|
||||
@@ -87,7 +93,11 @@ describe('#JSON RPC', () => {
|
||||
const result = await uut.router(jsonStr, 'peerA')
|
||||
// console.log(result)
|
||||
|
||||
assert.equal(result.retStr, 'true')
|
||||
const obj = JSON.parse(result.retStr)
|
||||
// console.log('obj: ', obj)
|
||||
|
||||
assert.equal(obj.result.value, 'true')
|
||||
assert.equal(obj.result.method, 'getAll')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,15 +3,18 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
// const jsonrpc = require('jsonrpc-lite')
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
const mongoose = require('mongoose')
|
||||
const sinon = require('sinon')
|
||||
const assert = require('chai').assert
|
||||
|
||||
const config = require('../../../config')
|
||||
|
||||
// const AuthRPC = require('../../../src/rpc/auth')
|
||||
const AuthRPC = require('../../../src/rpc/auth')
|
||||
|
||||
describe('#AuthRPC', () => {
|
||||
// let uut
|
||||
let uut
|
||||
let sandbox
|
||||
|
||||
before(async () => {
|
||||
// Connect to the Mongo Database.
|
||||
@@ -28,21 +31,47 @@ describe('#AuthRPC', () => {
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
// sandbox = sinon.createSandbox()
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
// uut = new AuthRPC()
|
||||
uut = new AuthRPC()
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
after(() => {
|
||||
mongoose.connection.close()
|
||||
})
|
||||
|
||||
describe('#authRouter', () => {
|
||||
it('should route to the authUser method', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut, 'authUser').resolves(true)
|
||||
|
||||
// Generate the parsed data that the main router would pass to this
|
||||
// endpoint.
|
||||
const authCall = jsonrpc.request('auth', 'authUser', {})
|
||||
const jsonStr = JSON.stringify(authCall, null, 2)
|
||||
const rpcData = jsonrpc.parse(jsonStr)
|
||||
|
||||
const result = await uut.authRouter(rpcData)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#authUser', () => {
|
||||
it('should return a JWT token if user successfully authenticates', async () => {
|
||||
const rpcData = 'placeholder'
|
||||
// Generate the parsed data that the main router would pass to this
|
||||
// endpoint.
|
||||
const authCall = jsonrpc.request('auth', '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)
|
||||
await uut.authUser(rpcData)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user