RateLimterTests.coffee 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/infrastructure/RateLimiter.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileStoreHandler", ->
  9. beforeEach ->
  10. @settings =
  11. redis:
  12. web:
  13. port:"1234"
  14. host:"somewhere"
  15. password: "password"
  16. @redbackInstance =
  17. addCount: sinon.stub()
  18. @redback =
  19. createRateLimit: sinon.stub().returns(@redbackInstance)
  20. @redis =
  21. createClient: ->
  22. return auth:->
  23. @limiter = SandboxedModule.require modulePath, requires:
  24. "settings-sharelatex":@settings
  25. "logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
  26. "redis-sharelatex": @redis
  27. "redback": use: => @redback
  28. @endpointName = "compiles"
  29. @subject = "some project id"
  30. @timeInterval = 20
  31. @throttleLimit = 5
  32. @details =
  33. endpointName: @endpointName
  34. subjectName: @subject
  35. throttle: @throttleLimit
  36. timeInterval: @timeInterval
  37. describe "addCount", ->
  38. beforeEach ->
  39. @redbackInstance.addCount.callsArgWith(2, null, 10)
  40. it "should use correct namespace", (done)->
  41. @limiter.addCount @details, =>
  42. @redback.createRateLimit.calledWith(@endpointName).should.equal true
  43. done()
  44. it "should only call it once", (done)->
  45. @limiter.addCount @details, =>
  46. @redbackInstance.addCount.callCount.should.equal 1
  47. done()
  48. it "should use the subjectName", (done)->
  49. @limiter.addCount @details, =>
  50. @redbackInstance.addCount.calledWith(@details.subjectName, @details.timeInterval).should.equal true
  51. done()
  52. it "should return true if the count is less than throttle", (done)->
  53. @details.throttle = 100
  54. @limiter.addCount @details, (err, canProcess)=>
  55. canProcess.should.equal true
  56. done()
  57. it "should return true if the count is less than throttle", (done)->
  58. @details.throttle = 1
  59. @limiter.addCount @details, (err, canProcess)=>
  60. canProcess.should.equal false
  61. done()
  62. it "should return false if the limit is matched", (done)->
  63. @details.throttle = 10
  64. @limiter.addCount @details, (err, canProcess)=>
  65. canProcess.should.equal false
  66. done()