mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 01:02:00 -07:00
142 lines
3.7 KiB
JavaScript
142 lines
3.7 KiB
JavaScript
/*
|
|
Unit tests for the REST API handler for the /order 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 OrderRESTController = require('../../../../../src/controllers/rest-api/order/controller')
|
|
let uut
|
|
let sandbox
|
|
let ctx
|
|
|
|
const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
|
|
|
describe('#Order-REST-Router', () => {
|
|
// const testUser = {}
|
|
|
|
beforeEach(() => {
|
|
const useCases = new UseCasesMock()
|
|
uut = new OrderRESTController({ 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 OrderRESTController()
|
|
|
|
assert.fail('Unexpected code path')
|
|
} catch (err) {
|
|
assert.include(
|
|
err.message,
|
|
'Instance of Adapters library required when instantiating /order REST Controller.'
|
|
)
|
|
}
|
|
})
|
|
|
|
it('should throw an error if useCases are not passed in', () => {
|
|
try {
|
|
uut = new OrderRESTController({ adapters })
|
|
|
|
assert.fail('Unexpected code path')
|
|
} catch (err) {
|
|
assert.include(
|
|
err.message,
|
|
'Instance of Use Cases library required when instantiating /order REST Controller.'
|
|
)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('#createOrder', () => {
|
|
it('should create a new order', async () => {
|
|
ctx.request.body = {
|
|
appId: 'swapTest555',
|
|
data: {
|
|
messageType: 1,
|
|
messageClass: 1,
|
|
tokenId:
|
|
'38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0',
|
|
buyOrSell: 'sell',
|
|
rateInSats: 1000,
|
|
minSatsToExchange: 10,
|
|
numTokens: 0.02,
|
|
utxoTxid:
|
|
'241c06bf61384b8623477e419bf4779edbcc7e3bc862f0f179a9ed2967069b87',
|
|
utxoVout: 0
|
|
},
|
|
timestamp: '2021-09-20T17:54:26.395Z',
|
|
localTimeStamp: '9/20/2021, 10:54:26 AM',
|
|
txid: '46f50f2a0cf44e3ed70dfb0618ef3ebfee57aabcf229b5d2d17c07322b54a8d7',
|
|
hash: 'zdpuB2X25AZCKo3wpr4sSbw44vqPWJRqcxWQRHZccK5BdtoGD'
|
|
}
|
|
|
|
// Mock dependencies
|
|
// sandbox.stub(uut.useCases.order, 'createOrder').resolves()
|
|
|
|
await uut.createOrder(ctx)
|
|
|
|
// assert.equal(ctx.body.hash, 'testHash')
|
|
})
|
|
|
|
// it('should catch and throw an error', async () => {
|
|
// try {
|
|
// ctx.request.body = {
|
|
// order: {}
|
|
// }
|
|
//
|
|
// // Force an error
|
|
// sandbox
|
|
// .stub(uut.useCases.order, 'createOrder')
|
|
// .rejects(new Error('test error'))
|
|
//
|
|
// await uut.createOrder(ctx)
|
|
//
|
|
// assert.fail('Unexpected code path')
|
|
// } catch (err) {
|
|
// // console.log('err: ', err)
|
|
// assert.include(err.message, 'test error')
|
|
// }
|
|
// })
|
|
})
|
|
|
|
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 catch error if message is provided', () => {
|
|
try {
|
|
const err = {
|
|
status: 422,
|
|
message: 'Unprocessable Entity'
|
|
}
|
|
|
|
uut.handleError(ctx, err)
|
|
} catch (err) {
|
|
assert.include(err.message, 'Unprocessable Entity')
|
|
}
|
|
})
|
|
})
|
|
})
|