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