]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/utils.js
Server: Add some cli tools to make it easy to upload a lot of videos
[github/Chocobozzz/PeerTube.git] / server / tests / api / utils.js
index ac43946cda82c54082fce2831fda818a2b074778..3cc769f265a26066ba85dd668700073d1955767c 100644 (file)
@@ -10,6 +10,8 @@ const request = require('supertest')
 const testUtils = {
   dateIsValid: dateIsValid,
   flushTests: flushTests,
+  getAllVideosListBy: getAllVideosListBy,
+  getClient: getClient,
   getFriendsList: getFriendsList,
   getVideo: getVideo,
   getVideosList: getVideosList,
@@ -45,6 +47,31 @@ function flushTests (callback) {
   exec('npm run clean:server:test', callback)
 }
 
+function getAllVideosListBy (url, end) {
+  const path = '/api/v1/videos'
+
+  request(url)
+    .get(path)
+    .query({ sort: 'createdDate' })
+    .query({ start: 0 })
+    .query({ count: 10000 })
+    .set('Accept', 'application/json')
+    .expect(200)
+    .expect('Content-Type', /json/)
+    .end(end)
+}
+
+function getClient (url, end) {
+  const path = '/api/v1/users/client'
+
+  request(url)
+    .get(path)
+    .set('Accept', 'application/json')
+    .expect(200)
+    .expect('Content-Type', /json/)
+    .end(end)
+}
+
 function getFriendsList (url, end) {
   const path = '/api/v1/pods/'
 
@@ -72,6 +99,7 @@ function getVideosList (url, end) {
 
   request(url)
     .get(path)
+    .query({ sort: 'name' })
     .set('Accept', 'application/json')
     .expect(200)
     .expect('Content-Type', /json/)
@@ -290,24 +318,31 @@ function runServer (number, callback) {
   })
 }
 
-function searchVideo (url, search, end) {
-  const path = '/api/v1/videos'
+function searchVideo (url, search, field, end) {
+  if (!end) {
+    end = field
+    field = null
+  }
 
-  request(url)
-    .get(path + '/search/' + search)
-    .set('Accept', 'application/json')
-    .expect(200)
-    .expect('Content-Type', /json/)
-    .end(end)
+  const path = '/api/v1/videos'
+  const req = request(url)
+              .get(path + '/search/' + search)
+              .set('Accept', 'application/json')
+
+  if (field) req.query({ field: field })
+  req.expect(200)
+     .expect('Content-Type', /json/)
+     .end(end)
 }
 
-function searchVideoWithPagination (url, search, start, count, end) {
+function searchVideoWithPagination (url, search, field, start, count, end) {
   const path = '/api/v1/videos'
 
   request(url)
     .get(path + '/search/' + search)
     .query({ start: start })
     .query({ count: count })
+    .query({ field: field })
     .set('Accept', 'application/json')
     .expect(200)
     .expect('Content-Type', /json/)
@@ -327,21 +362,28 @@ function searchVideoWithSort (url, search, sort, end) {
 }
 
 function testImage (url, videoName, imagePath, callback) {
-  request(url)
-    .get(imagePath)
-    .expect(200)
-    .end(function (err, res) {
-      if (err) return callback(err)
-
-      fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) {
+  // Don't test images if the node env is not set
+  // Because we need a special ffmpeg version for this test
+  if (process.env.NODE_TEST_IMAGE) {
+    request(url)
+      .get(imagePath)
+      .expect(200)
+      .end(function (err, res) {
         if (err) return callback(err)
 
-        callback(null, data.equals(res.body))
+        fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) {
+          if (err) return callback(err)
+
+          callback(null, data.equals(res.body))
+        })
       })
-    })
+  } else {
+    console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
+    callback(null, true)
+  }
 }
 
-function uploadVideo (url, accessToken, name, description, fixture, specialStatus, end) {
+function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
   if (!end) {
     end = specialStatus
     specialStatus = 204
@@ -349,15 +391,27 @@ function uploadVideo (url, accessToken, name, description, fixture, specialStatu
 
   const path = '/api/v1/videos'
 
-  request(url)
-    .post(path)
-    .set('Accept', 'application/json')
-    .set('Authorization', 'Bearer ' + accessToken)
-    .field('name', name)
-    .field('description', description)
-    .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture))
-    .expect(specialStatus)
-    .end(end)
+  const req = request(url)
+              .post(path)
+              .set('Accept', 'application/json')
+              .set('Authorization', 'Bearer ' + accessToken)
+              .field('name', name)
+              .field('description', description)
+
+  for (let i = 0; i < tags.length; i++) {
+    req.field('tags[' + i + ']', tags[i])
+  }
+
+  let filepath = ''
+  if (pathUtils.isAbsolute(fixture)) {
+    filepath = fixture
+  } else {
+    filepath = pathUtils.join(__dirname, 'fixtures', fixture)
+  }
+
+  req.attach('videofile', filepath)
+     .expect(specialStatus)
+     .end(end)
 }
 
 // ---------------------------------------------------------------------------