LoginRateLimiterTests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Security/LoginRateLimiter'
  19. )
  20. describe('LoginRateLimiter', function () {
  21. beforeEach(function () {
  22. this.email = 'bob@bob.com'
  23. this.RateLimiter = {
  24. clearRateLimit: sinon.stub(),
  25. addCount: sinon.stub(),
  26. }
  27. return (this.LoginRateLimiter = SandboxedModule.require(modulePath, {
  28. requires: {
  29. '../../infrastructure/RateLimiter': this.RateLimiter,
  30. },
  31. }))
  32. })
  33. describe('processLoginRequest', function () {
  34. beforeEach(function () {
  35. return (this.RateLimiter.addCount = sinon
  36. .stub()
  37. .callsArgWith(1, null, true))
  38. })
  39. it('should call RateLimiter.addCount', function (done) {
  40. return this.LoginRateLimiter.processLoginRequest(
  41. this.email,
  42. (err, allow) => {
  43. this.RateLimiter.addCount.callCount.should.equal(1)
  44. expect(
  45. this.RateLimiter.addCount.lastCall.args[0].endpointName
  46. ).to.equal('login')
  47. expect(
  48. this.RateLimiter.addCount.lastCall.args[0].subjectName
  49. ).to.equal(this.email)
  50. return done()
  51. }
  52. )
  53. })
  54. describe('when login is allowed', function () {
  55. beforeEach(function () {
  56. return (this.RateLimiter.addCount = sinon
  57. .stub()
  58. .callsArgWith(1, null, true))
  59. })
  60. it('should call pass allow=true', function (done) {
  61. return this.LoginRateLimiter.processLoginRequest(
  62. this.email,
  63. (err, allow) => {
  64. expect(err).to.equal(null)
  65. expect(allow).to.equal(true)
  66. return done()
  67. }
  68. )
  69. })
  70. })
  71. describe('when login is blocked', function () {
  72. beforeEach(function () {
  73. return (this.RateLimiter.addCount = sinon
  74. .stub()
  75. .callsArgWith(1, null, false))
  76. })
  77. it('should call pass allow=false', function (done) {
  78. return this.LoginRateLimiter.processLoginRequest(
  79. this.email,
  80. (err, allow) => {
  81. expect(err).to.equal(null)
  82. expect(allow).to.equal(false)
  83. return done()
  84. }
  85. )
  86. })
  87. })
  88. describe('when addCount produces an error', function () {
  89. beforeEach(function () {
  90. return (this.RateLimiter.addCount = sinon
  91. .stub()
  92. .callsArgWith(1, new Error('woops')))
  93. })
  94. it('should produce an error', function (done) {
  95. return this.LoginRateLimiter.processLoginRequest(
  96. this.email,
  97. (err, allow) => {
  98. expect(err).to.not.equal(null)
  99. expect(err).to.be.instanceof(Error)
  100. return done()
  101. }
  102. )
  103. })
  104. })
  105. })
  106. describe('recordSuccessfulLogin', function () {
  107. beforeEach(function () {
  108. return (this.RateLimiter.clearRateLimit = sinon
  109. .stub()
  110. .callsArgWith(2, null))
  111. })
  112. it('should call clearRateLimit', function (done) {
  113. return this.LoginRateLimiter.recordSuccessfulLogin(this.email, () => {
  114. this.RateLimiter.clearRateLimit.callCount.should.equal(1)
  115. this.RateLimiter.clearRateLimit
  116. .calledWith('login', this.email)
  117. .should.equal(true)
  118. return done()
  119. })
  120. })
  121. })
  122. })