Files
x402-base-facilitator/test/unit/use-cases/verify.js
T

78 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
Unit tests for the src/use-cases/verify.js business logic library.
*/
// Public npm libraries
import { assert } from 'chai'
import sinon from 'sinon'
import VerifyUseCases from '../../../src/use-cases/verify.js'
import adapters from '../mocks/adapters/index.js'
describe('#verify-use-case', () => {
let uut
let sandbox
before(async () => {
})
beforeEach(() => {
sandbox = sinon.createSandbox()
uut = new VerifyUseCases({ adapters })
console.log('SupportedLib adapters: ', adapters.facilitatorMock)
})
afterEach(() => sandbox.restore())
describe('#constructor', () => {
it('should throw an error if adapters are not passed in', () => {
try {
uut = new VerifyUseCases()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of adapters must be passed in when instantiating Verify Use Cases library.'
)
}
})
})
describe('#verify', () => {
it('should return the supported payment kinds and extensions', async () => {
const result = await uut.verify({ paymentPayload: {}, paymentRequirements: {} })
assert.isObject(result)
})
it('should throw an error if paymentPayload are not provided', async () => {
try {
await uut.verify({})
assert.fail('Unexpected code path')
} catch (error) {
assert.include(error.message, 'Missing paymentPayload or paymentRequirements')
}
})
it('should throw an error if paymentRequirements are not provided', async () => {
try {
await uut.verify({ paymentPayload: {} })
assert.fail('Unexpected code path')
} catch (error) {
assert.include(error.message, 'Missing paymentPayload or paymentRequirements')
}
})
it('should handle facilitator errors', async () => {
try {
sandbox.stub(uut.facilitator, 'verify').throws(new Error('test error'))
await uut.verify({ paymentPayload: {}, paymentRequirements: {} })
assert.fail('Unexpected code path')
} catch (error) {
assert.include(error.message, 'test error')
}
})
})
})