CooldownMiddlewareTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. requires: {
  25. './CooldownManager': this.CooldownManager,
  26. 'logger-sharelatex': { log: sinon.stub() }
  27. }
  28. }))
  29. })
  30. describe('freezeProject', function() {
  31. describe('when project is on cooldown', function() {
  32. beforeEach(function() {
  33. this.CooldownManager.isProjectOnCooldown = sinon
  34. .stub()
  35. .callsArgWith(1, null, true)
  36. this.req = { params: { Project_id: 'abc' } }
  37. this.res = { sendStatus: sinon.stub() }
  38. return (this.next = sinon.stub())
  39. })
  40. it('should call CooldownManager.isProjectOnCooldown', function() {
  41. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  42. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  43. return this.CooldownManager.isProjectOnCooldown
  44. .calledWith('abc')
  45. .should.equal(true)
  46. })
  47. it('should not produce an error', function() {
  48. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  49. return this.next.callCount.should.equal(0)
  50. })
  51. it('should send a 429 status', function() {
  52. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  53. this.res.sendStatus.callCount.should.equal(1)
  54. return this.res.sendStatus.calledWith(429).should.equal(true)
  55. })
  56. })
  57. describe('when project is not on cooldown', function() {
  58. beforeEach(function() {
  59. this.CooldownManager.isProjectOnCooldown = sinon
  60. .stub()
  61. .callsArgWith(1, null, false)
  62. this.req = { params: { Project_id: 'abc' } }
  63. this.res = { sendStatus: sinon.stub() }
  64. return (this.next = sinon.stub())
  65. })
  66. it('should call CooldownManager.isProjectOnCooldown', function() {
  67. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  68. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  69. return this.CooldownManager.isProjectOnCooldown
  70. .calledWith('abc')
  71. .should.equal(true)
  72. })
  73. it('call next with no arguments', function() {
  74. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  75. this.next.callCount.should.equal(1)
  76. return expect(this.next.lastCall.args.length).to.equal(0)
  77. })
  78. })
  79. describe('when isProjectOnCooldown produces an error', function() {
  80. beforeEach(function() {
  81. this.CooldownManager.isProjectOnCooldown = sinon
  82. .stub()
  83. .callsArgWith(1, new Error('woops'))
  84. this.req = { params: { Project_id: 'abc' } }
  85. this.res = { sendStatus: sinon.stub() }
  86. return (this.next = sinon.stub())
  87. })
  88. it('should call CooldownManager.isProjectOnCooldown', function() {
  89. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  90. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  91. return this.CooldownManager.isProjectOnCooldown
  92. .calledWith('abc')
  93. .should.equal(true)
  94. })
  95. it('call next with an error', function() {
  96. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  97. this.next.callCount.should.equal(1)
  98. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  99. })
  100. })
  101. describe('when projectId is not part of route', function() {
  102. beforeEach(function() {
  103. this.CooldownManager.isProjectOnCooldown = sinon
  104. .stub()
  105. .callsArgWith(1, null, true)
  106. this.req = { params: { lol: 'abc' } }
  107. this.res = { sendStatus: sinon.stub() }
  108. return (this.next = sinon.stub())
  109. })
  110. it('call next with an error', function() {
  111. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  112. this.next.callCount.should.equal(1)
  113. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  114. })
  115. it('should not call CooldownManager.isProjectOnCooldown', function() {
  116. this.CooldownMiddleware.freezeProject(this.req, this.res, this.next)
  117. return this.CooldownManager.isProjectOnCooldown.callCount.should.equal(
  118. 0
  119. )
  120. })
  121. })
  122. })
  123. })