CooldownMiddleware.test.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = new URL(
  4. '../../../../app/src/Features/Cooldown/CooldownMiddleware.mjs',
  5. import.meta.url
  6. ).pathname
  7. describe('CooldownMiddleware', function () {
  8. beforeEach(async function (ctx) {
  9. ctx.CooldownManager = { isProjectOnCooldown: sinon.stub() }
  10. vi.doMock(
  11. '../../../../app/src/Features/Cooldown/CooldownManager.js',
  12. () => ({
  13. default: ctx.CooldownManager,
  14. })
  15. )
  16. ctx.CooldownMiddleware = (await import(modulePath)).default
  17. })
  18. describe('freezeProject', function () {
  19. describe('when project is on cooldown', function () {
  20. beforeEach(function (ctx) {
  21. ctx.CooldownManager.isProjectOnCooldown = sinon
  22. .stub()
  23. .callsArgWith(1, null, true)
  24. ctx.req = { params: { Project_id: 'abc' } }
  25. ctx.res = { sendStatus: sinon.stub() }
  26. return (ctx.next = sinon.stub())
  27. })
  28. it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
  29. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  30. ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  31. return ctx.CooldownManager.isProjectOnCooldown
  32. .calledWith('abc')
  33. .should.equal(true)
  34. })
  35. it('should not produce an error', function (ctx) {
  36. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  37. return ctx.next.callCount.should.equal(0)
  38. })
  39. it('should send a 429 status', function (ctx) {
  40. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  41. ctx.res.sendStatus.callCount.should.equal(1)
  42. return ctx.res.sendStatus.calledWith(429).should.equal(true)
  43. })
  44. })
  45. describe('when project is not on cooldown', function () {
  46. beforeEach(function (ctx) {
  47. ctx.CooldownManager.isProjectOnCooldown = sinon
  48. .stub()
  49. .callsArgWith(1, null, false)
  50. ctx.req = { params: { Project_id: 'abc' } }
  51. ctx.res = { sendStatus: sinon.stub() }
  52. return (ctx.next = sinon.stub())
  53. })
  54. it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
  55. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  56. ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  57. return ctx.CooldownManager.isProjectOnCooldown
  58. .calledWith('abc')
  59. .should.equal(true)
  60. })
  61. it('call next with no arguments', function (ctx) {
  62. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  63. ctx.next.callCount.should.equal(1)
  64. return expect(ctx.next.lastCall.args.length).to.equal(0)
  65. })
  66. })
  67. describe('when isProjectOnCooldown produces an error', function () {
  68. beforeEach(function (ctx) {
  69. ctx.CooldownManager.isProjectOnCooldown = sinon
  70. .stub()
  71. .callsArgWith(1, new Error('woops'))
  72. ctx.req = { params: { Project_id: 'abc' } }
  73. ctx.res = { sendStatus: sinon.stub() }
  74. return (ctx.next = sinon.stub())
  75. })
  76. it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
  77. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  78. ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  79. return ctx.CooldownManager.isProjectOnCooldown
  80. .calledWith('abc')
  81. .should.equal(true)
  82. })
  83. it('call next with an error', function (ctx) {
  84. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  85. ctx.next.callCount.should.equal(1)
  86. return expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  87. })
  88. })
  89. describe('when projectId is not part of route', function () {
  90. beforeEach(function (ctx) {
  91. ctx.CooldownManager.isProjectOnCooldown = sinon
  92. .stub()
  93. .callsArgWith(1, null, true)
  94. ctx.req = { params: { lol: 'abc' } }
  95. ctx.res = { sendStatus: sinon.stub() }
  96. return (ctx.next = sinon.stub())
  97. })
  98. it('call next with an error', function (ctx) {
  99. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  100. ctx.next.callCount.should.equal(1)
  101. return expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  102. })
  103. it('should not call CooldownManager.isProjectOnCooldown', function (ctx) {
  104. ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
  105. return ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(0)
  106. })
  107. })
  108. })
  109. })