proxy.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { expectNotStartWith, expectStartWith, FIXTURE_URLS, MockProxy } from '@server/tests/shared'
  4. import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
  5. import { HttpStatusCode, VideoPrivacy } from '@shared/models'
  6. import {
  7. cleanupTests,
  8. createMultipleServers,
  9. doubleFollow,
  10. ObjectStorageCommand,
  11. PeerTubeServer,
  12. setAccessTokensToServers,
  13. setDefaultVideoChannel,
  14. waitJobs
  15. } from '@shared/server-commands'
  16. describe('Test proxy', function () {
  17. let servers: PeerTubeServer[] = []
  18. let proxy: MockProxy
  19. const goodEnv = { HTTP_PROXY: '' }
  20. const badEnv = { HTTP_PROXY: 'http://localhost:9000' }
  21. before(async function () {
  22. this.timeout(120000)
  23. proxy = new MockProxy()
  24. const proxyPort = await proxy.initialize()
  25. servers = await createMultipleServers(2)
  26. goodEnv.HTTP_PROXY = 'http://localhost:' + proxyPort
  27. await setAccessTokensToServers(servers)
  28. await setDefaultVideoChannel(servers)
  29. await doubleFollow(servers[0], servers[1])
  30. })
  31. describe('Federation', function () {
  32. it('Should succeed federation with the appropriate proxy config', async function () {
  33. this.timeout(40000)
  34. await servers[0].kill()
  35. await servers[0].run({}, { env: goodEnv })
  36. await servers[0].videos.quickUpload({ name: 'video 1' })
  37. await waitJobs(servers)
  38. for (const server of servers) {
  39. const { total, data } = await server.videos.list()
  40. expect(total).to.equal(1)
  41. expect(data).to.have.lengthOf(1)
  42. }
  43. })
  44. it('Should fail federation with a wrong proxy config', async function () {
  45. this.timeout(40000)
  46. await servers[0].kill()
  47. await servers[0].run({}, { env: badEnv })
  48. await servers[0].videos.quickUpload({ name: 'video 2' })
  49. await waitJobs(servers)
  50. {
  51. const { total, data } = await servers[0].videos.list()
  52. expect(total).to.equal(2)
  53. expect(data).to.have.lengthOf(2)
  54. }
  55. {
  56. const { total, data } = await servers[1].videos.list()
  57. expect(total).to.equal(1)
  58. expect(data).to.have.lengthOf(1)
  59. }
  60. })
  61. })
  62. describe('Videos import', async function () {
  63. function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
  64. return servers[0].imports.importVideo({
  65. attributes: {
  66. name: 'video import',
  67. channelId: servers[0].store.channel.id,
  68. privacy: VideoPrivacy.PUBLIC,
  69. targetUrl: FIXTURE_URLS.peertube_long
  70. },
  71. expectedStatus
  72. })
  73. }
  74. it('Should succeed import with the appropriate proxy config', async function () {
  75. this.timeout(120000)
  76. await servers[0].kill()
  77. await servers[0].run({}, { env: goodEnv })
  78. await quickImport()
  79. await waitJobs(servers)
  80. const { total, data } = await servers[0].videos.list()
  81. expect(total).to.equal(3)
  82. expect(data).to.have.lengthOf(3)
  83. })
  84. it('Should fail import with a wrong proxy config', async function () {
  85. this.timeout(120000)
  86. await servers[0].kill()
  87. await servers[0].run({}, { env: badEnv })
  88. await quickImport(HttpStatusCode.BAD_REQUEST_400)
  89. })
  90. })
  91. describe('Object storage', function () {
  92. if (areMockObjectStorageTestsDisabled()) return
  93. before(async function () {
  94. this.timeout(30000)
  95. await ObjectStorageCommand.prepareDefaultMockBuckets()
  96. })
  97. it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
  98. this.timeout(120000)
  99. await servers[0].kill()
  100. await servers[0].run(ObjectStorageCommand.getDefaultMockConfig(), { env: goodEnv })
  101. const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
  102. await waitJobs(servers)
  103. const video = await servers[0].videos.get({ id: uuid })
  104. expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
  105. })
  106. it('Should fail to upload to object storage with a wrong proxy config', async function () {
  107. this.timeout(120000)
  108. await servers[0].kill()
  109. await servers[0].run(ObjectStorageCommand.getDefaultMockConfig(), { env: badEnv })
  110. const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
  111. await waitJobs(servers)
  112. const video = await servers[0].videos.get({ id: uuid })
  113. expectNotStartWith(video.files[0].fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
  114. })
  115. })
  116. after(async function () {
  117. await proxy.terminate()
  118. await cleanupTests(servers)
  119. })
  120. })