Adding unit tests for Fulcrum REST transaction endpoint

This commit is contained in:
Chris Troutner
2021-07-14 14:55:40 -07:00
parent 32af7a7233
commit 322a10cd74
4 changed files with 198 additions and 10 deletions
+19 -8
View File
@@ -6,10 +6,25 @@ const BCHJS = require('@psf/bch-js')
let _this
class FulcrumController {
constructor () {
class FulcrumRESTController {
constructor (localConfig = {}) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating Fulcrum REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating Fulcrum REST Controller.'
)
}
this.bchjs = new BCHJS()
_this = this
_this.bchjs = new BCHJS()
}
/**
@@ -21,10 +36,6 @@ class FulcrumController {
* @apiExample Example usage:
* curl -H "Content-Type: application/json" -X POST -d '{ "addresses": ["bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj"] }' localhost:5001/fulcrum/transactions
*
* @apiParam {Object} obj object (required)
* @apiParam {String} obj.email Sender Email.
* @apiParam {String} obj.formMessage Message.
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
@@ -70,4 +81,4 @@ class FulcrumController {
}
}
}
module.exports = FulcrumController
module.exports = FulcrumRESTController
+2 -2
View File
@@ -6,7 +6,7 @@
const Router = require('koa-router')
// Local libraries.
const FulcrumRESTControllerLib = require('./controller')
const FulcrumRESTController = require('./controller')
class FulcrumRouter {
constructor (localConfig = {}) {
@@ -30,7 +30,7 @@ class FulcrumRouter {
}
// Encapsulate dependencies.
this.fulcrumRESTController = new FulcrumRESTControllerLib(dependencies)
this.fulcrumRESTController = new FulcrumRESTController(dependencies)
// Instantiate the router and set the base route.
const baseUrl = '/fulcrum'
@@ -0,0 +1,99 @@
/*
Unit tests for the REST API handler for the /users endpoints.
*/
// Public npm libraries
const assert = require('chai').assert
const sinon = require('sinon')
// Local support libraries
const adapters = require('../../../mocks/adapters')
const UseCasesMock = require('../../../mocks/use-cases')
// const app = require('../../../mocks/app-mock')
const FulcrumRESTController = require('../../../../../src/controllers/rest-api/fulcrum/controller')
let uut
let sandbox
let ctx
const mockContext = require('../../../../unit/mocks/ctx-mock').context
describe('#Fulcrum-REST-Router', () => {
// const testUser = {}
beforeEach(() => {
const useCases = new UseCasesMock()
uut = new FulcrumRESTController({ 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 FulcrumRESTController()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Adapters library required when instantiating Fulcrum REST Controller.'
)
}
})
it('should throw an error if useCases are not passed in', () => {
try {
uut = new FulcrumRESTController({ adapters })
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Use Cases library required when instantiating Fulcrum REST Controller.'
)
}
})
})
describe('#transactions', () => {
it('should return data from bchjs', async () => {
// Mock dependencies
sandbox
.stub(uut.bchjs.Electrumx, 'transactions')
.resolves({ success: true })
ctx.request.body = {
addresses: 'testAddr'
}
await uut.transactions(ctx)
// console.log('ctx.body: ', ctx.body)
assert.equal(ctx.body.success, true)
})
it('should catch and throw an error', async () => {
try {
// Force an error
sandbox
.stub(uut.bchjs.Electrumx, 'transactions')
.rejects(new Error('test error'))
ctx.request.body = {
addresses: 'testAddr'
}
await uut.transactions(ctx)
} catch (err) {
// console.log('err: ', err)
assert.include(err.message, 'test error')
}
})
})
})
@@ -0,0 +1,78 @@
/*
Unit tests for the REST API handler for the /fulcrum endpoints.
*/
// Public npm libraries
const assert = require('chai').assert
const sinon = require('sinon')
// Local support libraries
const adapters = require('../../../mocks/adapters')
const UseCasesMock = require('../../../mocks/use-cases')
// const app = require('../../../mocks/app-mock')
const FulcrumRouter = require('../../../../../src/controllers/rest-api/fulcrum')
let uut
let sandbox
// let ctx
// const mockContext = require('../../../../unit/mocks/ctx-mock').context
describe('#Fulcrum-REST-Router', () => {
// const testUser = {}
beforeEach(() => {
const useCases = new UseCasesMock()
uut = new FulcrumRouter({ 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 FulcrumRouter()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Adapters library required when instantiating Fulcrum REST Controller.'
)
}
})
it('should throw an error if useCases are not passed in', () => {
try {
uut = new FulcrumRouter({ adapters })
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Use Cases library required when instantiating Fulcrum 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.'
)
}
})
})
})