From ff1c37ddb692ce29a903a36b6b9fe7b19e2a81d5 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sat, 5 Dec 2020 08:46:22 -0800 Subject: [PATCH] fix(validateSingle): Added unit tests for /slp/validateSingle --- package.json | 3 +- src/routes/v3/slp.js | 1 + test/v3/slp.js | 223 +++++++++++++++++++++++++++++-------------- 3 files changed, 152 insertions(+), 75 deletions(-) diff --git a/package.json b/package.json index 92095fb..d2e68f3 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "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 && export TEST=integration && mocha --timeout 25000 test/v3/integration/price.js", + "test:temp:integration": "export NETWORK=mainnet && export TEST=integration && mocha --timeout 25000 test/v3/integration/price.js", + "test:temp:unit": "export NETWORK=mainnet && mocha -g '#validateSingle' --exit test/v3/", "test:integration": "mocha test/v3/integration", "test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v3/integration/slp*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", diff --git a/src/routes/v3/slp.js b/src/routes/v3/slp.js index 8e4301e..5778167 100644 --- a/src/routes/v3/slp.js +++ b/src/routes/v3/slp.js @@ -1110,6 +1110,7 @@ class Slp { } // Get data from SLPDB. const tokenRes = await _this.axios.request(opt) + // console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`) // Default return value. let result = { diff --git a/test/v3/slp.js b/test/v3/slp.js index e847904..cd685f9 100644 --- a/test/v3/slp.js +++ b/test/v3/slp.js @@ -338,6 +338,155 @@ describe('#SLP', () => { } }) + describe('#validate2Single', () => { + it('should throw 400 if txid is empty', async () => { + req.params.txid = '' + const result = await slpRoute.validate2Single(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ['error']) + assert.include(result.error, 'txid can not be empty') + }) + + it('should invalidate a known invalid TXID', async () => { + if (process.env.TEST === 'unit') { + // Mock to prevent live network connection. + sandbox + .stub(slpRoute.axios, 'request') + .resolves({ data: { isValid: false } }) + } + + const txid = + 'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a' + + req.params.txid = txid + const result = await slpRoute.validate2Single(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.equal(result.txid, txid) + assert.equal(result.isValid, false) + }) + + it('should validate a known valid TXID', async () => { + if (process.env.TEST === 'unit') { + // Mock to prevent live network connection. + sandbox + .stub(slpRoute.axios, 'request') + .resolves({ data: { isValid: true } }) + } + + const txid = + '3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488' + + req.params.txid = txid + const result = await slpRoute.validate2Single(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.equal(result.txid, txid) + assert.equal(result.isValid, true) + }) + + // This test can only be run as a mocked unit test. It's too inconsistent + // to run as an integration test, due to the caching built into slp-validate. + if (process.env.TEST === 'unit') { + it('should cancel if validation takes too long', async () => { + // Mock the timeout error. + sandbox.stub(slpRoute.axios, 'request').throws({ + code: 'ECONNABORTED' + }) + + const txid = + 'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579' + + req.params.txid = txid + const result = await slpRoute.validate2Single(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') + assert.include( + result.error, + 'Could not communicate with full node', + 'Error message expected' + ) + }) + } + }) + + describe('#validateSingle', () => { + it('should throw 400 if txid is empty', async () => { + req.params.txid = '' + const result = await slpRoute.validateSingle(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ['error']) + assert.include(result.error, 'txid can not be empty') + }) + + it('should invalidate a known invalid TXID', async () => { + if (process.env.TEST === 'unit') { + // Mock to prevent live network connection. + sandbox.stub(slpRoute.axios, 'request').resolves({ + data: { + c: [], + u: [] + } + }) + } + + const txid = + 'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a' + + req.params.txid = txid + const result = await slpRoute.validateSingle(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.equal(result.txid, txid) + assert.equal(result.valid, false) + }) + + it('should validate a known valid TXID', async () => { + if (process.env.TEST === 'unit') { + // Mock to prevent live network connection. + sandbox + .stub(slpRoute.axios, 'request') + .resolves({ data: mockData.mockSingleValidTxid }) + } + + const txid = + '77872738b6bddee6c0cbdb9509603de20b15d4f6b26602f629417aec2f5d5e8d' + + req.params.txid = txid + const result = await slpRoute.validateSingle(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.equal(result.txid, txid) + assert.equal(result.valid, true) + }) + + if (process.env.TEST === 'unit') { + it('should cancel if validation takes too long', async () => { + // Mock the timeout error. + sandbox.stub(slpRoute.axios, 'request').throws({ + code: 'ECONNABORTED' + }) + + const txid = + 'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579' + + req.params.txid = txid + const result = await slpRoute.validateSingle(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') + assert.include( + result.error, + 'Could not communicate with full node', + 'Error message expected' + ) + }) + } + }) + describe('validateBulk()', () => { const validateBulk = slpRoute.validateBulk @@ -463,80 +612,6 @@ describe('#SLP', () => { }) }) - describe('#validate2Single', () => { - it('should throw 400 if txid is empty', async () => { - req.params.txid = '' - const result = await slpRoute.validate2Single(req, res) - // console.log(`result: ${util.inspect(result)}`) - - assert.hasAllKeys(result, ['error']) - assert.include(result.error, 'txid can not be empty') - }) - - it('should invalidate a known invalid TXID', async () => { - if (process.env.TEST === 'unit') { - // Mock to prevent live network connection. - sandbox - .stub(slpRoute.axios, 'request') - .resolves({ data: { isValid: false } }) - } - - const txid = - 'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a' - - req.params.txid = txid - const result = await slpRoute.validate2Single(req, res) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.equal(result.txid, txid) - assert.equal(result.isValid, false) - }) - - it('should validate a known valid TXID', async () => { - if (process.env.TEST === 'unit') { - // Mock to prevent live network connection. - sandbox - .stub(slpRoute.axios, 'request') - .resolves({ data: { isValid: true } }) - } - - const txid = - '3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488' - - req.params.txid = txid - const result = await slpRoute.validate2Single(req, res) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.equal(result.txid, txid) - assert.equal(result.isValid, true) - }) - - // This test can only be run as a mocked unit test. It's too inconsistent - // to run as an integration test, due to the caching built into slp-validate. - if (process.env.TEST === 'unit') { - it('should cancel if validation takes too long', async () => { - // Mock the timeout error. - sandbox.stub(slpRoute.axios, 'request').throws({ - code: 'ECONNABORTED' - }) - - const txid = - 'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579' - - req.params.txid = txid - const result = await slpRoute.validate2Single(req, res) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') - assert.include( - result.error, - 'Could not communicate with full node', - 'Error message expected' - ) - }) - } - }) - describe('tokenStats()', () => { it('should throw 400 if tokenID is empty', async () => { req.params.tokenId = ''