mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-21 16:52:03 -07:00
Moved json-rpc tests to controllers dir
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
# Business Logic Unit Tests
|
||||
|
||||
The unit tests in this directly are concerned with business logic libraries in the /src/lib folder. These are the methods that should be triggered by REST API endpoints. These tests are not concerned with the handling of the REST API request/response, but by the code that is triggered by those endpoints. It also tests any business logic that is not directly associated with a REST API endpoint.
|
||||
+3
-3
@@ -12,9 +12,9 @@ const { v4: uid } = require('uuid')
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries.
|
||||
const JSONRPC = require('../../../src/controllers/json-rpc')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const UseCasesMock = require('../mocks/use-cases')
|
||||
const JSONRPC = require('../../../../src/controllers/json-rpc')
|
||||
const adapters = require('../../mocks/adapters')
|
||||
const UseCasesMock = require('../../mocks/use-cases')
|
||||
|
||||
describe('#JSON RPC', () => {
|
||||
let uut
|
||||
+5
-5
@@ -13,11 +13,11 @@ const { v4: uid } = require('uuid')
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries
|
||||
const config = require('../../../config')
|
||||
const AuthRPC = require('../../../src/controllers/json-rpc/auth')
|
||||
const RateLimit = require('../../../src/controllers/json-rpc/rate-limit')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const UseCasesMock = require('../mocks/use-cases')
|
||||
const config = require('../../../../config')
|
||||
const AuthRPC = require('../../../../src/controllers/json-rpc/auth')
|
||||
const RateLimit = require('../../../../src/controllers/json-rpc/rate-limit')
|
||||
const adapters = require('../../mocks/adapters')
|
||||
const UseCasesMock = require('../../mocks/use-cases')
|
||||
|
||||
describe('#AuthRPC', () => {
|
||||
let uut
|
||||
+2
-2
@@ -14,8 +14,8 @@ const { v4: uid } = require('uuid')
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries
|
||||
const Validators = require('../../../src/controllers/json-rpc/validators')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const Validators = require('../../../../src/controllers/json-rpc/validators')
|
||||
const adapters = require('../../mocks/adapters')
|
||||
|
||||
describe('#validators', () => {
|
||||
let uut
|
||||
+5
-5
@@ -13,12 +13,12 @@ const { v4: uid } = require('uuid')
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries
|
||||
const config = require('../../../config')
|
||||
const UserRPC = require('../../../src/controllers/json-rpc/users')
|
||||
const RateLimit = require('../../../src/controllers/json-rpc/rate-limit')
|
||||
const config = require('../../../../config')
|
||||
const UserRPC = require('../../../../src/controllers/json-rpc/users')
|
||||
const RateLimit = require('../../../../src/controllers/json-rpc/rate-limit')
|
||||
// const UserModel = require('../../../src/adapters/localdb/models/users')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const UseCasesMock = require('../mocks/use-cases')
|
||||
const adapters = require('../../mocks/adapters')
|
||||
const UseCasesMock = require('../../mocks/use-cases')
|
||||
|
||||
describe('#UserRPC', () => {
|
||||
let uut
|
||||
+1
-1
@@ -12,7 +12,7 @@ const assert = require('chai').assert
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
// Local libraries
|
||||
const RateLimit = require('../../../src/controllers/json-rpc/rate-limit')
|
||||
const RateLimit = require('../../../../src/controllers/json-rpc/rate-limit')
|
||||
|
||||
describe('#rate-limit', () => {
|
||||
let uut
|
||||
@@ -1,110 +0,0 @@
|
||||
/*
|
||||
Unit tests for the rpc/index.js library.
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
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'
|
||||
|
||||
// Local libraries.
|
||||
const JSONRPC = require('../../../src/controllers/json-rpc')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const UseCasesMock = require('../mocks/use-cases')
|
||||
|
||||
describe('#JSON RPC', () => {
|
||||
let uut
|
||||
let sandbox
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
const useCases = new UseCasesMock()
|
||||
uut = new JSONRPC({ adapters, useCases })
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#router', () => {
|
||||
it('should exit quietly if given a random string', async () => {
|
||||
const str = 'random string message'
|
||||
await uut.router(str)
|
||||
|
||||
assert.isOk('Not throwing an error is a pass.')
|
||||
})
|
||||
|
||||
it('should exit quietly if invalid JSON RPC message received', async () => {
|
||||
const malformedRpc = '{"jsonrpc":"2.0"}'
|
||||
|
||||
await uut.router(malformedRpc, 'peerA')
|
||||
|
||||
assert.isOk('Not throwing an error is a pass.')
|
||||
})
|
||||
|
||||
it('should return default response if routing is not possible', async () => {
|
||||
const id = uid()
|
||||
const json = jsonrpc.request(id, 'unknownMethod', {})
|
||||
|
||||
const str = JSON.stringify(json)
|
||||
|
||||
const result = await uut.router(str, 'peerA')
|
||||
// console.log('result: ', result)
|
||||
|
||||
const jsonObj = jsonrpc.parse(result.retStr)
|
||||
// console.log(`jsonObj: ${JSON.stringify(jsonObj, null, 2)}`)
|
||||
|
||||
// Assert the expected properties exist on the returned object.
|
||||
assert.property(jsonObj, 'payload')
|
||||
assert.property(jsonObj, 'type')
|
||||
assert.property(jsonObj.payload, 'jsonrpc')
|
||||
assert.property(jsonObj.payload, 'id')
|
||||
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, id)
|
||||
assert.equal(jsonObj.payload.result.value.success, false)
|
||||
assert.equal(jsonObj.payload.result.value.status, 422)
|
||||
assert.equal(
|
||||
jsonObj.payload.result.value.message,
|
||||
'Input does not match routing rules.'
|
||||
)
|
||||
})
|
||||
|
||||
it('should catch and handle errors', async () => {
|
||||
// Force an error
|
||||
sandbox.stub(uut.jsonrpc, 'parse').throws(new Error('test error'))
|
||||
|
||||
const malformedRpc = '{"jsonrpc":"2.0"}'
|
||||
|
||||
await uut.router(malformedRpc, 'peerA')
|
||||
|
||||
assert.isOk('Not throwing an error is a pass.')
|
||||
})
|
||||
|
||||
it('should route to users handler', async () => {
|
||||
const id = uid()
|
||||
const userCall = jsonrpc.request(id, 'users', { endpoint: 'getAll' })
|
||||
const jsonStr = JSON.stringify(userCall, null, 2)
|
||||
|
||||
// Mock the users controller.
|
||||
sandbox.stub(uut.userController, 'userRouter').resolves('true')
|
||||
|
||||
const result = await uut.router(jsonStr, 'peerA')
|
||||
// console.log(result)
|
||||
|
||||
const obj = JSON.parse(result.retStr)
|
||||
// console.log('obj: ', obj)
|
||||
|
||||
assert.equal(obj.result.value, 'true')
|
||||
assert.equal(obj.result.method, 'users')
|
||||
assert.equal(obj.id, id)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user