From 72ef95abe6652213598e2d277d71f1675575eee8 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 3 May 2020 14:12:12 -0700 Subject: [PATCH] Created Encryption route and tests --- src/routes/v3/encryption.js | 65 +++++++++++++++++++++++++++++++++ test/v3/encryption.js | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/routes/v3/encryption.js create mode 100644 test/v3/encryption.js diff --git a/src/routes/v3/encryption.js b/src/routes/v3/encryption.js new file mode 100644 index 0000000..73da0de --- /dev/null +++ b/src/routes/v3/encryption.js @@ -0,0 +1,65 @@ +/* + Encryption API route +*/ + +'use strict' + +const express = require('express') +const router = express.Router() +const axios = require('axios') +const util = require('util') + +const wlogger = require('../../util/winston-logging') +const config = require('../../../config') + +const RouteUtils = require('../../util/route-utils') +const routeUtils = new RouteUtils() + +const BCHJS = require('@chris.troutner/bch-js') +const bchjs = new BCHJS() + +let _this + +class Encryption { + constructor () { + _this = this + + _this.config = config + _this.axios = axios + _this.routeUtils = routeUtils + _this.bchjs = bchjs + + _this.router = router + _this.router.get('/', _this.root) + // _this.router.get('/publickey', _this.getPublicKey) + } + + // DRY error handler. + errorHandler (err, res) { + // Attempt to decode the error message. + const { msg, status } = _this.routeUtils.decodeError(err) + if (msg) { + res.status(status) + return res.json({ error: msg }) + } + + // Handle error patterns specific to this route. + if (err.message) { + res.status(400) + return res.json({ success: false, error: err.message }) + } + + wlogger.error('Unhandled error in Encryption library: ', err) + + // If error can be handled, return the stack trace + res.status(500) + return res.json({ error: util.inspect(err) }) + } + + // Root API endpoint. Simply acknowledges that it exists. + root (req, res, next) { + return res.json({ status: 'encryption' }) + } +} + +module.exports = Encryption diff --git a/test/v3/encryption.js b/test/v3/encryption.js new file mode 100644 index 0000000..5424bd5 --- /dev/null +++ b/test/v3/encryption.js @@ -0,0 +1,72 @@ +/* + TESTS FOR THE ENCRYPTION.JS LIBRARY + + This test file uses the environment variable TEST to switch between unit + and integration tests. By default, TEST is set to 'unit'. Set this variable + to 'integration' to run the tests against BCH mainnet. + + To-Do: +*/ + +'use strict' + +const chai = require('chai') +const assert = chai.assert + +const sinon = require('sinon') + +// Set default environment variables for unit tests. +if (!process.env.TEST) process.env.TEST = 'unit' + +// Only load blockbook library after setting BLOCKBOOK_URL env var. +const EncryptionRoute = require('../../src/routes/v3/encryption') +const encryptionRoute = new EncryptionRoute() + +// Mocking data. +const { mockReq, mockRes } = require('./mocks/express-mocks') +// const mockData = require('./mocks/blockbook-mock') + +// Used for debugging. +const util = require('util') +util.inspect.defaultOptions = { depth: 1 } + +describe('#Encryption Router', () => { + let req, res + let sandbox + before(() => { + // console.log(`Testing type is: ${process.env.TEST}`) + + if (!process.env.NETWORK) process.env.NETWORK = 'testnet' + }) + + // Setup the mocks before each test. + beforeEach(() => { + // Mock the req and res objects used by Express routes. + req = mockReq + res = mockRes + + // Explicitly reset the parmas and body. + req.params = {} + req.body = {} + req.query = {} + + sandbox = sinon.createSandbox() + }) + + afterEach(() => { + sandbox.restore() + }) + + after(() => {}) + + describe('#root', () => { + // root route handler. + const root = encryptionRoute.root + + it('should respond to GET for base route', async () => { + const result = root(req, res) + + assert.equal(result.status, 'encryption', 'Returns static string') + }) + }) +})