diff --git a/package-lock.json b/package-lock.json index 9530013..1d1f8bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5592,6 +5592,12 @@ "integrity": "sha1-+CbJtOKoUR2E46yinbBeGk87cqk=", "dev": true }, + "lodash.clonedeep": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", + "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=", + "dev": true + }, "lodash.defaults": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", diff --git a/package.json b/package.json index cd1e17c..dda247b 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "eslint-plugin-prettier": "^3.1.0", "eslint-plugin-standard": "^4.0.0", "fs-extra": "^9.0.0", + "lodash.clonedeep": "^4.5.0", "nock": "^13.0.5", "nyc": "^15.0.0", "prettier": "^2.0.0", diff --git a/src/middleware/route-ratelimit2.js b/src/middleware/route-ratelimit2.js index e484265..9624b90 100644 --- a/src/middleware/route-ratelimit2.js +++ b/src/middleware/route-ratelimit2.js @@ -63,8 +63,8 @@ class RateLimits { // support this function. async applyRateLimits (req, res, next) { try { - let userId - const decoded = {} + // let userId + // const decoded = {} // Create a re*Q*.locals object if not passed in. // req.locals.proLimit will be true if the user is using Basic Authentication. @@ -170,7 +170,7 @@ class RateLimits { return isInternal } catch (err) { - console.error( + wlogger.error( 'Error in checkInternalIp(). Returning false be default. Err: ', err ) diff --git a/test/v4/rate-limit2-unit.js b/test/v4/rate-limit2-unit.js new file mode 100644 index 0000000..2bb7c99 --- /dev/null +++ b/test/v4/rate-limit2-unit.js @@ -0,0 +1,93 @@ +/* + Unit tests for the route-ratelimit2.js middleware. +*/ + +'use strict' + +// Public npm libraries. +const assert = require('chai').assert +const sinon = require('sinon') +const cloneDeep = require('lodash.clonedeep') + +// Mocking data. +const { mockReq, mockRes, mockNext } = require('./mocks/express-mocks') + +// Libraries under test +const RateLimits = require('../../src/middleware/route-ratelimit2') +let uut = new RateLimits() + +let req, res, next + +describe('#rate-routelimit2', () => { + let sandbox + + before(async () => { + if (!process.env.JWT_AUTH_SERVER) { + process.env.JWT_AUTH_SERVER = 'http://fakeurl.com/' + } + + // Wipe the Redis DB, which prevents false negatives when running integration + // tests back-to-back. + await uut.wipeRedis() + }) + + // Setup the mocks before each test. + beforeEach(() => { + // Mock the req and res objects used by Express routes. + req = cloneDeep(mockReq) + res = cloneDeep(mockRes) + next = mockNext + + // Explicitly reset the parmas and body. + req.params = {} + req.body = {} + req.query = {} + req.locals = {} + + sandbox = sinon.createSandbox() + + uut = new RateLimits() + }) + + afterEach(() => { + sandbox.restore() + }) + + after(() => { + uut.closeRedis() + }) + + describe('#checkInternalIp', () => { + it('should return true for a request from localhost', () => { + req.ip = '127.0.0.1' + + const result = uut.checkInternalIp(req) + + assert.equal(result, true) + }) + + it('should return true for a request from a Docker container', () => { + req.ip = '172.17.0.3' + + const result = uut.checkInternalIp(req) + + assert.equal(result, true) + }) + + it('should return false for a random ip address', () => { + req.ip = '123.456.7.8' + + const result = uut.checkInternalIp(req) + + assert.equal(result, false) + }) + + it('should return false when an error is encountered', () => { + req.ip = 4 + + const result = uut.checkInternalIp(req) + + assert.equal(result, false) + }) + }) +})