reverse-proxy.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils'
  5. import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
  6. const expect = chai.expect
  7. describe('Test application behind a reverse proxy', function () {
  8. let server = null
  9. let videoId
  10. before(async function () {
  11. this.timeout(30000)
  12. const config = {
  13. rates_limit: {
  14. api: {
  15. max: 50,
  16. window: 5000
  17. },
  18. signup: {
  19. max: 3,
  20. window: 5000
  21. },
  22. login: {
  23. max: 20
  24. }
  25. },
  26. signup: {
  27. limit: 20
  28. }
  29. }
  30. server = await flushAndRunServer(1, config)
  31. await setAccessTokensToServers([ server ])
  32. const { body } = await uploadVideo(server.url, server.accessToken, {})
  33. videoId = body.video.uuid
  34. })
  35. it('Should view a video only once with the same IP by default', async function () {
  36. this.timeout(20000)
  37. await viewVideo(server.url, videoId)
  38. await viewVideo(server.url, videoId)
  39. // Wait the repeatable job
  40. await wait(8000)
  41. const { body } = await getVideo(server.url, videoId)
  42. expect(body.views).to.equal(1)
  43. })
  44. it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
  45. this.timeout(20000)
  46. await viewVideo(server.url, videoId, 204, '0.0.0.1,127.0.0.1')
  47. await viewVideo(server.url, videoId, 204, '0.0.0.2,127.0.0.1')
  48. // Wait the repeatable job
  49. await wait(8000)
  50. const { body } = await getVideo(server.url, videoId)
  51. expect(body.views).to.equal(3)
  52. })
  53. it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
  54. this.timeout(20000)
  55. await viewVideo(server.url, videoId, 204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1')
  56. await viewVideo(server.url, videoId, 204, '0.0.0.5,0.0.0.3,127.0.0.1')
  57. // Wait the repeatable job
  58. await wait(8000)
  59. const { body } = await getVideo(server.url, videoId)
  60. expect(body.views).to.equal(4)
  61. })
  62. it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
  63. this.timeout(20000)
  64. await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.6,127.0.0.1')
  65. await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.7,127.0.0.1')
  66. // Wait the repeatable job
  67. await wait(8000)
  68. const { body } = await getVideo(server.url, videoId)
  69. expect(body.views).to.equal(6)
  70. })
  71. it('Should rate limit logins', async function () {
  72. const user = { username: 'root', password: 'fail' }
  73. for (let i = 0; i < 19; i++) {
  74. await userLogin(server, user, 400)
  75. }
  76. await userLogin(server, user, 429)
  77. })
  78. it('Should rate limit signup', async function () {
  79. for (let i = 0; i < 3; i++) {
  80. await registerUser(server.url, 'test' + i, 'password')
  81. }
  82. await registerUser(server.url, 'test42', 'password', 429)
  83. })
  84. it('Should not rate limit failed signup', async function () {
  85. this.timeout(30000)
  86. await wait(7000)
  87. for (let i = 0; i < 3; i++) {
  88. await registerUser(server.url, 'test' + i, 'password', 409)
  89. }
  90. await registerUser(server.url, 'test43', 'password', 204)
  91. })
  92. it('Should rate limit API calls', async function () {
  93. this.timeout(30000)
  94. await wait(7000)
  95. for (let i = 0; i < 100; i++) {
  96. try {
  97. await getVideo(server.url, videoId)
  98. } catch {
  99. // don't care if it fails
  100. }
  101. }
  102. await getVideo(server.url, videoId, 429)
  103. })
  104. after(async function () {
  105. await cleanupTests([ server ])
  106. })
  107. })