1 /* tslint:disable:no-unused-expression */
3 import * as magnetUtil from 'magnet-uri'
13 } from '../../../../shared/extra-utils'
14 import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
15 import { VideoDetails } from '../../../../shared/models/videos'
16 import * as WebTorrent from 'webtorrent'
18 describe('Test tracker', function () {
19 let server: ServerInfo
21 let goodMagnet: string
23 before(async function () {
25 server = await flushAndRunServer(1)
26 await setAccessTokensToServers([ server ])
29 const res = await uploadVideo(server.url, server.accessToken, {})
30 const videoUUID = res.body.video.uuid
32 const resGet = await getVideo(server.url, videoUUID)
33 const video: VideoDetails = resGet.body
34 goodMagnet = video.files[0].magnetUri
36 const parsed = magnetUtil.decode(goodMagnet)
37 parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
39 badMagnet = magnetUtil.encode(parsed)
43 it('Should return an error when adding an incorrect infohash', function (done) {
45 const webtorrent = new WebTorrent()
47 const torrent = webtorrent.add(badMagnet)
49 torrent.on('error', done)
50 torrent.on('warning', warn => {
51 const message = typeof warn === 'string' ? warn : warn.message
52 if (message.indexOf('Unknown infoHash ') !== -1) return done()
55 torrent.on('done', () => done(new Error('No error on infohash')))
58 it('Should succeed with the correct infohash', function (done) {
60 const webtorrent = new WebTorrent()
62 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('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
70 torrent.on('done', done)
73 it('Should disable the tracker', function (done) {
76 killallServers([ server ])
77 reRunServer(server, { tracker: { enabled: false } })
79 const webtorrent = new WebTorrent()
81 const torrent = webtorrent.add(goodMagnet)
83 torrent.on('error', done)
84 torrent.on('warning', warn => {
85 const message = typeof warn === 'string' ? warn : warn.message
86 if (message.indexOf('disabled ') !== -1) return done()
89 torrent.on('done', () => done(new Error('Tracker is enabled')))
93 after(async function () {
94 await cleanupTests([ server ])