PeerTube/server/tests/api/server/tracker.ts

122 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
2018-08-14 04:00:03 -05:00
import * as magnetUtil from 'magnet-uri'
import 'mocha'
2019-04-24 08:10:37 -05:00
import {
cleanupTests,
flushAndRunServer,
getVideo,
killallServers,
reRunServer,
ServerInfo,
uploadVideo
} from '../../../../shared/extra-utils'
import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
2018-08-14 04:00:03 -05:00
import { VideoDetails } from '../../../../shared/models/videos'
import * as WebTorrent from 'webtorrent'
describe('Test tracker', function () {
let server: ServerInfo
let badMagnet: string
let goodMagnet: string
before(async function () {
this.timeout(60000)
2019-04-24 03:53:40 -05:00
server = await flushAndRunServer(1)
2018-08-14 04:00:03 -05:00
await setAccessTokensToServers([ server ])
{
const res = await uploadVideo(server.url, server.accessToken, {})
const videoUUID = res.body.video.uuid
const resGet = await getVideo(server.url, videoUUID)
const video: VideoDetails = resGet.body
goodMagnet = video.files[0].magnetUri
const parsed = magnetUtil.decode(goodMagnet)
parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
badMagnet = magnetUtil.encode(parsed)
}
})
2019-04-10 02:23:18 -05:00
it('Should succeed with the correct infohash', function (done) {
2018-08-14 04:00:03 -05:00
this.timeout(10000)
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
2020-02-28 09:03:39 -06:00
if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
2018-08-14 04:00:03 -05:00
})
torrent.on('done', done)
})
2019-04-10 02:23:18 -05:00
it('Should disable the tracker', function (done) {
this.timeout(20000)
2020-03-20 10:17:14 -05:00
const errCb = () => done(new Error('Tracker is enabled'))
2019-04-10 02:23:18 -05:00
killallServers([ server ])
2020-06-25 09:27:35 -05:00
2019-04-10 02:23:18 -05:00
reRunServer(server, { tracker: { enabled: false } })
.then(() => {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
2020-03-20 10:17:14 -05:00
if (message.includes('disabled ')) {
torrent.off('done', errCb)
return done()
}
2019-04-10 02:23:18 -05:00
})
2020-03-20 10:17:14 -05:00
torrent.on('done', errCb)
2019-04-10 02:23:18 -05:00
})
})
2020-06-25 09:27:35 -05:00
it('Should return an error when adding an incorrect infohash', function (done) {
this.timeout(20000)
killallServers([ server ])
reRunServer(server)
.then(() => {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(badMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
if (message.includes('Unknown infoHash ')) return done()
})
torrent.on('done', () => done(new Error('No error on infohash')))
})
})
it('Should block the IP after the failed infohash', function (done) {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
if (message.includes('Unsupported tracker protocol')) return done()
})
})
2019-04-24 08:10:37 -05:00
after(async function () {
await cleanupTests([ server ])
2018-08-14 04:00:03 -05:00
})
})