CooldownMiddlewareTests.js 4.8 KB

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