diff --git a/package.json b/package.json index 1305f9f..85e85e8 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "npm run lint && npm run test-v3", "lint": "standard --env mocha --fix", "test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/", - "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/blockchain.js", + "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/encryption.js", "test:integration": "mocha test/v3/integration", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v3/", diff --git a/src/routes/v3/encryption.js b/src/routes/v3/encryption.js index 3f81fb1..c393598 100644 --- a/src/routes/v3/encryption.js +++ b/src/routes/v3/encryption.js @@ -120,7 +120,8 @@ class Encryption { const txHistory = balance.txids // console.log(`txHistory: ${JSON.stringify(txHistory, null, 2)}`) - if (txHistory.length === 0) { + // throw error if there is no transaction history. + if (!txHistory || txHistory.length === 0) { throw new Error('No transaction history.') } diff --git a/test/v3/encryption.js b/test/v3/encryption.js index 0a0c76c..578ceaa 100644 --- a/test/v3/encryption.js +++ b/test/v3/encryption.js @@ -77,10 +77,6 @@ describe('#Encryption Router', () => { // Mock the Insight URL for unit tests. if (process.env.TEST === 'unit') { - // sandbox.stub(blockbookRoute.axios, 'request').resolves({ - // data: mockData.mockBalance - // }) - sandbox .stub(encryptionRoute.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) @@ -96,7 +92,55 @@ describe('#Encryption Router', () => { assert.equal(result.success, true) assert.property(result, 'publicKey') - assert.equal(result.publicKey, '044eb40b025df18409f2a5197b010dd62a9e65d9a74e415e5b10367721a9c4baa7ebfee22d14b8ece1c9bd70c0d9e5e8b00b61b81b88a1b5ce6f24eac6b8a34b2c') + assert.equal( + result.publicKey, + '044eb40b025df18409f2a5197b010dd62a9e65d9a74e415e5b10367721a9c4baa7ebfee22d14b8ece1c9bd70c0d9e5e8b00b61b81b88a1b5ce6f24eac6b8a34b2c' + ) + }) + + it('should return false for address with no tx history', async () => { + req.params.address = + 'bitcoincash:qqwfpk04ecf69wuprj9yjys9rla5mk7rj5j8uthqel' + + // Mock the Insight URL for unit tests. + if (process.env.TEST === 'unit') { + sandbox + .stub(encryptionRoute.blockbook, 'balanceFromBlockbook') + .resolves(mockData.mockNoTxHistory) + } + + const result = await encryptionRoute.getPublicKey(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, 'success') + assert.equal(result.success, false) + + assert.property(result, 'error') + assert.include(result.error, 'No transaction history') + }) + + it('should return false for address with no send history', async () => { + req.params.address = + 'bitcoincash:qq78nwj5x97yh6wtlfd27dtlwjuh70vkjc59h8tgtg' + + // Mock the Insight URL for unit tests. + if (process.env.TEST === 'unit') { + sandbox + .stub(encryptionRoute.blockbook, 'balanceFromBlockbook') + .resolves(mockData.mockNoSendBalance) + sandbox + .stub(encryptionRoute.rawTransactions, 'getRawTransactionsFromNode') + .resolves(mockData.mockNoSendTx) + } + + const result = await encryptionRoute.getPublicKey(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, 'success') + assert.equal(result.success, false) + + assert.property(result, 'publicKey') + assert.include(result.publicKey, 'not found') }) }) }) diff --git a/test/v3/mocks/encryption-mocks.js b/test/v3/mocks/encryption-mocks.js index f1de453..79100a0 100644 --- a/test/v3/mocks/encryption-mocks.js +++ b/test/v3/mocks/encryption-mocks.js @@ -93,7 +93,87 @@ const mockTxDetails = { blocktime: 1545564654 } +const mockNoTxHistory = { + page: 1, + totalPages: 1, + itemsOnPage: 1000, + address: 'bitcoincash:qqwfpk04ecf69wuprj9yjys9rla5mk7rj5j8uthqel', + balance: '0', + totalReceived: '0', + totalSent: '0', + unconfirmedBalance: '0', + unconfirmedTxs: 0, + txs: 0 +} + +const mockNoSendBalance = { + page: 1, + totalPages: 1, + itemsOnPage: 1000, + address: 'bitcoincash:qq78nwj5x97yh6wtlfd27dtlwjuh70vkjc59h8tgtg', + balance: '0', + totalReceived: '0', + totalSent: '0', + unconfirmedBalance: '600', + unconfirmedTxs: 1, + txs: 0, + txids: [ + 'a3b62cd4f4c56ba52139179db14bffd4ab22a2e077f3c62bd5cf0541bfcaf023' + ] +} + +const mockNoSendTx = { + txid: 'a3b62cd4f4c56ba52139179db14bffd4ab22a2e077f3c62bd5cf0541bfcaf023', + hash: 'a3b62cd4f4c56ba52139179db14bffd4ab22a2e077f3c62bd5cf0541bfcaf023', + version: 2, + size: 226, + locktime: 0, + vin: [ + { + txid: '681cc1d392975a7e78cbd490b8a940d7eb029c554f2cee332e3a7119d77fb189', + vout: 1, + scriptSig: { + asm: '30450221008a7c86ce3a9f5765573440143a00631bb5afa9a58b24674a7fa794bb11ffffc30220397dd4c672c3e0dbe0c4eb6390b5727c5fdc3a42cf3ae275ac13b44d27bb234f[ALL|FORKID] 02bc9fc26ae2bd9a9a14e9baeda4b4fdd95a00bcbf375732c647488b3ce82a2fd7', + hex: '4830450221008a7c86ce3a9f5765573440143a00631bb5afa9a58b24674a7fa794bb11ffffc30220397dd4c672c3e0dbe0c4eb6390b5727c5fdc3a42cf3ae275ac13b44d27bb234f412102bc9fc26ae2bd9a9a14e9baeda4b4fdd95a00bcbf375732c647488b3ce82a2fd7' + }, + sequence: 4294967295 + } + ], + vout: [ + { + value: 0.000006, + n: 0, + scriptPubKey: { + asm: 'OP_DUP OP_HASH160 3c79ba54317c4be9cbfa5aaf357f74b97f3d9696 OP_EQUALVERIFY OP_CHECKSIG', + hex: '76a9143c79ba54317c4be9cbfa5aaf357f74b97f3d969688ac', + reqSigs: 1, + type: 'pubkeyhash', + addresses: [ + 'bitcoincash:qq78nwj5x97yh6wtlfd27dtlwjuh70vkjc59h8tgtg' + ] + } + }, + { + value: 0.00003664, + n: 1, + scriptPubKey: { + asm: 'OP_DUP OP_HASH160 58502a73888e0a386fc69b44850af4b2a86ac9a6 OP_EQUALVERIFY OP_CHECKSIG', + hex: '76a91458502a73888e0a386fc69b44850af4b2a86ac9a688ac', + reqSigs: 1, + type: 'pubkeyhash', + addresses: [ + 'bitcoincash:qpv9q2nn3z8q5wr0c6d5fpg27je2s6kf5cavp4ney0' + ] + } + } + ], + hex: '020000000189b17fd719713a2e33ee2c4f559c02ebd740a9b890d4cb787e5a9792d3c11c68010000006b4830450221008a7c86ce3a9f5765573440143a00631bb5afa9a58b24674a7fa794bb11ffffc30220397dd4c672c3e0dbe0c4eb6390b5727c5fdc3a42cf3ae275ac13b44d27bb234f412102bc9fc26ae2bd9a9a14e9baeda4b4fdd95a00bcbf375732c647488b3ce82a2fd7ffffffff0258020000000000001976a9143c79ba54317c4be9cbfa5aaf357f74b97f3d969688ac500e0000000000001976a91458502a73888e0a386fc69b44850af4b2a86ac9a688ac00000000' +} + module.exports = { mockBalance, - mockTxDetails + mockTxDetails, + mockNoTxHistory, + mockNoSendBalance, + mockNoSendTx }