CooldownMiddlewareTests.coffee 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/Cooldown/CooldownMiddleware'
  6. describe "CooldownMiddleware", ->
  7. beforeEach ->
  8. @CooldownManager =
  9. isProjectOnCooldown: sinon.stub()
  10. @CooldownMiddleware = SandboxedModule.require modulePath, requires:
  11. './CooldownManager': @CooldownManager
  12. 'logger-sharelatex': {log: sinon.stub()}
  13. describe 'freezeProject', ->
  14. describe 'when project is on cooldown', ->
  15. beforeEach ->
  16. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
  17. @req = {params: {Project_id: 'abc'}}
  18. @res = {sendStatus: sinon.stub()}
  19. @next = sinon.stub()
  20. it 'should call CooldownManager.isProjectOnCooldown', ->
  21. @CooldownMiddleware.freezeProject @req, @res, @next
  22. @CooldownManager.isProjectOnCooldown.callCount.should.equal 1
  23. @CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
  24. it 'should not produce an error', ->
  25. @CooldownMiddleware.freezeProject @req, @res, @next
  26. @next.callCount.should.equal 0
  27. it 'should send a 429 status', ->
  28. @CooldownMiddleware.freezeProject @req, @res, @next
  29. @res.sendStatus.callCount.should.equal 1
  30. @res.sendStatus.calledWith(429).should.equal true
  31. describe 'when project is not on cooldown', ->
  32. beforeEach ->
  33. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, false)
  34. @req = {params: {Project_id: 'abc'}}
  35. @res = {sendStatus: sinon.stub()}
  36. @next = sinon.stub()
  37. it 'should call CooldownManager.isProjectOnCooldown', ->
  38. @CooldownMiddleware.freezeProject @req, @res, @next
  39. @CooldownManager.isProjectOnCooldown.callCount.should.equal 1
  40. @CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
  41. it 'call next with no arguments', ->
  42. @CooldownMiddleware.freezeProject @req, @res, @next
  43. @next.callCount.should.equal 1
  44. expect(@next.lastCall.args.length).to.equal 0
  45. describe 'when isProjectOnCooldown produces an error', ->
  46. beforeEach ->
  47. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, new Error('woops'))
  48. @req = {params: {Project_id: 'abc'}}
  49. @res = {sendStatus: sinon.stub()}
  50. @next = sinon.stub()
  51. it 'should call CooldownManager.isProjectOnCooldown', ->
  52. @CooldownMiddleware.freezeProject @req, @res, @next
  53. @CooldownManager.isProjectOnCooldown.callCount.should.equal 1
  54. @CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
  55. it 'call next with an error', ->
  56. @CooldownMiddleware.freezeProject @req, @res, @next
  57. @next.callCount.should.equal 1
  58. expect(@next.lastCall.args[0]).to.be.instanceof Error
  59. describe 'when projectId is not part of route', ->
  60. beforeEach ->
  61. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
  62. @req = {params: {lol: 'abc'}}
  63. @res = {sendStatus: sinon.stub()}
  64. @next = sinon.stub()
  65. it 'call next with an error', ->
  66. @CooldownMiddleware.freezeProject @req, @res, @next
  67. @next.callCount.should.equal 1
  68. expect(@next.lastCall.args[0]).to.be.instanceof Error
  69. it 'should not call CooldownManager.isProjectOnCooldown', ->
  70. @CooldownMiddleware.freezeProject @req, @res, @next
  71. @CooldownManager.isProjectOnCooldown.callCount.should.equal 0