]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/tracker.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / tracker.ts
index 25ca00029bc9fc8d94731b27be1b9daf257f2257..f597ac60ce84504a4857976d78759a033340e050 100644 (file)
@@ -1,30 +1,23 @@
-/* tslint:disable:no-unused-expression */
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
 
-import * as magnetUtil from 'magnet-uri'
 import 'mocha'
-import { getVideo, killallServers, runServer, ServerInfo, uploadVideo } from '../../../../shared/utils'
-import { flushTests, setAccessTokensToServers } from '../../../../shared/utils/index'
-import { VideoDetails } from '../../../../shared/models/videos'
+import * as magnetUtil from 'magnet-uri'
 import * as WebTorrent from 'webtorrent'
+import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
 
 describe('Test tracker', function () {
-  let server: ServerInfo
+  let server: PeerTubeServer
   let badMagnet: string
   let goodMagnet: string
 
   before(async function () {
     this.timeout(60000)
-
-    await flushTests()
-    server = await runServer(1)
+    server = await createSingleServer(1)
     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
+      const { uuid } = await server.videos.upload()
+      const video = await server.videos.get({ id: uuid })
       goodMagnet = video.files[0].magnetUri
 
       const parsed = magnetUtil.decode(goodMagnet)
@@ -34,23 +27,68 @@ describe('Test tracker', function () {
     }
   })
 
-  it('Should return an error when adding an incorrect infohash', done => {
+  it('Should succeed with the correct infohash', function (done) {
     this.timeout(10000)
     const webtorrent = new WebTorrent()
 
-    const torrent = webtorrent.add(badMagnet)
+    const torrent = webtorrent.add(goodMagnet)
 
     torrent.on('error', done)
     torrent.on('warning', warn => {
       const message = typeof warn === 'string' ? warn : warn.message
-      if (message.indexOf('Unknown infoHash ') !== -1) return done()
+      if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
     })
 
-    torrent.on('done', () => done(new Error('No error on infohash')))
+    torrent.on('done', done)
   })
 
-  it('Should succeed with the correct infohash', done => {
-    this.timeout(10000)
+  it('Should disable the tracker', function (done) {
+    this.timeout(20000)
+
+    const errCb = () => done(new Error('Tracker is enabled'))
+
+    killallServers([ server ])
+      .then(() => server.run({ 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
+          if (message.includes('disabled ')) {
+            torrent.off('done', errCb)
+
+            return done()
+          }
+        })
+
+        torrent.on('done', errCb)
+      })
+  })
+
+  it('Should return an error when adding an incorrect infohash', function (done) {
+    this.timeout(20000)
+
+    killallServers([ server ])
+      .then(() => server.run())
+      .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)
@@ -58,13 +96,11 @@ describe('Test tracker', function () {
     torrent.on('error', done)
     torrent.on('warning', warn => {
       const message = typeof warn === 'string' ? warn : warn.message
-      if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
+      if (message.includes('Unsupported tracker protocol')) return done()
     })
-
-    torrent.on('done', done)
   })
 
   after(async function () {
-    killallServers([ server ])
+    await cleanupTests([ server ])
   })
 })