X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Futils.js;h=3cc769f265a26066ba85dd668700073d1955767c;hb=677618d4a600a1678088d107850c8f1f8c95255f;hp=45f11ac8f36ce57fc51b464d8ba538b2f5099532;hpb=bc503c2a62dcf9aed6b8d90b68f0f27a7755ac01;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js index 45f11ac8f..3cc769f26 100644 --- a/server/tests/api/utils.js +++ b/server/tests/api/utils.js @@ -8,10 +8,15 @@ const pathUtils = require('path') const request = require('supertest') const testUtils = { + dateIsValid: dateIsValid, flushTests: flushTests, + getAllVideosListBy: getAllVideosListBy, + getClient: getClient, getFriendsList: getFriendsList, getVideo: getVideo, getVideosList: getVideosList, + getVideosListPagination: getVideosListPagination, + getVideosListSort: getVideosListSort, login: login, loginAndGetAccessToken: loginAndGetAccessToken, makeFriends: makeFriends, @@ -20,16 +25,53 @@ 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) } +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/' @@ -57,6 +99,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 +165,7 @@ function loginAndGetAccessToken (server, callback) { }) } -function makeFriends (url, expectedStatus, callback) { +function makeFriends (url, accessToken, expectedStatus, callback) { if (!callback) { callback = expectedStatus expectedStatus = 204 @@ -109,6 +177,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 +187,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,33 +318,72 @@ 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 testImage (url, videoName, imagePath, callback) { +function searchVideoWithSort (url, search, sort, end) { + const path = '/api/v1/videos' + request(url) - .get(imagePath) + .get(path + '/search/' + search) + .query({ sort: sort }) + .set('Accept', 'application/json') .expect(200) - .end(function (err, res) { - if (err) return callback(err) + .expect('Content-Type', /json/) + .end(end) +} - fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) { +function testImage (url, videoName, imagePath, callback) { + // 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 @@ -277,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) } // ---------------------------------------------------------------------------