2014-02-28 12:59:54 -05:00
|
|
|
assert = require("chai").assert
|
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/infrastructure/RateLimiter.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2016-12-19 07:17:02 -05:00
|
|
|
describe "RateLimiter", ->
|
2014-02-28 12:59:54 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2014-03-03 06:46:40 -05:00
|
|
|
@settings =
|
|
|
|
redis:
|
|
|
|
web:
|
|
|
|
port:"1234"
|
|
|
|
host:"somewhere"
|
|
|
|
password: "password"
|
2016-12-19 07:17:02 -05:00
|
|
|
@rclient =
|
|
|
|
incr: sinon.stub()
|
|
|
|
get: sinon.stub()
|
|
|
|
expire: sinon.stub()
|
|
|
|
exec: sinon.stub()
|
|
|
|
@rclient.multi = sinon.stub().returns(@rclient)
|
|
|
|
@RedisWrapper =
|
|
|
|
client: sinon.stub().returns(@rclient)
|
2014-02-28 12:59:54 -05:00
|
|
|
|
2017-01-13 11:04:26 -05:00
|
|
|
@limiterFn = sinon.stub()
|
|
|
|
@RollingRateLimiter = (opts) =>
|
|
|
|
return @limiterFn
|
|
|
|
|
2014-02-28 12:59:54 -05:00
|
|
|
@limiter = SandboxedModule.require modulePath, requires:
|
2017-01-13 11:04:26 -05:00
|
|
|
"rolling-rate-limiter": @RollingRateLimiter
|
2014-02-28 12:59:54 -05:00
|
|
|
"settings-sharelatex":@settings
|
|
|
|
"logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
|
2016-12-19 07:17:02 -05:00
|
|
|
"./RedisWrapper": @RedisWrapper
|
2014-02-28 12:59:54 -05:00
|
|
|
|
|
|
|
@endpointName = "compiles"
|
2016-12-19 07:17:02 -05:00
|
|
|
@subject = "some-project-id"
|
2014-02-28 12:59:54 -05:00
|
|
|
@timeInterval = 20
|
|
|
|
@throttleLimit = 5
|
|
|
|
|
|
|
|
@details =
|
|
|
|
endpointName: @endpointName
|
|
|
|
subjectName: @subject
|
|
|
|
throttle: @throttleLimit
|
|
|
|
timeInterval: @timeInterval
|
2016-12-19 07:17:02 -05:00
|
|
|
@key = "RateLimiter:#{@endpointName}:{#{@subject}}"
|
|
|
|
|
2017-01-13 11:04:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe 'when action is permitted', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@limiterFn = sinon.stub().callsArgWith(1, null, 0, 22)
|
|
|
|
|
|
|
|
it 'should not produce and error', (done) ->
|
|
|
|
@limiter.addCount {}, (err, should) ->
|
|
|
|
expect(err).to.equal null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should callback with true', (done) ->
|
|
|
|
@limiter.addCount {}, (err, should) ->
|
|
|
|
expect(should).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when action is not permitted', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@limiterFn = sinon.stub().callsArgWith(1, null, 4000, 0)
|
|
|
|
|
|
|
|
it 'should not produce and error', (done) ->
|
|
|
|
@limiter.addCount {}, (err, should) ->
|
|
|
|
expect(err).to.equal null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should callback with false', (done) ->
|
|
|
|
@limiter.addCount {}, (err, should) ->
|
|
|
|
expect(should).to.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when limiter produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@limiterFn = sinon.stub().callsArgWith(1, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should produce and error', (done) ->
|
|
|
|
@limiter.addCount {}, (err, should) ->
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|