]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/tracker.ts
Fix missing wait jobs
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / tracker.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
2
3 import magnetUtil from 'magnet-uri'
4 import WebTorrent from 'webtorrent'
5 import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
6
7 describe('Test tracker', function () {
8 let server: PeerTubeServer
9 let badMagnet: string
10 let goodMagnet: string
11
12 before(async function () {
13 this.timeout(60000)
14 server = await createSingleServer(1)
15 await setAccessTokensToServers([ server ])
16
17 {
18 const { uuid } = await server.videos.upload()
19 const video = await server.videos.get({ id: uuid })
20 goodMagnet = video.files[0].magnetUri
21
22 const parsed = magnetUtil.decode(goodMagnet)
23 parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
24
25 badMagnet = magnetUtil.encode(parsed)
26 }
27 })
28
29 it('Should succeed with the correct infohash', function (done) {
30 this.timeout(10000)
31 const webtorrent = new WebTorrent()
32
33 const torrent = webtorrent.add(goodMagnet)
34
35 torrent.on('error', done)
36 torrent.on('warning', warn => {
37 const message = typeof warn === 'string' ? warn : warn.message
38 if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
39 })
40
41 torrent.on('done', done)
42 })
43
44 it('Should disable the tracker', function (done) {
45 this.timeout(20000)
46
47 const errCb = () => done(new Error('Tracker is enabled'))
48
49 killallServers([ server ])
50 .then(() => server.run({ tracker: { enabled: false } }))
51 .then(() => {
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.includes('disabled ')) {
60 torrent.off('done', errCb)
61
62 return done()
63 }
64 })
65
66 torrent.on('done', errCb)
67 })
68 })
69
70 it('Should return an error when adding an incorrect infohash', function (done) {
71 this.timeout(20000)
72
73 killallServers([ server ])
74 .then(() => server.run())
75 .then(() => {
76 const webtorrent = new WebTorrent()
77
78 const torrent = webtorrent.add(badMagnet)
79
80 torrent.on('error', done)
81 torrent.on('warning', warn => {
82 const message = typeof warn === 'string' ? warn : warn.message
83 if (message.includes('Unknown infoHash ')) return done()
84 })
85
86 torrent.on('done', () => done(new Error('No error on infohash')))
87 })
88 })
89
90 it('Should block the IP after the failed infohash', function (done) {
91 const webtorrent = new WebTorrent()
92
93 const torrent = webtorrent.add(goodMagnet)
94
95 torrent.on('error', done)
96 torrent.on('warning', warn => {
97 const message = typeof warn === 'string' ? warn : warn.message
98 if (message.includes('Unsupported tracker protocol')) return done()
99 })
100 })
101
102 after(async function () {
103 await cleanupTests([ server ])
104 })
105 })