mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
fix(tests): Increased test coverage to 100%
This commit is contained in:
@@ -174,10 +174,10 @@ class IPFS {
|
|||||||
// If the peer couldn't be found, skip.
|
// If the peer couldn't be found, skip.
|
||||||
if (!thisPeer.length) {
|
if (!thisPeer.length) {
|
||||||
thisRelay.name = ''
|
thisRelay.name = ''
|
||||||
|
thisRelay.description = ''
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
thisRelay.name = thisPeer[0].data.jsonLd.name
|
thisRelay.name = thisPeer[0].data.jsonLd.name
|
||||||
thisRelay.description = thisPeer[0].data.jsonLd.description
|
thisRelay.description = thisPeer[0].data.jsonLd.description
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
import wlogger from '../../../adapters/wlogger.js'
|
import wlogger from '../../../adapters/wlogger.js'
|
||||||
|
|
||||||
let _this
|
|
||||||
|
|
||||||
class IpfsRESTControllerLib {
|
class IpfsRESTControllerLib {
|
||||||
constructor (localConfig = {}) {
|
constructor (localConfig = {}) {
|
||||||
// Dependency Injection.
|
// Dependency Injection.
|
||||||
@@ -26,7 +24,11 @@ class IpfsRESTControllerLib {
|
|||||||
// this.UserModel = this.adapters.localdb.Users
|
// this.UserModel = this.adapters.localdb.Users
|
||||||
// this.userUseCases = this.useCases.user
|
// this.userUseCases = this.useCases.user
|
||||||
|
|
||||||
_this = this
|
// Bind 'this' object to all subfunctions
|
||||||
|
this.getStatus = this.getStatus.bind(this)
|
||||||
|
this.getPeers = this.getPeers.bind(this)
|
||||||
|
this.getRelays = this.getRelays.bind(this)
|
||||||
|
this.handleError = this.handleError.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,13 +43,13 @@ class IpfsRESTControllerLib {
|
|||||||
*/
|
*/
|
||||||
async getStatus (ctx) {
|
async getStatus (ctx) {
|
||||||
try {
|
try {
|
||||||
const status = await _this.adapters.ipfs.getStatus()
|
const status = await this.adapters.ipfs.getStatus()
|
||||||
|
|
||||||
ctx.body = { status }
|
ctx.body = { status }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
wlogger.error('Error in ipfs/controller.js/getStatus(): ')
|
wlogger.error('Error in ipfs/controller.js/getStatus(): ')
|
||||||
// ctx.throw(422, err.message)
|
// ctx.throw(422, err.message)
|
||||||
_this.handleError(ctx, err)
|
this.handleError(ctx, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,26 +58,26 @@ class IpfsRESTControllerLib {
|
|||||||
try {
|
try {
|
||||||
const showAll = ctx.request.body.showAll
|
const showAll = ctx.request.body.showAll
|
||||||
|
|
||||||
const peers = await _this.adapters.ipfs.getPeers(showAll)
|
const peers = await this.adapters.ipfs.getPeers(showAll)
|
||||||
|
|
||||||
ctx.body = { peers }
|
ctx.body = { peers }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
wlogger.error('Error in ipfs/controller.js/getPeers(): ')
|
wlogger.error('Error in ipfs/controller.js/getPeers(): ')
|
||||||
// ctx.throw(422, err.message)
|
// ctx.throw(422, err.message)
|
||||||
_this.handleError(ctx, err)
|
this.handleError(ctx, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get data about the known Circuit Relays. Hydrate with data from peers list.
|
// Get data about the known Circuit Relays. Hydrate with data from peers list.
|
||||||
async getRelays (ctx) {
|
async getRelays (ctx) {
|
||||||
try {
|
try {
|
||||||
const relays = await _this.adapters.ipfs.getRelays()
|
const relays = await this.adapters.ipfs.getRelays()
|
||||||
|
|
||||||
ctx.body = { relays }
|
ctx.body = { relays }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
wlogger.error('Error in ipfs/controller.js/getRelays(): ')
|
wlogger.error('Error in ipfs/controller.js/getRelays(): ')
|
||||||
// ctx.throw(422, err.message)
|
// ctx.throw(422, err.message)
|
||||||
_this.handleError(ctx, err)
|
this.handleError(ctx, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import Validators from '../middleware/validators.js'
|
|||||||
|
|
||||||
// let _this
|
// let _this
|
||||||
|
|
||||||
class BchRouter {
|
class IpfsRouter {
|
||||||
constructor (localConfig = {}) {
|
constructor (localConfig = {}) {
|
||||||
// Dependency Injection.
|
// Dependency Injection.
|
||||||
this.adapters = localConfig.adapters
|
this.adapters = localConfig.adapters
|
||||||
@@ -66,4 +66,4 @@ class BchRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// module.exports = BchRouter
|
// module.exports = BchRouter
|
||||||
export default BchRouter
|
export default IpfsRouter
|
||||||
|
|||||||
@@ -141,4 +141,80 @@ describe('#IPFS-adapter-index', () => {
|
|||||||
assert.equal(result.length, inArray.length - 2)
|
assert.equal(result.length, inArray.length - 2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#getPeers', () => {
|
||||||
|
it('should return an array of current JSON data about each peer when showAll is true', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
uut.ipfsCoordAdapter.ipfsCoord = {
|
||||||
|
thisNode: {
|
||||||
|
peerData: [{ from: 'a', data: { jsonLd: { name: 'a', protocol: 'test', version: '1' } } }, { from: 'b' }]
|
||||||
|
},
|
||||||
|
adapters: {
|
||||||
|
ipfs: {
|
||||||
|
getPeers: async () => ['a', 'b']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await uut.getPeers(true)
|
||||||
|
|
||||||
|
assert.isArray(result)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should catch, report, and throw errors', async () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
uut.ipfsCoordAdapter.ipfsCoord = {
|
||||||
|
thisNode: {},
|
||||||
|
adapters: {
|
||||||
|
ipfs: {
|
||||||
|
getPeers: async () => { throw new Error('test error') }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await uut.getPeers()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err.message: ', err.message)
|
||||||
|
assert.include(err.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#getRelays', () => {
|
||||||
|
it('should return known relays', () => {
|
||||||
|
uut.ipfsCoordAdapter.ipfsCoord = {
|
||||||
|
thisNode: {
|
||||||
|
peerData: [{ from: 'a', data: { jsonLd: { name: 'a', description: 'test' } } }, { from: 'b' }],
|
||||||
|
relayData: [{ ipfsId: 'c' }, { ipfsId: 'a' }]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = uut.getRelays()
|
||||||
|
// console.log('result: ', result)
|
||||||
|
|
||||||
|
assert.isArray(result)
|
||||||
|
assert.equal(result.length, 2)
|
||||||
|
assert.property(result[0], 'name')
|
||||||
|
assert.property(result[0], 'description')
|
||||||
|
assert.property(result[1], 'name')
|
||||||
|
assert.property(result[1], 'description')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should catch, report, and throw errors', () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
uut.ipfsCoordAdapter.ipfsCoord = { thisNode: {} }
|
||||||
|
|
||||||
|
uut.getRelays()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err.message: ', err.message)
|
||||||
|
assert.include(err.message, 'Cannot read')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,179 @@
|
|||||||
|
/*
|
||||||
|
Unit tests for the REST API handler for the /ipfs endpoints.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Public npm libraries
|
||||||
|
import { assert } from 'chai'
|
||||||
|
import sinon from 'sinon'
|
||||||
|
|
||||||
|
// Local libraries
|
||||||
|
import IpfsApiController from '../../../../../src/controllers/rest-api/ipfs/controller.js'
|
||||||
|
import adapters from '../../../mocks/adapters/index.js'
|
||||||
|
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||||
|
|
||||||
|
import { context as mockContext } from '../../../mocks/ctx-mock.js'
|
||||||
|
let uut
|
||||||
|
let sandbox
|
||||||
|
let ctx
|
||||||
|
|
||||||
|
describe('#IPFS REST API', () => {
|
||||||
|
before(async () => {
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const useCases = new UseCasesMock()
|
||||||
|
|
||||||
|
uut = new IpfsApiController({ adapters, useCases })
|
||||||
|
|
||||||
|
sandbox = sinon.createSandbox()
|
||||||
|
|
||||||
|
// Mock the context object.
|
||||||
|
ctx = mockContext()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => sandbox.restore())
|
||||||
|
|
||||||
|
describe('#constructor', () => {
|
||||||
|
it('should throw an error if adapters are not passed in', () => {
|
||||||
|
try {
|
||||||
|
uut = new IpfsApiController()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(
|
||||||
|
err.message,
|
||||||
|
'Instance of Adapters library required when instantiating /ipfs REST Controller.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error if useCases are not passed in', () => {
|
||||||
|
try {
|
||||||
|
uut = new IpfsApiController({ adapters })
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(
|
||||||
|
err.message,
|
||||||
|
'Instance of Use Cases library required when instantiating /ipfs REST Controller.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#GET /status', () => {
|
||||||
|
it('should return 422 status on biz logic error', async () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getStatus').rejects(new Error('test error'))
|
||||||
|
|
||||||
|
await uut.getStatus(ctx)
|
||||||
|
|
||||||
|
assert.fail('Unexpected result')
|
||||||
|
} catch (err) {
|
||||||
|
assert.equal(err.status, 422)
|
||||||
|
assert.include(err.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return 200 status on success', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getStatus').resolves({ a: 'b' })
|
||||||
|
|
||||||
|
await uut.getStatus(ctx)
|
||||||
|
// console.log('ctx.body: ', ctx.body)
|
||||||
|
|
||||||
|
assert.property(ctx.body, 'status')
|
||||||
|
assert.equal(ctx.body.status.a, 'b')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#POST /peers', () => {
|
||||||
|
it('should return 422 status on biz logic error', async () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getPeers').rejects(new Error('test error'))
|
||||||
|
|
||||||
|
ctx.request.body = {
|
||||||
|
showAll: true
|
||||||
|
}
|
||||||
|
|
||||||
|
await uut.getPeers(ctx)
|
||||||
|
|
||||||
|
assert.fail('Unexpected result')
|
||||||
|
} catch (err) {
|
||||||
|
assert.equal(err.status, 422)
|
||||||
|
assert.include(err.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return 200 status on success', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves({ a: 'b' })
|
||||||
|
|
||||||
|
ctx.request.body = {
|
||||||
|
showAll: true
|
||||||
|
}
|
||||||
|
|
||||||
|
await uut.getPeers(ctx)
|
||||||
|
// console.log('ctx.body: ', ctx.body)
|
||||||
|
|
||||||
|
assert.property(ctx.body, 'peers')
|
||||||
|
assert.equal(ctx.body.peers.a, 'b')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#POST /relays', () => {
|
||||||
|
it('should return 422 status on biz logic error', async () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getRelays').rejects(new Error('test error'))
|
||||||
|
|
||||||
|
await uut.getRelays(ctx)
|
||||||
|
|
||||||
|
assert.fail('Unexpected result')
|
||||||
|
} catch (err) {
|
||||||
|
assert.equal(err.status, 422)
|
||||||
|
assert.include(err.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return 200 status on success', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
sandbox.stub(uut.adapters.ipfs, 'getRelays').resolves({ a: 'b' })
|
||||||
|
|
||||||
|
await uut.getRelays(ctx)
|
||||||
|
// console.log('ctx.body: ', ctx.body)
|
||||||
|
|
||||||
|
assert.property(ctx.body, 'relays')
|
||||||
|
assert.equal(ctx.body.relays.a, 'b')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#handleError', () => {
|
||||||
|
it('should still throw error if there is no message', () => {
|
||||||
|
try {
|
||||||
|
const err = {
|
||||||
|
status: 404
|
||||||
|
}
|
||||||
|
|
||||||
|
uut.handleError(ctx, err)
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(err.message, 'Not Found')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw error with message', () => {
|
||||||
|
try {
|
||||||
|
const err = {
|
||||||
|
status: 422,
|
||||||
|
message: 'test error'
|
||||||
|
}
|
||||||
|
|
||||||
|
uut.handleError(ctx, err)
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(err.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
Unit tests for the REST API handler for the /users endpoints.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Public npm libraries
|
||||||
|
import { assert } from 'chai'
|
||||||
|
import sinon from 'sinon'
|
||||||
|
|
||||||
|
// Local support libraries
|
||||||
|
import adapters from '../../../mocks/adapters/index.js'
|
||||||
|
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||||
|
import IpfsRouter from '../../../../../src/controllers/rest-api/ipfs/index.js'
|
||||||
|
// const app = require('../../../mocks/app-mock')
|
||||||
|
|
||||||
|
let uut
|
||||||
|
let sandbox
|
||||||
|
// let ctx
|
||||||
|
|
||||||
|
// const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||||
|
|
||||||
|
describe('#IPFS-REST-Router', () => {
|
||||||
|
// const testUser = {}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const useCases = new UseCasesMock()
|
||||||
|
uut = new IpfsRouter({ adapters, useCases })
|
||||||
|
|
||||||
|
sandbox = sinon.createSandbox()
|
||||||
|
|
||||||
|
// Mock the context object.
|
||||||
|
// ctx = mockContext()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => sandbox.restore())
|
||||||
|
|
||||||
|
describe('#constructor', () => {
|
||||||
|
it('should throw an error if adapters are not passed in', () => {
|
||||||
|
try {
|
||||||
|
uut = new IpfsRouter()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(
|
||||||
|
err.message,
|
||||||
|
'Instance of Adapters library required when instantiating IPFS REST Controller.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error if useCases are not passed in', () => {
|
||||||
|
try {
|
||||||
|
uut = new IpfsRouter({ adapters })
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(
|
||||||
|
err.message,
|
||||||
|
'Instance of Use Cases library required when instantiating IPFS REST Controller.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#attach', () => {
|
||||||
|
it('should throw an error if app is not passed in.', () => {
|
||||||
|
try {
|
||||||
|
uut.attach()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(
|
||||||
|
err.message,
|
||||||
|
'Must pass app object when attaching REST API controllers.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -26,7 +26,10 @@ class IpfsCoordAdapter {
|
|||||||
|
|
||||||
const ipfs = {
|
const ipfs = {
|
||||||
ipfsAdapter: new IpfsAdapter(),
|
ipfsAdapter: new IpfsAdapter(),
|
||||||
ipfsCoordAdapter: new IpfsCoordAdapter()
|
ipfsCoordAdapter: new IpfsCoordAdapter(),
|
||||||
|
getStatus: async () => {},
|
||||||
|
getPeers: async () => {},
|
||||||
|
getRelays: async () => {}
|
||||||
}
|
}
|
||||||
ipfs.ipfs = ipfs.ipfsAdapter.ipfs
|
ipfs.ipfs = ipfs.ipfsAdapter.ipfs
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user