]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/tracker.ts
Fix tests
[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 const webtorrent = new WebTorrent()
31
32 const torrent = webtorrent.add(goodMagnet)
33
34 torrent.on('error', done)
35 torrent.on('warning', warn => {
36 const message = typeof warn === 'string' ? warn : warn.message
37 if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
38 })
39
40 torrent.on('done', done)
41 })
42
43 it('Should disable the tracker', function (done) {
44 this.timeout(20000)
45
46 const errCb = () => done(new Error('Tracker is enabled'))
47
48 killallServers([ server ])
49 .then(() => server.run({ tracker: { enabled: false } }))
50 .then(() => {
51 const webtorrent = new WebTorrent()
52
53 const torrent = webtorrent.add(goodMagnet)
54
55 torrent.on('error', done)
56 torrent.on('warning', warn => {
57 const message = typeof warn === 'string' ? warn : warn.message
58 if (message.includes('disabled ')) {
59 torrent.off('done', errCb)
60
61 return done()
62 }
63 })
64
65 torrent.on('done', errCb)
66 })
67 })
68
69 it('Should return an error when adding an incorrect infohash', function (done) {
70 this.timeout(20000)
71
72 killallServers([ server ])
73 .then(() => server.run())
74 .then(() => {
75 const webtorrent = new WebTorrent()
76
77 const torrent = webtorrent.add(badMagnet)
78
79 torrent.on('error', done)
80 torrent.on('warning', warn => {
81 const message = typeof warn === 'string' ? warn : warn.message
82 if (message.includes('Unknown infoHash ')) return done()
83 })
84
85 torrent.on('done', () => done(new Error('No error on infohash')))
86 })
87 })
88
89 it('Should block the IP after the failed infohash', function (done) {
90 const webtorrent = new WebTorrent()
91
92 const torrent = webtorrent.add(goodMagnet)
93
94 torrent.on('error', done)
95 torrent.on('warning', warn => {
96 const message = typeof warn === 'string' ? warn : warn.message
97 if (message.includes('Unsupported tracker protocol')) return done()
98 })
99 })
100
101 after(async function () {
102 await cleanupTests([ server ])
103 })
104 })