]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */ |
cc43831a C |
2 | |
3 | import * as magnetUtil from 'magnet-uri' | |
cc43831a | 4 | import 'mocha' |
7c3b7976 C |
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' | |
cc43831a C |
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) | |
210feb6c | 25 | server = await flushAndRunServer(1) |
cc43831a C |
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 | ||
31b6ddf8 | 43 | it('Should succeed with the correct infohash', function (done) { |
cc43831a C |
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 | |
bdd428a6 | 52 | if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash')) |
cc43831a C |
53 | }) |
54 | ||
55 | torrent.on('done', done) | |
56 | }) | |
57 | ||
31b6ddf8 C |
58 | it('Should disable the tracker', function (done) { |
59 | this.timeout(20000) | |
60 | ||
cf5d7d9d C |
61 | const errCb = () => done(new Error('Tracker is enabled')) |
62 | ||
31b6ddf8 | 63 | killallServers([ server ]) |
9293139f | 64 | .then(() => reRunServer(server, { tracker: { enabled: false } })) |
31b6ddf8 C |
65 | .then(() => { |
66 | const webtorrent = new WebTorrent() | |
67 | ||
68 | const torrent = webtorrent.add(goodMagnet) | |
69 | ||
70 | torrent.on('error', done) | |
71 | torrent.on('warning', warn => { | |
72 | const message = typeof warn === 'string' ? warn : warn.message | |
cf5d7d9d C |
73 | if (message.includes('disabled ')) { |
74 | torrent.off('done', errCb) | |
75 | ||
76 | return done() | |
77 | } | |
31b6ddf8 C |
78 | }) |
79 | ||
cf5d7d9d | 80 | torrent.on('done', errCb) |
31b6ddf8 C |
81 | }) |
82 | }) | |
83 | ||
db48de85 C |
84 | it('Should return an error when adding an incorrect infohash', function (done) { |
85 | this.timeout(20000) | |
86 | ||
87 | killallServers([ server ]) | |
9293139f | 88 | .then(() => reRunServer(server)) |
db48de85 C |
89 | .then(() => { |
90 | const webtorrent = new WebTorrent() | |
91 | ||
92 | const torrent = webtorrent.add(badMagnet) | |
93 | ||
94 | torrent.on('error', done) | |
95 | torrent.on('warning', warn => { | |
96 | const message = typeof warn === 'string' ? warn : warn.message | |
97 | if (message.includes('Unknown infoHash ')) return done() | |
98 | }) | |
99 | ||
100 | torrent.on('done', () => done(new Error('No error on infohash'))) | |
101 | }) | |
102 | }) | |
103 | ||
104 | it('Should block the IP after the failed infohash', function (done) { | |
105 | const webtorrent = new WebTorrent() | |
106 | ||
107 | const torrent = webtorrent.add(goodMagnet) | |
108 | ||
109 | torrent.on('error', done) | |
110 | torrent.on('warning', warn => { | |
111 | const message = typeof warn === 'string' ? warn : warn.message | |
112 | if (message.includes('Unsupported tracker protocol')) return done() | |
113 | }) | |
114 | }) | |
115 | ||
7c3b7976 C |
116 | after(async function () { |
117 | await cleanupTests([ server ]) | |
cc43831a C |
118 | }) |
119 | }) |