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