]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/utils.js
Extends the search feature by customizing the search field (name,
[github/Chocobozzz/PeerTube.git] / server / tests / api / utils.js
index 45f11ac8f36ce57fc51b464d8ba538b2f5099532..c6430c930612cd020b3f1e5f57f0a7bf1db7e941 100644 (file)
@@ -8,10 +8,13 @@ const pathUtils = require('path')
 const request = require('supertest')
 
 const testUtils = {
+  dateIsValid: dateIsValid,
   flushTests: flushTests,
   getFriendsList: getFriendsList,
   getVideo: getVideo,
   getVideosList: getVideosList,
+  getVideosListPagination: getVideosListPagination,
+  getVideosListSort: getVideosListSort,
   login: login,
   loginAndGetAccessToken: loginAndGetAccessToken,
   makeFriends: makeFriends,
@@ -20,12 +23,24 @@ const testUtils = {
   flushAndRunMultipleServers: flushAndRunMultipleServers,
   runServer: runServer,
   searchVideo: searchVideo,
+  searchVideoWithPagination: searchVideoWithPagination,
+  searchVideoWithSort: searchVideoWithSort,
   testImage: testImage,
   uploadVideo: uploadVideo
 }
 
 // ---------------------- Export functions --------------------
 
+function dateIsValid (dateString) {
+  const dateToCheck = new Date(dateString)
+  const now = new Date()
+
+  // Check if the interval is more than 2 minutes
+  if (now - dateToCheck > 120000) return false
+
+  return true
+}
+
 function flushTests (callback) {
   exec('npm run clean:server:test', callback)
 }
@@ -57,6 +72,32 @@ function getVideosList (url, end) {
 
   request(url)
     .get(path)
+    .query({ sort: 'name' })
+    .set('Accept', 'application/json')
+    .expect(200)
+    .expect('Content-Type', /json/)
+    .end(end)
+}
+
+function getVideosListPagination (url, start, count, end) {
+  const path = '/api/v1/videos'
+
+  request(url)
+    .get(path)
+    .query({ start: start })
+    .query({ count: count })
+    .set('Accept', 'application/json')
+    .expect(200)
+    .expect('Content-Type', /json/)
+    .end(end)
+}
+
+function getVideosListSort (url, sort, end) {
+  const path = '/api/v1/videos'
+
+  request(url)
+    .get(path)
+    .query({ sort: sort })
     .set('Accept', 'application/json')
     .expect(200)
     .expect('Content-Type', /json/)
@@ -97,7 +138,7 @@ function loginAndGetAccessToken (server, callback) {
   })
 }
 
-function makeFriends (url, expectedStatus, callback) {
+function makeFriends (url, accessToken, expectedStatus, callback) {
   if (!callback) {
     callback = expectedStatus
     expectedStatus = 204
@@ -109,6 +150,7 @@ function makeFriends (url, expectedStatus, callback) {
   request(url)
     .get(path)
     .set('Accept', 'application/json')
+    .set('Authorization', 'Bearer ' + accessToken)
     .expect(expectedStatus)
     .end(function (err, res) {
       if (err) throw err
@@ -118,14 +160,20 @@ function makeFriends (url, expectedStatus, callback) {
     })
 }
 
-function quitFriends (url, callback) {
+function quitFriends (url, accessToken, expectedStatus, callback) {
+  if (!callback) {
+    callback = expectedStatus
+    expectedStatus = 204
+  }
+
   const path = '/api/v1/pods/quitfriends'
 
   // The first pod make friend with the third
   request(url)
     .get(path)
     .set('Accept', 'application/json')
-    .expect(204)
+    .set('Authorization', 'Bearer ' + accessToken)
+    .expect(expectedStatus)
     .end(function (err, res) {
       if (err) throw err
 
@@ -243,11 +291,43 @@ function runServer (number, callback) {
   })
 }
 
-function searchVideo (url, search, end) {
+function searchVideo (url, search, field, end) {
+  if (!end) {
+    end = field
+    field = null
+  }
+
+  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, 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/)
+    .end(end)
+}
+
+function searchVideoWithSort (url, search, sort, end) {
   const path = '/api/v1/videos'
 
   request(url)
     .get(path + '/search/' + search)
+    .query({ sort: sort })
     .set('Accept', 'application/json')
     .expect(200)
     .expect('Content-Type', /json/)