CooldownMiddlewareTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. require('chai').should()
  15. const { expect } = require('chai')
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../../app/src/Features/Cooldown/CooldownMiddleware'
  19. )
  20. describe('CooldownMiddleware', function() {
  21. beforeEach(function() {
  22. this.CooldownManager = { isProjectOnCooldown: sinon.stub() }
  23. return (this.CooldownMiddleware = SandboxedModule.require(modulePath, {
  24. globals: {
  25. console: console
  26. },
  27. requires: {
  28. './CooldownManager': this.CooldownManager,
  29. 'logger-sharelatex': { log: sinon.stub() }
  30. }
  31. }))
  32. })
  33. describe('freezeProject', function() {
  34. describe('when project is on cooldown', function() {
  35. beforeEach(function() {
  36. this.CooldownManager.isProjectOnCooldown = sinon
  37. .stub()
  38. .callsArgWith(1, null, true)
  39. this.req = { params: { Project_id: 'abc' } }
  40. this.res = { sendStatus: sinon.stub() }
  41. return (this.next = sinon.stub())
  42. })
  43. it('should call CooldownManager.isProjectOnCooldown', function() {
  44. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  45. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  46. return this.CooldownManager.isProjectOnCooldown
  47. .calledWith('abc')
  48. .should.equal(true)
  49. })
  50. it('should not produce an error', function() {
  51. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  52. return this.next.callCount.should.equal(0)
  53. })
  54. it('should send a 429 status', function() {
  55. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  56. this.res.sendStatus.callCount.should.equal(1)
  57. return this.res.sendStatus.calledWith(429).should.equal(true)
  58. })
  59. })
  60. describe('when project is not on cooldown', function() {
  61. beforeEach(function() {
  62. this.CooldownManager.isProjectOnCooldown = sinon
  63. .stub()
  64. .callsArgWith(1, null, false)
  65. this.req = { params: { Project_id: 'abc' } }
  66. this.res = { sendStatus: sinon.stub() }
  67. return (this.next = sinon.stub())
  68. })
  69. it('should call CooldownManager.isProjectOnCooldown', function() {
  70. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  71. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  72. return this.CooldownManager.isProjectOnCooldown
  73. .calledWith('abc')
  74. .should.equal(true)
  75. })
  76. it('call next with no arguments', function() {
  77. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  78. this.next.callCount.should.equal(1)
  79. return expect(this.next.lastCall.args.length).to.equal(0)
  80. })
  81. })
  82. describe('when isProjectOnCooldown produces an error', function() {
  83. beforeEach(function() {
  84. this.CooldownManager.isProjectOnCooldown = sinon
  85. .stub()
  86. .callsArgWith(1, new Error('woops'))
  87. this.req = { params: { Project_id: 'abc' } }
  88. this.res = { sendStatus: sinon.stub() }
  89. return (this.next = sinon.stub())
  90. })
  91. it('should call CooldownManager.isProjectOnCooldown', function() {
  92. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  93. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  94. return this.CooldownManager.isProjectOnCooldown
  95. .calledWith('abc')
  96. .should.equal(true)
  97. })
  98. it('call next with an error', function() {
  99. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  100. this.next.callCount.should.equal(1)
  101. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  102. })
  103. })
  104. describe('when projectId is not part of route', function() {
  105. beforeEach(function() {
  106. this.CooldownManager.isProjectOnCooldown = sinon
  107. .stub()
  108. .callsArgWith(1, null, true)
  109. this.req = { params: { lol: 'abc' } }
  110. this.res = { sendStatus: sinon.stub() }
  111. return (this.next = sinon.stub())
  112. })
  113. it('call next with an error', function() {
  114. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  115. this.next.callCount.should.equal(1)
  116. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  117. })
  118. it('should not call CooldownManager.isProjectOnCooldown', function() {
  119. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  120. return this.CooldownManager.isProjectOnCooldown.callCount.should.equal(
  121. 0
  122. )
  123. })
  124. })
  125. })
  126. })