Files

70 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

import chai from 'chai'
import sinon from 'sinon'
2020-07-27 21:32:51 -07:00
import BCHJS from '../../src/bch-js.js'
import mockData from './fixtures/encryption-mock.js'
const { assert } = chai
2020-07-27 21:32:51 -07:00
// const bchjs = new BCHJS()
let bchjs
2021-01-02 15:41:12 -08:00
describe('#Encryption', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => {
bchjs = new BCHJS()
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
describe('#getPubKey', () => {
it('should throw error if BCH address is not provided.', async () => {
2020-07-27 21:32:51 -07:00
try {
await bchjs.encryption.getPubKey()
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-07-27 21:32:51 -07:00
} catch (err) {
// console.log(`err: `, err)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'Input must be a valid Bitcoin Cash address'
2020-07-27 21:32:51 -07:00
)
}
})
2021-01-02 15:41:12 -08:00
it('should report when public key can not be found', async () => {
2020-07-27 21:32:51 -07:00
// Stub the network call.
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.encryption.axios, 'get')
2020-07-27 21:32:51 -07:00
.resolves({ data: mockData.failureMock })
2021-01-02 15:41:12 -08:00
const addr = 'bitcoincash:qpxqr2pmcverj4vukgjqssvk2zju8tp9xsgz2nqagx'
2020-07-27 21:32:51 -07:00
const result = await bchjs.encryption.getPubKey(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, false)
2021-01-02 15:41:12 -08:00
assert.property(result, 'publicKey')
assert.equal(result.publicKey, 'not found')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should get a public key', async () => {
2020-07-27 21:32:51 -07:00
// Stub the network call.
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.encryption.axios, 'get')
2020-07-27 21:32:51 -07:00
.resolves({ data: mockData.successMock })
2021-01-02 15:41:12 -08:00
const addr = 'bitcoincash:qpf8jv9hmqcda0502gjp7nm3g24y5h5s4unutghsxq'
2020-07-27 21:32:51 -07:00
const result = await bchjs.encryption.getPubKey(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'publicKey')
2020-07-27 21:32:51 -07:00
})
})
})