]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/tracker.ts
Cleanup tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / tracker.ts
CommitLineData
cc43831a
C
1/* tslint:disable:no-unused-expression */
2
3import * as magnetUtil from 'magnet-uri'
cc43831a 4import 'mocha'
210feb6c 5import { getVideo, killallServers, reRunServer, flushAndRunServer, ServerInfo, uploadVideo } from '../../../../shared/extra-utils'
94565d52 6import { flushTests, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
cc43831a
C
7import { VideoDetails } from '../../../../shared/models/videos'
8import * as WebTorrent from 'webtorrent'
9
10describe('Test tracker', function () {
11 let server: ServerInfo
12 let badMagnet: string
13 let goodMagnet: string
14
15 before(async function () {
16 this.timeout(60000)
210feb6c 17 server = await flushAndRunServer(1)
cc43831a
C
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
31b6ddf8 35 it('Should return an error when adding an incorrect infohash', function (done) {
cc43831a
C
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
31b6ddf8 50 it('Should succeed with the correct infohash', function (done) {
cc43831a
C
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
31b6ddf8
C
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
210feb6c 85 after(function () {
cc43831a
C
86 killallServers([ server ])
87 })
88})