tracker.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* tslint:disable:no-unused-expression */
  2. import * as magnetUtil from 'magnet-uri'
  3. import 'mocha'
  4. import {
  5. cleanupTests,
  6. flushAndRunServer,
  7. getVideo,
  8. killallServers,
  9. reRunServer,
  10. ServerInfo,
  11. uploadVideo
  12. } from '../../../../shared/extra-utils'
  13. import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
  14. import { VideoDetails } from '../../../../shared/models/videos'
  15. import * as WebTorrent from 'webtorrent'
  16. describe('Test tracker', function () {
  17. let server: ServerInfo
  18. let badMagnet: string
  19. let goodMagnet: string
  20. before(async function () {
  21. this.timeout(60000)
  22. server = await flushAndRunServer(1)
  23. await setAccessTokensToServers([ server ])
  24. {
  25. const res = await uploadVideo(server.url, server.accessToken, {})
  26. const videoUUID = res.body.video.uuid
  27. const resGet = await getVideo(server.url, videoUUID)
  28. const video: VideoDetails = resGet.body
  29. goodMagnet = video.files[0].magnetUri
  30. const parsed = magnetUtil.decode(goodMagnet)
  31. parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
  32. badMagnet = magnetUtil.encode(parsed)
  33. }
  34. })
  35. it('Should return an error when adding an incorrect infohash', function (done) {
  36. this.timeout(10000)
  37. const webtorrent = new WebTorrent()
  38. const torrent = webtorrent.add(badMagnet)
  39. torrent.on('error', done)
  40. torrent.on('warning', warn => {
  41. const message = typeof warn === 'string' ? warn : warn.message
  42. if (message.indexOf('Unknown infoHash ') !== -1) return done()
  43. })
  44. torrent.on('done', () => done(new Error('No error on infohash')))
  45. })
  46. it('Should succeed with the correct infohash', function (done) {
  47. this.timeout(10000)
  48. const webtorrent = new WebTorrent()
  49. const torrent = webtorrent.add(goodMagnet)
  50. torrent.on('error', done)
  51. torrent.on('warning', warn => {
  52. const message = typeof warn === 'string' ? warn : warn.message
  53. if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
  54. })
  55. torrent.on('done', done)
  56. })
  57. it('Should disable the tracker', function (done) {
  58. this.timeout(20000)
  59. killallServers([ server ])
  60. reRunServer(server, { tracker: { enabled: false } })
  61. .then(() => {
  62. const webtorrent = new WebTorrent()
  63. const torrent = webtorrent.add(goodMagnet)
  64. torrent.on('error', done)
  65. torrent.on('warning', warn => {
  66. const message = typeof warn === 'string' ? warn : warn.message
  67. if (message.indexOf('disabled ') !== -1) return done()
  68. })
  69. torrent.on('done', () => done(new Error('Tracker is enabled')))
  70. })
  71. })
  72. after(async function () {
  73. await cleanupTests([ server ])
  74. })
  75. })