CooldownManagerTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../../app/src/Features/Cooldown/CooldownManager'
  19. )
  20. describe('CooldownManager', function () {
  21. beforeEach(function () {
  22. this.projectId = 'abcdefg'
  23. this.rclient = { set: sinon.stub(), get: sinon.stub() }
  24. this.RedisWrapper = { client: () => this.rclient }
  25. return (this.CooldownManager = SandboxedModule.require(modulePath, {
  26. requires: {
  27. '../../infrastructure/RedisWrapper': this.RedisWrapper,
  28. },
  29. }))
  30. })
  31. describe('_buildKey', function () {
  32. it('should build a properly formatted redis key', function () {
  33. return expect(this.CooldownManager._buildKey('ABC')).to.equal(
  34. 'Cooldown:{ABC}'
  35. )
  36. })
  37. })
  38. describe('isProjectOnCooldown', function () {
  39. beforeEach(function () {
  40. return (this.call = cb => {
  41. return this.CooldownManager.isProjectOnCooldown(this.projectId, cb)
  42. })
  43. })
  44. describe('when project is on cooldown', function () {
  45. beforeEach(function () {
  46. return (this.rclient.get = sinon.stub().callsArgWith(1, null, '1'))
  47. })
  48. it('should fetch key from redis', function (done) {
  49. return this.call((err, result) => {
  50. this.rclient.get.callCount.should.equal(1)
  51. this.rclient.get.calledWith('Cooldown:{abcdefg}').should.equal(true)
  52. return done()
  53. })
  54. })
  55. it('should not produce an error', function (done) {
  56. return this.call((err, result) => {
  57. expect(err).to.equal(null)
  58. return done()
  59. })
  60. })
  61. it('should produce a true result', function (done) {
  62. return this.call((err, result) => {
  63. expect(result).to.equal(true)
  64. return done()
  65. })
  66. })
  67. })
  68. describe('when project is not on cooldown', function () {
  69. beforeEach(function () {
  70. return (this.rclient.get = sinon.stub().callsArgWith(1, null, null))
  71. })
  72. it('should fetch key from redis', function (done) {
  73. return this.call((err, result) => {
  74. this.rclient.get.callCount.should.equal(1)
  75. this.rclient.get.calledWith('Cooldown:{abcdefg}').should.equal(true)
  76. return done()
  77. })
  78. })
  79. it('should not produce an error', function (done) {
  80. return this.call((err, result) => {
  81. expect(err).to.equal(null)
  82. return done()
  83. })
  84. })
  85. it('should produce a false result', function (done) {
  86. return this.call((err, result) => {
  87. expect(result).to.equal(false)
  88. return done()
  89. })
  90. })
  91. })
  92. describe('when rclient.get produces an error', function () {
  93. beforeEach(function () {
  94. return (this.rclient.get = sinon
  95. .stub()
  96. .callsArgWith(1, new Error('woops')))
  97. })
  98. it('should fetch key from redis', function (done) {
  99. return this.call((err, result) => {
  100. this.rclient.get.callCount.should.equal(1)
  101. this.rclient.get.calledWith('Cooldown:{abcdefg}').should.equal(true)
  102. return done()
  103. })
  104. })
  105. it('should produce an error', function (done) {
  106. return this.call((err, result) => {
  107. expect(err).to.not.equal(null)
  108. expect(err).to.be.instanceof(Error)
  109. return done()
  110. })
  111. })
  112. })
  113. })
  114. describe('putProjectOnCooldown', function () {
  115. beforeEach(function () {
  116. return (this.call = cb => {
  117. return this.CooldownManager.putProjectOnCooldown(this.projectId, cb)
  118. })
  119. })
  120. describe('when rclient.set does not produce an error', function () {
  121. beforeEach(function () {
  122. return (this.rclient.set = sinon.stub().callsArgWith(4, null))
  123. })
  124. it('should set a key in redis', function (done) {
  125. return this.call(err => {
  126. this.rclient.set.callCount.should.equal(1)
  127. this.rclient.set.calledWith('Cooldown:{abcdefg}').should.equal(true)
  128. return done()
  129. })
  130. })
  131. it('should not produce an error', function (done) {
  132. return this.call(err => {
  133. expect(err).to.equal(null)
  134. return done()
  135. })
  136. })
  137. })
  138. describe('when rclient.set produces an error', function () {
  139. beforeEach(function () {
  140. return (this.rclient.set = sinon
  141. .stub()
  142. .callsArgWith(4, new Error('woops')))
  143. })
  144. it('should set a key in redis', function (done) {
  145. return this.call(err => {
  146. this.rclient.set.callCount.should.equal(1)
  147. this.rclient.set.calledWith('Cooldown:{abcdefg}').should.equal(true)
  148. return done()
  149. })
  150. })
  151. it('produce an error', function (done) {
  152. return this.call(err => {
  153. expect(err).to.not.equal(null)
  154. expect(err).to.be.instanceof(Error)
  155. return done()
  156. })
  157. })
  158. })
  159. })
  160. })