mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
fix(rate-limits): Re-enabling rate-limit unit tests. Setting rate limit to 10 RPM
This commit is contained in:
@@ -15,7 +15,7 @@ const RateLimit = require("express-rate-limit")
|
|||||||
// Set max requests per minute
|
// Set max requests per minute
|
||||||
const maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS
|
const maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS
|
||||||
? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS)
|
? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS)
|
||||||
: 60
|
: 6
|
||||||
|
|
||||||
// Pro-tier rate limits are 10x the freemium limits.
|
// Pro-tier rate limits are 10x the freemium limits.
|
||||||
const PRO_RPM = 10 * maxRequests
|
const PRO_RPM = 10 * maxRequests
|
||||||
@@ -33,6 +33,8 @@ const routeRateLimit = function(req, res, next) {
|
|||||||
// Current route
|
// Current route
|
||||||
const rateLimitTier = req.locals.proLimit ? "PRO" : "BASIC"
|
const rateLimitTier = req.locals.proLimit ? "PRO" : "BASIC"
|
||||||
const path = req.baseUrl + req.path
|
const path = req.baseUrl + req.path
|
||||||
|
|
||||||
|
// Create a unique string as a route identifier.
|
||||||
const route =
|
const route =
|
||||||
rateLimitTier +
|
rateLimitTier +
|
||||||
req.method +
|
req.method +
|
||||||
@@ -40,6 +42,7 @@ const routeRateLimit = function(req, res, next) {
|
|||||||
.split("/")
|
.split("/")
|
||||||
.slice(0, 4)
|
.slice(0, 4)
|
||||||
.join("/")
|
.join("/")
|
||||||
|
//console.log(`route identifier: ${JSON.stringify(route, null, 2)}`)
|
||||||
|
|
||||||
// This boolean value is passed from the auth.js middleware.
|
// This boolean value is passed from the auth.js middleware.
|
||||||
const proRateLimits = req.locals.proLimit
|
const proRateLimits = req.locals.proLimit
|
||||||
@@ -89,6 +92,8 @@ const routeRateLimit = function(req, res, next) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//console.log(`calling uniqueRateLimits() on this route: ${route}`)
|
||||||
|
|
||||||
// Call rate limit for this route
|
// Call rate limit for this route
|
||||||
uniqueRateLimits[route](req, res, next)
|
uniqueRateLimits[route](req, res, next)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const axios = require("axios")
|
|||||||
const util = require("util")
|
const util = require("util")
|
||||||
util.inspect.defaultOptions = { depth: 1 }
|
util.inspect.defaultOptions = { depth: 1 }
|
||||||
|
|
||||||
const SERVER = `https://rest.btctest.net/v2/`
|
const SERVER = `http://192.168.0.36:12400/v3/`
|
||||||
//const SERVER = `http://localhost:3000/v2/`
|
//const SERVER = `http://localhost:3000/v2/`
|
||||||
|
|
||||||
describe("#rate limits", () => {
|
describe("#rate limits", () => {
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ const mockRes = {
|
|||||||
format: sinon.stub().returns({})
|
format: sinon.stub().returns({})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dev-Note on Rate Limits: Since next() is mocked, I can call the Sinon untility
|
||||||
|
// functions on it, like called(), to see if this stub was called.
|
||||||
const mockNext = sinon.stub().returns()
|
const mockNext = sinon.stub().returns()
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
+23
-23
@@ -12,7 +12,7 @@ util.inspect.defaultOptions = { depth: 1 }
|
|||||||
const { mockReq, mockRes, mockNext } = require("./mocks/express-mocks")
|
const { mockReq, mockRes, mockNext } = require("./mocks/express-mocks")
|
||||||
|
|
||||||
// Libraries under test
|
// Libraries under test
|
||||||
const rateLimitMiddleware = require("../../src/middleware/route-ratelimit")
|
let rateLimitMiddleware = require("../../src/middleware/route-ratelimit")
|
||||||
const controlRoute = require("../../src/routes/v3/control")
|
const controlRoute = require("../../src/routes/v3/control")
|
||||||
|
|
||||||
let req, res, next
|
let req, res, next
|
||||||
@@ -43,26 +43,28 @@ describe("#route-ratelimits", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("#routeRateLimit", () => {
|
describe("#routeRateLimit", () => {
|
||||||
const routeRateLimit = rateLimitMiddleware.routeRateLimit
|
let routeRateLimit = rateLimitMiddleware.routeRateLimit
|
||||||
const getInfo = controlRoute.testableComponents.getInfo
|
const getInfo = controlRoute.testableComponents.getInfo
|
||||||
/*
|
|
||||||
it("should pass through rate-limit middleware", async () => {
|
it("should pass through rate-limit middleware", async () => {
|
||||||
req.baseUrl = "/v2"
|
req.baseUrl = "/v3"
|
||||||
req.path = "/control/getInfo"
|
req.path = "/control/getNetworkInfo"
|
||||||
req.method = "GET"
|
req.method = "GET"
|
||||||
|
|
||||||
|
// Call the route twice to trigger the rate handling.
|
||||||
|
await routeRateLimit(req, res, next)
|
||||||
await routeRateLimit(req, res, next)
|
await routeRateLimit(req, res, next)
|
||||||
|
|
||||||
// next() will be called if rate-limit is not triggered
|
// next() will be called if rate-limit is not triggered
|
||||||
assert.equal(next.called, true)
|
assert.equal(next.called, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should trigger rate-limit handler if rate limits exceeds 60 request per minute", async () => {
|
it("should trigger rate-limit handler if rate limits exceeds 15 request per minute", async () => {
|
||||||
req.baseUrl = "/v2"
|
req.baseUrl = "/v3"
|
||||||
req.path = "/control/getInfo"
|
req.path = "/control/getNetworkInfo"
|
||||||
req.method = "GET"
|
req.method = "GET"
|
||||||
|
|
||||||
for (let i = 0; i < 65; i++) {
|
for (let i = 0; i < 15; i++) {
|
||||||
next.reset() // reset the stubbed next() function.
|
next.reset() // reset the stubbed next() function.
|
||||||
|
|
||||||
await routeRateLimit(req, res, next)
|
await routeRateLimit(req, res, next)
|
||||||
@@ -76,18 +78,17 @@ describe("#route-ratelimits", () => {
|
|||||||
`next should not be called if rate limit was triggered.`
|
`next should not be called if rate limit was triggered.`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
*/
|
|
||||||
/*
|
it("should NOT trigger rate-limit handler for pro-tier at 20 RPM", async () => {
|
||||||
it("should NOT trigger rate-limit handler for pro-tier at 65 RPM", async () => {
|
|
||||||
// Clear the require cache before running this test.
|
// Clear the require cache before running this test.
|
||||||
delete require.cache[
|
delete require.cache[
|
||||||
require.resolve("../../dist/middleware/route-ratelimit")
|
require.resolve("../../src/middleware/route-ratelimit")
|
||||||
]
|
]
|
||||||
rateLimitMiddleware = require("../../dist/middleware/route-ratelimit")
|
rateLimitMiddleware = require("../../src/middleware/route-ratelimit")
|
||||||
routeRateLimit = rateLimitMiddleware.routeRateLimit
|
routeRateLimit = rateLimitMiddleware.routeRateLimit
|
||||||
|
|
||||||
req.baseUrl = "/v2"
|
req.baseUrl = "/v3"
|
||||||
req.path = "/control/getInfo"
|
req.path = "/control/getNetworkInfo"
|
||||||
req.method = "GET"
|
req.method = "GET"
|
||||||
|
|
||||||
req.locals.proLimit = true
|
req.locals.proLimit = true
|
||||||
@@ -97,7 +98,7 @@ describe("#route-ratelimits", () => {
|
|||||||
// Prepare the authorization header
|
// Prepare the authorization header
|
||||||
//req.headers.authorization = generateAuthHeader("BITBOX")
|
//req.headers.authorization = generateAuthHeader("BITBOX")
|
||||||
|
|
||||||
for (let i = 0; i < 65; i++) {
|
for (let i = 0; i < 20; i++) {
|
||||||
next.reset() // reset the stubbed next() function.
|
next.reset() // reset the stubbed next() function.
|
||||||
|
|
||||||
await routeRateLimit(req, res, next)
|
await routeRateLimit(req, res, next)
|
||||||
@@ -117,13 +118,13 @@ describe("#route-ratelimits", () => {
|
|||||||
it("rate-limiting should still kick in at a higher RPM for pro-tier", async () => {
|
it("rate-limiting should still kick in at a higher RPM for pro-tier", async () => {
|
||||||
// Clear the require cache before running this test.
|
// Clear the require cache before running this test.
|
||||||
delete require.cache[
|
delete require.cache[
|
||||||
require.resolve("../../dist/middleware/route-ratelimit")
|
require.resolve("../../src/middleware/route-ratelimit")
|
||||||
]
|
]
|
||||||
rateLimitMiddleware = require("../../dist/middleware/route-ratelimit")
|
rateLimitMiddleware = require("../../src/middleware/route-ratelimit")
|
||||||
routeRateLimit = rateLimitMiddleware.routeRateLimit
|
routeRateLimit = rateLimitMiddleware.routeRateLimit
|
||||||
|
|
||||||
req.baseUrl = "/v2"
|
req.baseUrl = "/v3"
|
||||||
req.path = "/control/getInfo"
|
req.path = "/control/getNetworkInfo"
|
||||||
req.method = "GET"
|
req.method = "GET"
|
||||||
|
|
||||||
req.locals.proLimit = true
|
req.locals.proLimit = true
|
||||||
@@ -133,7 +134,7 @@ describe("#route-ratelimits", () => {
|
|||||||
// Prepare the authorization header
|
// Prepare the authorization header
|
||||||
//req.headers.authorization = generateAuthHeader("BITBOX")
|
//req.headers.authorization = generateAuthHeader("BITBOX")
|
||||||
|
|
||||||
for (let i = 0; i < 650; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
next.reset() // reset the stubbed next() function.
|
next.reset() // reset the stubbed next() function.
|
||||||
|
|
||||||
await routeRateLimit(req, res, next)
|
await routeRateLimit(req, res, next)
|
||||||
@@ -149,7 +150,6 @@ describe("#route-ratelimits", () => {
|
|||||||
`next should NOT be called if rate limit was triggered.`
|
`next should NOT be called if rate limit was triggered.`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
*/
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user