Skip to content

Commit

Permalink
feat(production.js): rate limit config from env (#452)
Browse files Browse the repository at this point in the history
* feat(production.js): rate limit config from env

* fix(production.js): add base in parseInt

Co-authored-by: Christian <59786962+christian-hawk@users.noreply.github.com>
  • Loading branch information
kdhttps and christian-hawk authored Jun 22, 2022
1 parent 2e32cd9 commit 067b1a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions config/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = {
passportFile: '/etc/gluu/conf/passport-config.json',
saltFile: '/etc/gluu/conf/salt',
timerInterval: 60000,
rateLimitWindowMs: 24 * 60 * 60 * 1000, // 24 hrs in milliseconds
rateLimitMaxRequestAllow: 1000,
rateLimitWindowMs: parseInt(process.env.PASSPORT_RATE_LIMIT_WINDOW_MS, 10) || 24 * 60 * 60 * 1000, // 24 hrs in milliseconds
rateLimitMaxRequestAllow: parseInt(process.env.PASSPORT_RATE_LIMIT_MAX_REQUEST_ALLOW, 10) || 1000,
cookieSameSite: 'none',
cookieSecure: true,
HTTP_PROXY: process.env.HTTP_PROXY,
Expand Down
36 changes: 35 additions & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

const chai = require('chai')
const assert = chai.assert
const rewire = require('rewire')
const sinon = require('sinon')

/**
* Testing configs (env) on /config/*.js (uses node-config module)
Expand Down Expand Up @@ -34,7 +36,7 @@ describe('defaultcfg', function () {
})

describe('productioncfg', function () {
it('production.js should have passportFile not null or undefined', () => {
it('production.js should have passportFile not null or undefined', () => {
assert.exists(
productioncfg.passportFile, 'passportFile is not null or undefined')
})
Expand All @@ -56,4 +58,36 @@ describe('productioncfg', function () {
)
assert.isTrue(productioncfg.cookieSecure)
})
describe('rate limit', () => {
describe('limitWindowMs', () => {
it('should load from env', () => {
const rateLimitWindow = 1
process.env.PASSPORT_RATE_LIMIT_WINDOW_MS = rateLimitWindow
const rewiredProductionCfg = rewire('../config/production.js')
assert.equal(rewiredProductionCfg.rateLimitWindowMs, 1)
})
it('should call parseInt once with value', () => {
process.env.PASSPORT_RATE_LIMIT_WINDOW_MS = 'a valid rate limit'
const parseIntspy = sinon.spy(global, 'parseInt')
rewire('../config/production.js')
assert.isTrue(parseIntspy.calledWith('a valid rate limit'))
global.parseInt.restore()
})
})
describe('maxRequestAllow', () => {
it('should load from env', () => {
const maxRequestAllow = 2
process.env.PASSPORT_RATE_LIMIT_MAX_REQUEST_ALLOW = maxRequestAllow
const rewiredProductionCfg = rewire('../config/production.js')
assert.equal(rewiredProductionCfg.rateLimitMaxRequestAllow, 2)
})
it('should call parseInt with value', () => {
process.env.PASSPORT_RATE_LIMIT_MAX_REQUEST_ALLOW = 'a valid max request limit'
const parseIntspy = sinon.spy(global, 'parseInt')
rewire('../config/production.js')
assert.isTrue(parseIntspy.calledWith('a valid max request limit'))
global.parseInt.restore()
})
})
})
})

0 comments on commit 067b1a7

Please sign in to comment.