From 551080f5f166157105a8cf1b17bc9898321dc93e Mon Sep 17 00:00:00 2001 From: Craig Date: Sat, 18 Mar 2017 13:32:22 +1300 Subject: [PATCH] AddressGenerator @ 100% coverage --- src/lib/AddressGenerator.js | 8 +- test/AddressGenerator.spec.js | 133 ++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/src/lib/AddressGenerator.js b/src/lib/AddressGenerator.js index cdc8481..603ca0e 100644 --- a/src/lib/AddressGenerator.js +++ b/src/lib/AddressGenerator.js @@ -60,8 +60,8 @@ AddressGenerator.getAccountAddressesForGeneration = (options, callback) => { if (err.code === -12) { AddressGenerator.runKeypoolRefill(options, callback) } else { - console.log('ERROR: client.getAddressesByAccount', err) - callback(false) + Logger.writeLog('ADG_004', 'client.getAddressesByAccount', { err }) + callback(false, err) return } }) @@ -84,8 +84,8 @@ AddressGenerator.generateNewAccountAddresses = (options, callback) => { if (err.code === -12) { AddressGenerator.runKeypoolRefill(options, callback) } else { - console.log('ERROR: client.getNewAddress', err) - callback(false) + Logger.writeLog('ADG_005', 'client.getNewAddress', { err }) + callback(false, err) return } }) diff --git a/test/AddressGenerator.spec.js b/test/AddressGenerator.spec.js index e398d7d..6b196bb 100644 --- a/test/AddressGenerator.spec.js +++ b/test/AddressGenerator.spec.js @@ -150,5 +150,138 @@ describe('[AddressGenerator]', () => { maxAddresses: 100, }, callback) }) + it('fail client.getAddressesByAccount catch all error', (done) => { + const mockClient = { + getAddressesByAccount: () => { return Promise.reject({ code: -17 }) }, + } + + const callback = (success, err) => { + expect(success).toBe(false) + expect(err.code).toBe(-17) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + }, callback) + }) + it('should return true because the max addresses already generated', (done) => { + const addresses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] + + const mockClient = { + getAddressesByAccount: () => { return Promise.resolve(addresses) }, + } + + const callback = (success) => { + expect(success).toBe(true) + done() + } + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + }, callback) + }) + it('should call the generate function because more addresses are required ', (done) => { + const addresses = [1, 2, 3, 4, 5] + + const mockClient = { + getAddressesByAccount: () => { return Promise.resolve(addresses) }, + } + + const callback = () => {} + + AddressGenerator.generateNewAccountAddresses = (options, parsedCallback) => { + expect(parsedCallback).toBe(callback) + expect(options.numToGenerate).toBe(5) + expect(options.client).toBe(mockClient) + expect(options.maxAddresses).toBe(10) + expect(options.accountName).toBe('incomingAccount') + done() + } + + AddressGenerator.getAccountAddressesForGeneration({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + }, callback) + }) + }) + describe('(generateNewAccountAddresses)', () => { + before(() => { // reset the rewired functions + AddressGenerator = rewire('../src/lib/AddressGenerator') + }) + it('should fail client.getNewAddress with error 12', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.reject({ code: -12 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = () => {} + + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + numToGenerate: 100, + }, callback) + }) + it('should fail client.getNewAddress catch all error', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.reject({ code: -17 }) }, + } + + AddressGenerator.runKeypoolRefill = () => { + expect(true).toBe(true) + done() + } + + const callback = (success, err) => { + expect(success).toBe(false) + expect(err.code).toBe(-17) + sinon.assert.calledOnce(mockLogger.writeLog) + done() + } + const mockLogger = { + writeLog: sinon.spy(), + } + AddressGenerator.__set__('Logger', mockLogger) + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 100, + numToGenerate: 100, + }, callback) + }) + it('should generate the address and call recurse until numToGenerate is 0', (done) => { + const mockClient = { + getNewAddress: () => { return Promise.resolve('123412341234') }, + } + + const callback = (success) => { + expect(success).toBe(true) + done() + } + + AddressGenerator.generateNewAccountAddresses({ + accountName: 'incomingAccount', + client: mockClient, + maxAddresses: 10, + numToGenerate: 10, + }, callback) + }) }) })