]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/tracker.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / tracker.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
cc43831a 2
cc43831a 3import 'mocha'
d23dd9fb 4import * as magnetUtil from 'magnet-uri'
cc43831a 5import * as WebTorrent from 'webtorrent'
254d3579 6import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
cc43831a
C
7
8describe('Test tracker', function () {
254d3579 9 let server: PeerTubeServer
cc43831a
C
10 let badMagnet: string
11 let goodMagnet: string
12
13 before(async function () {
14 this.timeout(60000)
254d3579 15 server = await createSingleServer(1)
cc43831a
C
16 await setAccessTokensToServers([ server ])
17
18 {
89d241a7
C
19 const { uuid } = await server.videos.upload()
20 const video = await server.videos.get({ id: uuid })
cc43831a
C
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
31b6ddf8 30 it('Should succeed with the correct infohash', function (done) {
cc43831a
C
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
bdd428a6 39 if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
cc43831a
C
40 })
41
42 torrent.on('done', done)
43 })
44
31b6ddf8
C
45 it('Should disable the tracker', function (done) {
46 this.timeout(20000)
47
cf5d7d9d
C
48 const errCb = () => done(new Error('Tracker is enabled'))
49
31b6ddf8 50 killallServers([ server ])
254d3579 51 .then(() => server.run({ tracker: { enabled: false } }))
31b6ddf8
C
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
cf5d7d9d
C
60 if (message.includes('disabled ')) {
61 torrent.off('done', errCb)
62
63 return done()
64 }
31b6ddf8
C
65 })
66
cf5d7d9d 67 torrent.on('done', errCb)
31b6ddf8
C
68 })
69 })
70
db48de85
C
71 it('Should return an error when adding an incorrect infohash', function (done) {
72 this.timeout(20000)
73
74 killallServers([ server ])
254d3579 75 .then(() => server.run())
db48de85
C
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
7c3b7976
C
103 after(async function () {
104 await cleanupTests([ server ])
cc43831a
C
105 })
106})