From 843aa7ba0312e7180e7bbae147e32ee60e70d9ba Mon Sep 17 00:00:00 2001
From: Chocobozzz <florian.bigard@gmail.com>
Date: Mon, 1 May 2017 19:04:29 +0200
Subject: Server: add tests for video blacklists

---
 server/tests/api/check-params/index.js            |   1 +
 server/tests/api/check-params/video-abuses.js     |   8 +-
 server/tests/api/check-params/video-blacklists.js | 123 +++++++++++++++++++
 server/tests/api/index.js                         |   2 +
 server/tests/api/video-abuse.js                   |   2 +-
 server/tests/api/video-blacklist.js               | 138 ++++++++++++++++++++++
 server/tests/utils/video-abuses.js                |   4 +-
 server/tests/utils/video-blacklists.js            |  29 +++++
 8 files changed, 300 insertions(+), 7 deletions(-)
 create mode 100644 server/tests/api/check-params/video-blacklists.js
 create mode 100644 server/tests/api/video-blacklist.js
 create mode 100644 server/tests/utils/video-blacklists.js

(limited to 'server')

diff --git a/server/tests/api/check-params/index.js b/server/tests/api/check-params/index.js
index d0824f08a..527ab65a9 100644
--- a/server/tests/api/check-params/index.js
+++ b/server/tests/api/check-params/index.js
@@ -7,3 +7,4 @@ require('./users')
 require('./requests')
 require('./videos')
 require('./video-abuses')
+require('./video-blacklists')
diff --git a/server/tests/api/check-params/video-abuses.js b/server/tests/api/check-params/video-abuses.js
index 6dc5a7090..8c520aab4 100644
--- a/server/tests/api/check-params/video-abuses.js
+++ b/server/tests/api/check-params/video-abuses.js
@@ -129,7 +129,7 @@ describe('Test video abuses API validators', function () {
     const basePath = '/api/v1/videos/'
 
     it('Should fail with nothing', function (done) {
-      const path = basePath + server.video + '/abuse'
+      const path = basePath + server.video.id + '/abuse'
       const data = {}
       requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
     })
@@ -142,7 +142,7 @@ describe('Test video abuses API validators', function () {
 
     it('Should fail with a non authenticated user', function (done) {
       const data = {}
-      const path = basePath + server.video + '/abuse'
+      const path = basePath + server.video.id + '/abuse'
       requestsUtils.makePostBodyRequest(server.url, path, 'hello', data, done, 401)
     })
 
@@ -150,7 +150,7 @@ describe('Test video abuses API validators', function () {
       const data = {
         reason: 'h'
       }
-      const path = basePath + server.video + '/abuse'
+      const path = basePath + server.video.id + '/abuse'
       requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
     })
 
@@ -161,7 +161,7 @@ describe('Test video abuses API validators', function () {
                 '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' +
                 '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
       }
-      const path = basePath + server.video + '/abuse'
+      const path = basePath + server.video.id + '/abuse'
       requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
     })
   })
diff --git a/server/tests/api/check-params/video-blacklists.js b/server/tests/api/check-params/video-blacklists.js
new file mode 100644
index 000000000..a39ab0cfa
--- /dev/null
+++ b/server/tests/api/check-params/video-blacklists.js
@@ -0,0 +1,123 @@
+/* eslint-disable no-unused-expressions */
+
+'use strict'
+
+const series = require('async/series')
+
+const loginUtils = require('../../utils/login')
+const requestsUtils = require('../../utils/requests')
+const serversUtils = require('../../utils/servers')
+const usersUtils = require('../../utils/users')
+const videosUtils = require('../../utils/videos')
+
+describe('Test video blacklists API validators', function () {
+  let server = null
+  let userAccessToken = null
+
+  // ---------------------------------------------------------------
+
+  before(function (done) {
+    this.timeout(20000)
+
+    series([
+      function (next) {
+        serversUtils.flushTests(next)
+      },
+      function (next) {
+        serversUtils.runServer(1, function (server1) {
+          server = server1
+
+          next()
+        })
+      },
+      function (next) {
+        loginUtils.loginAndGetAccessToken(server, function (err, token) {
+          if (err) throw err
+          server.accessToken = token
+
+          next()
+        })
+      },
+      function (next) {
+        const username = 'user1'
+        const password = 'my super password'
+
+        usersUtils.createUser(server.url, server.accessToken, username, password, next)
+      },
+      function (next) {
+        const user = {
+          username: 'user1',
+          password: 'my super password'
+        }
+
+        loginUtils.getUserAccessToken(server, user, function (err, accessToken) {
+          if (err) throw err
+
+          userAccessToken = accessToken
+
+          next()
+        })
+      },
+      // Upload a video
+      function (next) {
+        const videoAttributes = {}
+        videosUtils.uploadVideo(server.url, server.accessToken, videoAttributes, next)
+      },
+      function (next) {
+        videosUtils.getVideosList(server.url, function (err, res) {
+          if (err) throw err
+
+          const videos = res.body.data
+          server.video = videos[0]
+
+          next()
+        })
+      }
+    ], done)
+  })
+
+  describe('When adding a video in blacklist', function () {
+    const basePath = '/api/v1/videos/'
+
+    it('Should fail with nothing', function (done) {
+      const path = basePath + server.video + '/blacklist'
+      const data = {}
+      requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
+    })
+
+    it('Should fail with a wrong video', function (done) {
+      const wrongPath = '/api/v1/videos/blabla/blacklist'
+      const data = {}
+      requestsUtils.makePostBodyRequest(server.url, wrongPath, server.accessToken, data, done)
+    })
+
+    it('Should fail with a non authenticated user', function (done) {
+      const data = {}
+      const path = basePath + server.video + '/blacklist'
+      requestsUtils.makePostBodyRequest(server.url, path, 'hello', data, done, 401)
+    })
+
+    it('Should fail with a non admin user', function (done) {
+      const data = {}
+      const path = basePath + server.video + '/blacklist'
+      requestsUtils.makePostBodyRequest(server.url, path, userAccessToken, data, done, 403)
+    })
+
+    it('Should fail with a local video', function (done) {
+      const data = {}
+      const path = basePath + server.video.id + '/blacklist'
+      requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 403)
+    })
+  })
+
+  after(function (done) {
+    process.kill(-server.app.pid)
+
+    // Keep the logs if the test failed
+    if (this.ok) {
+      serversUtils.flushTests(done)
+    } else {
+      done()
+    }
+  })
+})
diff --git a/server/tests/api/index.js b/server/tests/api/index.js
index fcd12617e..7ae18f674 100644
--- a/server/tests/api/index.js
+++ b/server/tests/api/index.js
@@ -6,6 +6,8 @@ require('./check-params')
 require('./friends-basic')
 require('./users')
 require('./single-pod')
+require('./video-abuse')
+require('./video-blacklist')
 require('./multiple-pods')
 require('./requests')
 require('./friends-advanced')
diff --git a/server/tests/api/video-abuse.js b/server/tests/api/video-abuse.js
index 7db067585..1f64ec861 100644
--- a/server/tests/api/video-abuse.js
+++ b/server/tests/api/video-abuse.js
@@ -38,7 +38,7 @@ describe('Test video abuses', function () {
           })
         }, next)
       },
-      // Pod 1 make friends too
+      // Pod 1 makes friend with pod 2
       function (next) {
         const server = servers[0]
         podsUtils.makeFriends(server.url, server.accessToken, next)
diff --git a/server/tests/api/video-blacklist.js b/server/tests/api/video-blacklist.js
new file mode 100644
index 000000000..c95fc17cb
--- /dev/null
+++ b/server/tests/api/video-blacklist.js
@@ -0,0 +1,138 @@
+/* eslint-disable no-unused-expressions */
+
+'use strict'
+
+const chai = require('chai')
+const each = require('async/each')
+const expect = chai.expect
+const series = require('async/series')
+
+const loginUtils = require('../utils/login')
+const podsUtils = require('../utils/pods')
+const serversUtils = require('../utils/servers')
+const videosUtils = require('../utils/videos')
+const videoBlacklistsUtils = require('../utils/video-blacklists')
+
+describe('Test video blacklists', function () {
+  let servers = []
+
+  before(function (done) {
+    this.timeout(30000)
+
+    series([
+      // Run servers
+      function (next) {
+        serversUtils.flushAndRunMultipleServers(2, function (serversRun) {
+          servers = serversRun
+          next()
+        })
+      },
+      // Get the access tokens
+      function (next) {
+        each(servers, function (server, callbackEach) {
+          loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
+            if (err) return callbackEach(err)
+
+            server.accessToken = accessToken
+            callbackEach()
+          })
+        }, next)
+      },
+      // Pod 1 makes friend with pod 2
+      function (next) {
+        const server = servers[0]
+        podsUtils.makeFriends(server.url, server.accessToken, next)
+      },
+      // Upload a video on pod 2
+      function (next) {
+        const videoAttributes = {
+          name: 'my super name for pod 2',
+          description: 'my super description for pod 2'
+        }
+        videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes, next)
+      },
+      // Wait videos propagation
+      function (next) {
+        setTimeout(next, 11000)
+      },
+      function (next) {
+        videosUtils.getVideosList(servers[0].url, function (err, res) {
+          if (err) throw err
+
+          const videos = res.body.data
+
+          expect(videos.length).to.equal(1)
+
+          servers[0].remoteVideo = videos.find(function (video) { return video.name === 'my super name for pod 2' })
+
+          next()
+        })
+      }
+    ], done)
+  })
+
+  it('Should blacklist a remote video on pod 1', function (done) {
+    videoBlacklistsUtils.addVideoToBlacklist(servers[0].url, servers[0].accessToken, servers[0].remoteVideo.id, done)
+  })
+
+  it('Should not have the video blacklisted in videos list on pod 1', function (done) {
+    videosUtils.getVideosList(servers[0].url, function (err, res) {
+      if (err) throw err
+
+      expect(res.body.total).to.equal(0)
+      expect(res.body.data).to.be.an('array')
+      expect(res.body.data.length).to.equal(0)
+
+      done()
+    })
+  })
+
+  it('Should not have the video blacklisted in videos search on pod 1', function (done) {
+    videosUtils.searchVideo(servers[0].url, 'name', function (err, res) {
+      if (err) throw err
+
+      expect(res.body.total).to.equal(0)
+      expect(res.body.data).to.be.an('array')
+      expect(res.body.data.length).to.equal(0)
+
+      done()
+    })
+  })
+
+  it('Should have the blacklisted video in videos list on pod 2', function (done) {
+    videosUtils.getVideosList(servers[1].url, function (err, res) {
+      if (err) throw err
+
+      expect(res.body.total).to.equal(1)
+      expect(res.body.data).to.be.an('array')
+      expect(res.body.data.length).to.equal(1)
+
+      done()
+    })
+  })
+
+  it('Should have the video blacklisted in videos search on pod 2', function (done) {
+    videosUtils.searchVideo(servers[1].url, 'name', function (err, res) {
+      if (err) throw err
+
+      expect(res.body.total).to.equal(1)
+      expect(res.body.data).to.be.an('array')
+      expect(res.body.data.length).to.equal(1)
+
+      done()
+    })
+  })
+
+  after(function (done) {
+    servers.forEach(function (server) {
+      process.kill(-server.app.pid)
+    })
+
+    // Keep the logs if the test failed
+    if (this.ok) {
+      serversUtils.flushTests(done)
+    } else {
+      done()
+    }
+  })
+})
diff --git a/server/tests/utils/video-abuses.js b/server/tests/utils/video-abuses.js
index 596c824b3..c4dd87990 100644
--- a/server/tests/utils/video-abuses.js
+++ b/server/tests/utils/video-abuses.js
@@ -2,7 +2,7 @@
 
 const request = require('supertest')
 
-const videosUtils = {
+const videosAbuseUtils = {
   getVideoAbusesList,
   getVideoAbusesListPagination,
   getVideoAbusesListSort,
@@ -70,4 +70,4 @@ function getVideoAbusesListSort (url, token, sort, end) {
 
 // ---------------------------------------------------------------------------
 
-module.exports = videosUtils
+module.exports = videosAbuseUtils
diff --git a/server/tests/utils/video-blacklists.js b/server/tests/utils/video-blacklists.js
new file mode 100644
index 000000000..0a58dd631
--- /dev/null
+++ b/server/tests/utils/video-blacklists.js
@@ -0,0 +1,29 @@
+'use strict'
+
+const request = require('supertest')
+
+const videosBlacklistsUtils = {
+  addVideoToBlacklist
+}
+
+// ---------------------- Export functions --------------------
+
+function addVideoToBlacklist (url, token, videoId, specialStatus, end) {
+  if (!end) {
+    end = specialStatus
+    specialStatus = 204
+  }
+
+  const path = '/api/v1/videos/' + videoId + '/blacklist'
+
+  request(url)
+    .post(path)
+    .set('Accept', 'application/json')
+    .set('Authorization', 'Bearer ' + token)
+    .expect(specialStatus)
+    .end(end)
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = videosBlacklistsUtils
-- 
cgit v1.2.3