Cleanup tests
[oweals/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, flushAndRunServer, 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     server = await flushAndRunServer(1)
18     await setAccessTokensToServers([ server ])
19
20     {
21       const res = await uploadVideo(server.url, server.accessToken, {})
22       const videoUUID = res.body.video.uuid
23
24       const resGet = await getVideo(server.url, videoUUID)
25       const video: VideoDetails = resGet.body
26       goodMagnet = video.files[0].magnetUri
27
28       const parsed = magnetUtil.decode(goodMagnet)
29       parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
30
31       badMagnet = magnetUtil.encode(parsed)
32     }
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
39     const torrent = webtorrent.add(badMagnet)
40
41     torrent.on('error', done)
42     torrent.on('warning', warn => {
43       const message = typeof warn === 'string' ? warn : warn.message
44       if (message.indexOf('Unknown infoHash ') !== -1) return done()
45     })
46
47     torrent.on('done', () => done(new Error('No error on infohash')))
48   })
49
50   it('Should succeed with the correct infohash', function (done) {
51     this.timeout(10000)
52     const webtorrent = new WebTorrent()
53
54     const torrent = webtorrent.add(goodMagnet)
55
56     torrent.on('error', done)
57     torrent.on('warning', warn => {
58       const message = typeof warn === 'string' ? warn : warn.message
59       if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
60     })
61
62     torrent.on('done', done)
63   })
64
65   it('Should disable the tracker', function (done) {
66     this.timeout(20000)
67
68     killallServers([ server ])
69     reRunServer(server, { tracker: { enabled: false } })
70       .then(() => {
71         const webtorrent = new WebTorrent()
72
73         const torrent = webtorrent.add(goodMagnet)
74
75         torrent.on('error', done)
76         torrent.on('warning', warn => {
77           const message = typeof warn === 'string' ? warn : warn.message
78           if (message.indexOf('disabled ') !== -1) return done()
79         })
80
81         torrent.on('done', () => done(new Error('Tracker is enabled')))
82       })
83   })
84
85   after(function () {
86     killallServers([ server ])
87   })
88 })