overleaf/services/web/test/unit/coffee/Security/LoginRateLimiterTests.coffee

75 lines
2.3 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
expect = require('chai').expect
2014-02-12 05:23:40 -05:00
modulePath = require('path').join __dirname, '../../../../app/js/Features/Security/LoginRateLimiter'
describe "LoginRateLimiter", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@email = "bob@bob.com"
@RateLimiter =
clearRateLimit: sinon.stub()
addCount: sinon.stub()
2014-02-12 05:23:40 -05:00
@LoginRateLimiter = SandboxedModule.require modulePath, requires:
'../../infrastructure/RateLimiter': @RateLimiter
2014-02-12 05:23:40 -05:00
describe "processLoginRequest", ->
beforeEach ->
@RateLimiter.addCount = sinon.stub().callsArgWith(1, null, true)
2014-02-12 05:23:40 -05:00
it 'should call RateLimiter.addCount', (done) ->
@LoginRateLimiter.processLoginRequest @email, (err, allow) =>
@RateLimiter.addCount.callCount.should.equal 1
expect(@RateLimiter.addCount.lastCall.args[0].endpointName).to.equal 'login'
expect(@RateLimiter.addCount.lastCall.args[0].subjectName).to.equal @email
2014-02-12 05:23:40 -05:00
done()
describe 'when login is allowed', ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@RateLimiter.addCount = sinon.stub().callsArgWith(1, null, true)
2014-02-12 05:23:40 -05:00
it 'should call pass allow=true', (done) ->
@LoginRateLimiter.processLoginRequest @email, (err, allow) =>
expect(err).to.equal null
expect(allow).to.equal true
done()
2014-02-12 05:23:40 -05:00
describe 'when login is blocked', ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@RateLimiter.addCount = sinon.stub().callsArgWith(1, null, false)
2014-09-29 06:42:46 -04:00
it 'should call pass allow=false', (done) ->
@LoginRateLimiter.processLoginRequest @email, (err, allow) =>
expect(err).to.equal null
expect(allow).to.equal false
done()
2014-09-29 06:42:46 -04:00
describe 'when addCount produces an error', ->
2014-09-29 06:42:46 -04:00
beforeEach ->
@RateLimiter.addCount = sinon.stub().callsArgWith(1, new Error('woops'))
2014-09-29 06:42:46 -04:00
it 'should produce an error', (done) ->
@LoginRateLimiter.processLoginRequest @email, (err, allow) =>
expect(err).to.not.equal null
expect(err).to.be.instanceof Error
done()
2014-09-29 06:42:46 -04:00
2014-02-12 05:23:40 -05:00
describe "recordSuccessfulLogin", ->
beforeEach ->
@RateLimiter.clearRateLimit = sinon.stub().callsArgWith 2, null
it "should call clearRateLimit", (done)->
2014-02-12 05:23:40 -05:00
@LoginRateLimiter.recordSuccessfulLogin @email, =>
@RateLimiter.clearRateLimit.callCount.should.equal 1
@RateLimiter.clearRateLimit.calledWith('login', @email).should.equal true
done()