mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
/*
|
|
Unit tests for the REST API handler for the /settle 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'
|
|
|
|
// const app = require('../../../mocks/app-mock')
|
|
|
|
import SettleRESTController from '../../../../../src/controllers/rest-api/settle/controller.js'
|
|
|
|
import { context as mockContext } from '../../../mocks/ctx-mock.js'
|
|
|
|
let uut
|
|
let sandbox
|
|
let ctx
|
|
|
|
describe('#Settle-REST-Controller', () => {
|
|
// const testUser = {}
|
|
|
|
beforeEach(() => {
|
|
const useCases = new UseCasesMock()
|
|
uut = new SettleRESTController({ 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 SettleRESTController()
|
|
|
|
assert.fail('Unexpected code path')
|
|
} catch (err) {
|
|
assert.include(
|
|
err.message,
|
|
'Instance of Adapters library required when instantiating /settle REST Controller.'
|
|
)
|
|
}
|
|
})
|
|
|
|
it('should throw an error if useCases are not passed in', () => {
|
|
try {
|
|
uut = new SettleRESTController({ adapters })
|
|
|
|
assert.fail('Unexpected code path')
|
|
} catch (err) {
|
|
assert.include(
|
|
err.message,
|
|
'Instance of Use Cases library required when instantifying /settle REST Controller.'
|
|
)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('#settle', () => {
|
|
it('should return the settle result', async () => {
|
|
// Mock dependencies
|
|
ctx.body = {}
|
|
const result = {
|
|
transaction: '1234567890'
|
|
}
|
|
sandbox.stub(uut.useCases.settle, 'settle').resolves(result)
|
|
|
|
await uut.settle(ctx)
|
|
|
|
assert.isObject(ctx.body)
|
|
assert.property(ctx.body, 'transaction')
|
|
})
|
|
|
|
it('should catch and throw useCases error', async () => {
|
|
try {
|
|
// Force an error
|
|
sandbox.stub(uut.useCases.settle, 'settle').throws(new Error('test error'))
|
|
|
|
await uut.settle(ctx)
|
|
} 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 throw error with message', () => {
|
|
try {
|
|
const err = {
|
|
status: 422,
|
|
message: 'test error'
|
|
}
|
|
|
|
uut.handleError(ctx, err)
|
|
} catch (err) {
|
|
assert.include(err.message, 'test error')
|
|
}
|
|
})
|
|
})
|
|
})
|