RateLimiterTests.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/infrastructure/RateLimiter'
  7. )
  8. describe('RateLimiter', function () {
  9. beforeEach(function () {
  10. this.rclient = {}
  11. this.RedisWrapper = {
  12. client: sinon.stub().returns(this.rclient),
  13. }
  14. this.RateLimiter = {
  15. RateLimiter: sinon.stub().withArgs('login').returns(this.rateLimiter),
  16. }
  17. this.RateLimiterFlexible = {
  18. RateLimiterRedis: sinon.stub(),
  19. }
  20. this.Settings = {}
  21. this.RateLimiter = SandboxedModule.require(modulePath, {
  22. requires: {
  23. './RedisWrapper': this.RedisWrapper,
  24. 'rate-limiter-flexible': this.RateLimiterFlexible,
  25. '@overleaf/settings': this.Settings,
  26. },
  27. })
  28. })
  29. describe('getSubnetKeyFromIp', function () {
  30. beforeEach(function () {
  31. this.rateLimiter = new this.RateLimiter.RateLimiter('some-limit', {
  32. points: 20,
  33. subnetPoints: 200,
  34. duration: 60,
  35. })
  36. })
  37. it('should correctly extract a subnet key from an ip address', function () {
  38. expect(this.rateLimiter.getSubnetKeyFromIp('255.255.255.255')).to.equal(
  39. '255.255.255'
  40. )
  41. expect(this.rateLimiter.getSubnetKeyFromIp('127.0.0.1')).to.equal(
  42. '127.0.0'
  43. )
  44. expect(this.rateLimiter.getSubnetKeyFromIp('243.12.3.126')).to.equal(
  45. '243.12.3'
  46. )
  47. })
  48. it('should throw an error when given an incorrectly formatted ip', function () {
  49. expect(() => this.rateLimiter.getSubnetKeyFromIp(1)).to.throw()
  50. expect(() => this.rateLimiter.getSubnetKeyFromIp('Not an ip')).to.throw()
  51. expect(() =>
  52. this.rateLimiter.getSubnetKeyFromIp('255.255.255.255.255.255')
  53. ).to.throw()
  54. expect(() =>
  55. this.rateLimiter.getSubnetKeyFromIp('255.255.255')
  56. ).to.throw()
  57. })
  58. })
  59. describe('_subnetRateLimiter', function () {
  60. it('should be defined by default', function () {
  61. const rateLimiter = new this.RateLimiter.RateLimiter('some-limit', {
  62. points: 20,
  63. subnetPoints: 200,
  64. duration: 60,
  65. })
  66. expect(rateLimiter._subnetRateLimiter).not.to.be.undefined
  67. })
  68. it('should be undefined when subnet rate limiting is disabled', function () {
  69. this.Settings.rateLimit = { subnetRateLimiterDisabled: true }
  70. const rateLimiter = new this.RateLimiter.RateLimiter('some-limit', {
  71. points: 20,
  72. subnetPoints: 200,
  73. duration: 60,
  74. })
  75. expect(rateLimiter._subnetRateLimiter).to.be.undefined
  76. })
  77. it('should be undefined when subnetPoints are not passed as an option', function () {
  78. const rateLimiter = new this.RateLimiter.RateLimiter('some-limit', {
  79. points: 20,
  80. duration: 60,
  81. })
  82. expect(rateLimiter._subnetRateLimiter).to.be.undefined
  83. })
  84. })
  85. })