LoginRateLimiterTests.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. expect = require('chai').expect
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/Security/LoginRateLimiter'
  6. describe "LoginRateLimiter", ->
  7. beforeEach ->
  8. @email = "bob@bob.com"
  9. @RateLimiter =
  10. clearRateLimit: sinon.stub()
  11. addCount: sinon.stub()
  12. @LoginRateLimiter = SandboxedModule.require modulePath, requires:
  13. '../../infrastructure/RateLimiter': @RateLimiter
  14. describe "processLoginRequest", ->
  15. beforeEach ->
  16. @RateLimiter.addCount = sinon.stub().callsArgWith(1, null, true)
  17. it 'should call RateLimiter.addCount', (done) ->
  18. @LoginRateLimiter.processLoginRequest @email, (err, allow) =>
  19. @RateLimiter.addCount.callCount.should.equal 1
  20. expect(@RateLimiter.addCount.lastCall.args[0].endpointName).to.equal 'login'
  21. expect(@RateLimiter.addCount.lastCall.args[0].subjectName).to.equal @email
  22. done()
  23. describe 'when login is allowed', ->
  24. beforeEach ->
  25. @RateLimiter.addCount = sinon.stub().callsArgWith(1, null, true)
  26. it 'should call pass allow=true', (done) ->
  27. @LoginRateLimiter.processLoginRequest @email, (err, allow) =>
  28. expect(err).to.equal null
  29. expect(allow).to.equal true
  30. done()
  31. describe 'when login is blocked', ->
  32. beforeEach ->
  33. @RateLimiter.addCount = sinon.stub().callsArgWith(1, null, false)
  34. it 'should call pass allow=false', (done) ->
  35. @LoginRateLimiter.processLoginRequest @email, (err, allow) =>
  36. expect(err).to.equal null
  37. expect(allow).to.equal false
  38. done()
  39. describe 'when addCount produces an error', ->
  40. beforeEach ->
  41. @RateLimiter.addCount = sinon.stub().callsArgWith(1, new Error('woops'))
  42. it 'should produce an error', (done) ->
  43. @LoginRateLimiter.processLoginRequest @email, (err, allow) =>
  44. expect(err).to.not.equal null
  45. expect(err).to.be.instanceof Error
  46. done()
  47. describe "recordSuccessfulLogin", ->
  48. beforeEach ->
  49. @RateLimiter.clearRateLimit = sinon.stub().callsArgWith 2, null
  50. it "should call clearRateLimit", (done)->
  51. @LoginRateLimiter.recordSuccessfulLogin @email, =>
  52. @RateLimiter.clearRateLimit.callCount.should.equal 1
  53. @RateLimiter.clearRateLimit.calledWith('login', @email).should.equal true
  54. done()