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