LoginRateLimiterTests.coffee 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Security/LoginRateLimiter'
  5. buildKey = (k)->
  6. return "LoginRateLimit:#{k}"
  7. describe "LoginRateLimiter", ->
  8. beforeEach ->
  9. @email = "bob@bob.com"
  10. @incrStub = sinon.stub()
  11. @getStub = sinon.stub()
  12. @execStub = sinon.stub()
  13. @expireStub = sinon.stub()
  14. @delStub = sinon.stub().callsArgWith(1)
  15. @rclient =
  16. auth:->
  17. del: @delStub
  18. multi: =>
  19. incr: @incrStub
  20. expire: @expireStub
  21. get: @getStub
  22. exec: @execStub
  23. @LoginRateLimiter = SandboxedModule.require modulePath, requires:
  24. 'redis-sharelatex' : createClient: () => @rclient
  25. "settings-sharelatex":{redis:{}}
  26. describe "processLoginRequest", ->
  27. it "should inc the counter for login requests in redis", (done)->
  28. @execStub.callsArgWith(0, "null", ["",""])
  29. @LoginRateLimiter.processLoginRequest @email, =>
  30. @incrStub.calledWith(buildKey(@email)).should.equal true
  31. done()
  32. it "should set a expire", (done)->
  33. @execStub.callsArgWith(0, "null", ["",""])
  34. @LoginRateLimiter.processLoginRequest @email, =>
  35. @expireStub.calledWith(buildKey(@email), 60 * 2).should.equal true
  36. done()
  37. it "should return true if the count is below 10", (done)->
  38. @execStub.callsArgWith(0, "null", ["", 9])
  39. @LoginRateLimiter.processLoginRequest @email, (err, isAllowed)=>
  40. isAllowed.should.equal true
  41. done()
  42. it "should return true if the count is 10", (done)->
  43. @execStub.callsArgWith(0, "null", ["", 10])
  44. @LoginRateLimiter.processLoginRequest @email, (err, isAllowed)=>
  45. isAllowed.should.equal true
  46. done()
  47. it "should return false if the count is above 10", (done)->
  48. @execStub.callsArgWith(0, "null", ["", 11])
  49. @LoginRateLimiter.processLoginRequest @email, (err, isAllowed)=>
  50. isAllowed.should.equal false
  51. done()
  52. describe "smoke test user", ->
  53. it "should have a higher limit", (done)->
  54. done()
  55. describe "recordSuccessfulLogin", ->
  56. it "should delete the user key", (done)->
  57. @LoginRateLimiter.recordSuccessfulLogin @email, =>
  58. @delStub.calledWith(buildKey(@email)).should.equal true
  59. done()