mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Got first unit tests for new rate limit middleware
This commit is contained in:
Generated
+6
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user