From 8d30905858245f12a42fc327d2d57cbfe062d548 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Aug 2016 22:09:59 +0200 Subject: [PATCH] Server: split tests utils in multiple files --- server/tests/api/checkParams.js | 20 +- server/tests/api/friendsAdvanced.js | 23 +- server/tests/api/friendsBasic.js | 30 +- server/tests/api/multiplePods.js | 62 ++-- server/tests/api/singlePod.js | 90 ++--- server/tests/api/users.js | 59 ++-- server/tests/api/utils.js | 492 ---------------------------- server/tests/utils/clients.js | 24 ++ server/tests/utils/login.js | 48 +++ server/tests/utils/miscs.js | 21 ++ server/tests/utils/pods.js | 70 ++++ server/tests/utils/servers.js | 115 +++++++ server/tests/utils/users.js | 85 +++++ server/tests/utils/videos.js | 199 +++++++++++ 14 files changed, 712 insertions(+), 626 deletions(-) delete mode 100644 server/tests/api/utils.js create mode 100644 server/tests/utils/clients.js create mode 100644 server/tests/utils/login.js create mode 100644 server/tests/utils/miscs.js create mode 100644 server/tests/utils/pods.js create mode 100644 server/tests/utils/servers.js create mode 100644 server/tests/utils/users.js create mode 100644 server/tests/utils/videos.js diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js index e489df277..675dc19e6 100644 --- a/server/tests/api/checkParams.js +++ b/server/tests/api/checkParams.js @@ -6,7 +6,9 @@ const pathUtils = require('path') const request = require('supertest') const series = require('async/series') -const utils = require('./utils') +const loginUtils = require('../utils/login') +const serversUtils = require('../utils/servers') +const usersUtils = require('../utils/users') describe('Test parameters validator', function () { let server = null @@ -71,17 +73,17 @@ describe('Test parameters validator', function () { series([ function (next) { - utils.flushTests(next) + serversUtils.flushTests(next) }, function (next) { - utils.runServer(1, function (server1) { + serversUtils.runServer(1, function (server1) { server = server1 next() }) }, function (next) { - utils.loginAndGetAccessToken(server, function (err, token) { + loginUtils.loginAndGetAccessToken(server, function (err, token) { if (err) throw err server.accessToken = token @@ -141,13 +143,13 @@ describe('Test parameters validator', function () { let userAccessToken = null before(function (done) { - utils.createUser(server.url, server.accessToken, 'user1', 'password', function () { + usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () { server.user = { username: 'user1', password: 'password' } - utils.loginAndGetAccessToken(server, function (err, accessToken) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) throw err userAccessToken = accessToken @@ -581,7 +583,7 @@ describe('Test parameters validator', function () { password: 'my super password' } - utils.loginAndGetAccessToken(server, function (err, accessToken) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) throw err userAccessToken = accessToken @@ -598,7 +600,7 @@ describe('Test parameters validator', function () { describe('When updating a user', function () { before(function (done) { - utils.getUsersList(server.url, function (err, res) { + usersUtils.getUsersList(server.url, function (err, res) { if (err) throw err userId = res.body.data[1].id @@ -702,7 +704,7 @@ describe('Test parameters validator', function () { // Keep the logs if the test failed if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/friendsAdvanced.js b/server/tests/api/friendsAdvanced.js index 603fbc16b..0d24481ef 100644 --- a/server/tests/api/friendsAdvanced.js +++ b/server/tests/api/friendsAdvanced.js @@ -5,24 +5,27 @@ const each = require('async/each') const expect = chai.expect const series = require('async/series') -const utils = require('./utils') +const loginUtils = require('../utils/login') +const podsUtils = require('../utils/pods') +const serversUtils = require('../utils/servers') +const videosUtils = require('../utils/videos') describe('Test advanced friends', function () { let servers = [] function makeFriends (podNumber, callback) { const server = servers[podNumber - 1] - return utils.makeFriends(server.url, server.accessToken, callback) + return podsUtils.makeFriends(server.url, server.accessToken, callback) } function quitFriends (podNumber, callback) { const server = servers[podNumber - 1] - return utils.quitFriends(server.url, server.accessToken, callback) + return podsUtils.quitFriends(server.url, server.accessToken, callback) } function getFriendsList (podNumber, end) { const server = servers[podNumber - 1] - return utils.getFriendsList(server.url, end) + return podsUtils.getFriendsList(server.url, end) } function uploadVideo (podNumber, callback) { @@ -32,22 +35,22 @@ describe('Test advanced friends', function () { const fixture = 'video_short.webm' const server = servers[podNumber - 1] - return utils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback) + return videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback) } function getVideos (podNumber, callback) { - return utils.getVideosList(servers[podNumber - 1].url, callback) + return videosUtils.getVideosList(servers[podNumber - 1].url, callback) } // --------------------------------------------------------------- before(function (done) { this.timeout(30000) - utils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) { + serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) { servers = serversRun each(servers, function (server, callbackEach) { - utils.loginAndGetAccessToken(server, function (err, accessToken) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) return callbackEach(err) server.accessToken = accessToken @@ -169,7 +172,7 @@ describe('Test advanced friends', function () { }, // Rerun server 4 function (next) { - utils.runServer(4, function (server) { + serversUtils.runServer(4, function (server) { servers[3].app = server.app next() }) @@ -273,7 +276,7 @@ describe('Test advanced friends', function () { }) if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/friendsBasic.js b/server/tests/api/friendsBasic.js index c74a7f224..2a6883acb 100644 --- a/server/tests/api/friendsBasic.js +++ b/server/tests/api/friendsBasic.js @@ -5,14 +5,16 @@ const each = require('async/each') const expect = chai.expect const series = require('async/series') -const utils = require('./utils') +const loginUtils = require('../utils/login') +const podsUtils = require('../utils/pods') +const serversUtils = require('../utils/servers') describe('Test basic friends', function () { let servers = [] function makeFriends (podNumber, callback) { const server = servers[podNumber - 1] - return utils.makeFriends(server.url, server.accessToken, callback) + return podsUtils.makeFriends(server.url, server.accessToken, callback) } function testMadeFriends (servers, serverToTest, callback) { @@ -22,7 +24,7 @@ describe('Test basic friends', function () { friends.push(servers[i].url) } - utils.getFriendsList(serverToTest.url, function (err, res) { + podsUtils.getFriendsList(serverToTest.url, function (err, res) { if (err) throw err const result = res.body @@ -43,11 +45,11 @@ describe('Test basic friends', function () { before(function (done) { this.timeout(20000) - utils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { + serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { servers = serversRun each(servers, function (server, callbackEach) { - utils.loginAndGetAccessToken(server, function (err, accessToken) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) return callbackEach(err) server.accessToken = accessToken @@ -59,7 +61,7 @@ describe('Test basic friends', function () { it('Should not have friends', function (done) { each(servers, function (server, callback) { - utils.getFriendsList(server.url, function (err, res) { + podsUtils.getFriendsList(server.url, function (err, res) { if (err) throw err const result = res.body @@ -84,7 +86,7 @@ describe('Test basic friends', function () { }, // The second pod should have the third as a friend function (next) { - utils.getFriendsList(servers[1].url, function (err, res) { + podsUtils.getFriendsList(servers[1].url, function (err, res) { if (err) throw err const result = res.body @@ -97,7 +99,7 @@ describe('Test basic friends', function () { }, // Same here, the third pod should have the second pod as a friend function (next) { - utils.getFriendsList(servers[2].url, function (err, res) { + podsUtils.getFriendsList(servers[2].url, function (err, res) { if (err) throw err const result = res.body @@ -128,7 +130,7 @@ describe('Test basic friends', function () { it('Should not be allowed to make friend again', function (done) { const server = servers[1] - utils.makeFriends(server.url, server.accessToken, 409, done) + podsUtils.makeFriends(server.url, server.accessToken, 409, done) }) it('Should quit friends of pod 2', function (done) { @@ -136,11 +138,11 @@ describe('Test basic friends', function () { // Pod 1 quit friends function (next) { const server = servers[1] - utils.quitFriends(server.url, server.accessToken, next) + podsUtils.quitFriends(server.url, server.accessToken, next) }, // Pod 1 should not have friends anymore function (next) { - utils.getFriendsList(servers[1].url, function (err, res) { + podsUtils.getFriendsList(servers[1].url, function (err, res) { if (err) throw err const result = res.body @@ -153,7 +155,7 @@ describe('Test basic friends', function () { // Other pods shouldn't have pod 1 too function (next) { each([ servers[0].url, servers[2].url ], function (url, callback) { - utils.getFriendsList(url, function (err, res) { + podsUtils.getFriendsList(url, function (err, res) { if (err) throw err const result = res.body @@ -169,7 +171,7 @@ describe('Test basic friends', function () { it('Should allow pod 2 to make friend again', function (done) { const server = servers[1] - utils.makeFriends(server.url, server.accessToken, function () { + podsUtils.makeFriends(server.url, server.accessToken, function () { each(servers, function (server, callback) { testMadeFriends(servers, server, callback) }, done) @@ -182,7 +184,7 @@ describe('Test basic friends', function () { }) if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js index ac140f6bb..b86f88c22 100644 --- a/server/tests/api/multiplePods.js +++ b/server/tests/api/multiplePods.js @@ -6,7 +6,11 @@ const expect = chai.expect const pathUtils = require('path') const series = require('async/series') -const utils = require('./utils') +const loginUtils = require('../utils/login') +const miscsUtils = require('../utils/miscs') +const podsUtils = require('../utils/pods') +const serversUtils = require('../utils/servers') +const videosUtils = require('../utils/videos') const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) webtorrent.silent = true @@ -20,7 +24,7 @@ describe('Test multiple pods', function () { series([ // Run servers function (next) { - utils.flushAndRunMultipleServers(3, function (serversRun) { + serversUtils.flushAndRunMultipleServers(3, function (serversRun) { servers = serversRun next() }) @@ -28,7 +32,7 @@ describe('Test multiple pods', function () { // Get the access tokens function (next) { each(servers, function (server, callbackEach) { - utils.loginAndGetAccessToken(server, function (err, accessToken) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) return callbackEach(err) server.accessToken = accessToken @@ -39,7 +43,7 @@ describe('Test multiple pods', function () { // The second pod make friend with the third function (next) { const server = servers[1] - utils.makeFriends(server.url, server.accessToken, next) + podsUtils.makeFriends(server.url, server.accessToken, next) }, // Wait for the request between pods function (next) { @@ -48,7 +52,7 @@ describe('Test multiple pods', function () { // Pod 1 make friends too function (next) { const server = servers[0] - utils.makeFriends(server.url, server.accessToken, next) + podsUtils.makeFriends(server.url, server.accessToken, next) }, function (next) { webtorrent.create({ host: 'client', port: '1' }, next) @@ -58,7 +62,7 @@ describe('Test multiple pods', function () { it('Should not have videos for all pods', function (done) { each(servers, function (server, callback) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -80,7 +84,7 @@ describe('Test multiple pods', function () { const description = 'my super description for pod 1' const tags = [ 'tag1p1', 'tag2p1' ] const file = 'video_short1.webm' - utils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next) + videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 11000) @@ -92,7 +96,7 @@ describe('Test multiple pods', function () { each(servers, function (server, callback) { let baseMagnet = null - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -105,7 +109,7 @@ describe('Test multiple pods', function () { expect(video.magnetUri).to.exist expect(video.duration).to.equal(10) expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true expect(video.author).to.equal('root') if (server.url !== 'http://localhost:9001') { @@ -121,7 +125,7 @@ describe('Test multiple pods', function () { expect(video.magnetUri).to.equal.magnetUri } - utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -142,7 +146,7 @@ describe('Test multiple pods', function () { const description = 'my super description for pod 2' const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ] const file = 'video_short2.webm' - utils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next) + videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 11000) @@ -154,7 +158,7 @@ describe('Test multiple pods', function () { each(servers, function (server, callback) { let baseMagnet = null - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -167,7 +171,7 @@ describe('Test multiple pods', function () { expect(video.magnetUri).to.exist expect(video.duration).to.equal(5) expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true expect(video.author).to.equal('root') if (server.url !== 'http://localhost:9002') { @@ -183,7 +187,7 @@ describe('Test multiple pods', function () { expect(video.magnetUri).to.equal.magnetUri } - utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -204,14 +208,14 @@ describe('Test multiple pods', function () { const description = 'my super description for pod 3' const tags = [ 'tag1p3' ] const file = 'video_short3.webm' - utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) + videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) }, function (next) { const name = 'my super name for pod 3-2' const description = 'my super description for pod 3-2' const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ] const file = 'video_short.webm' - utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) + videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 22000) @@ -222,7 +226,7 @@ describe('Test multiple pods', function () { let baseMagnet = null // All pods should have this video each(servers, function (server, callback) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -247,7 +251,7 @@ describe('Test multiple pods', function () { expect(video1.duration).to.equal(5) expect(video1.tags).to.deep.equal([ 'tag1p3' ]) expect(video1.author).to.equal('root') - expect(utils.dateIsValid(video1.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video1.createdDate)).to.be.true expect(video2.name).to.equal('my super name for pod 3-2') expect(video2.description).to.equal('my super description for pod 3-2') @@ -256,7 +260,7 @@ describe('Test multiple pods', function () { expect(video2.duration).to.equal(5) expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ]) expect(video2.author).to.equal('root') - expect(utils.dateIsValid(video2.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video2.createdDate)).to.be.true if (server.url !== 'http://localhost:9003') { expect(video1.isLocal).to.be.false @@ -273,11 +277,11 @@ describe('Test multiple pods', function () { expect(video2.magnetUri).to.equal.magnetUri } - utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) - utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -296,7 +300,7 @@ describe('Test multiple pods', function () { // Yes, this could be long this.timeout(200000) - utils.getVideosList(servers[2].url, function (err, res) { + videosUtils.getVideosList(servers[2].url, function (err, res) { if (err) throw err const video = res.body.data[0] @@ -317,7 +321,7 @@ describe('Test multiple pods', function () { // Yes, this could be long this.timeout(200000) - utils.getVideosList(servers[0].url, function (err, res) { + videosUtils.getVideosList(servers[0].url, function (err, res) { if (err) throw err const video = res.body.data[1] @@ -336,7 +340,7 @@ describe('Test multiple pods', function () { // Yes, this could be long this.timeout(200000) - utils.getVideosList(servers[1].url, function (err, res) { + videosUtils.getVideosList(servers[1].url, function (err, res) { if (err) throw err const video = res.body.data[2] @@ -355,7 +359,7 @@ describe('Test multiple pods', function () { // Yes, this could be long this.timeout(200000) - utils.getVideosList(servers[0].url, function (err, res) { + videosUtils.getVideosList(servers[0].url, function (err, res) { if (err) throw err const video = res.body.data[3] @@ -375,10 +379,10 @@ describe('Test multiple pods', function () { series([ function (next) { - utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next) + videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next) }, function (next) { - utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next) + videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next) }], function (err) { if (err) throw err @@ -389,7 +393,7 @@ describe('Test multiple pods', function () { it('Should have videos 1 and 3 on each pod', function (done) { each(servers, function (server, callback) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -415,7 +419,7 @@ describe('Test multiple pods', function () { // Keep the logs if the test failed if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/singlePod.js b/server/tests/api/singlePod.js index 6ed719f87..573eaa3a8 100644 --- a/server/tests/api/singlePod.js +++ b/server/tests/api/singlePod.js @@ -8,11 +8,13 @@ const keyBy = require('lodash/keyBy') const pathUtils = require('path') const series = require('async/series') +const loginUtils = require('../utils/login') +const miscsUtils = require('../utils/miscs') +const serversUtils = require('../utils/servers') +const videosUtils = require('../utils/videos') const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) webtorrent.silent = true -const utils = require('./utils') - describe('Test a single pod', function () { let server = null let videoId = -1 @@ -23,16 +25,16 @@ describe('Test a single pod', function () { series([ function (next) { - utils.flushTests(next) + serversUtils.flushTests(next) }, function (next) { - utils.runServer(1, function (server1) { + serversUtils.runServer(1, function (server1) { server = server1 next() }) }, function (next) { - utils.loginAndGetAccessToken(server, function (err, token) { + loginUtils.loginAndGetAccessToken(server, function (err, token) { if (err) throw err server.accessToken = token next() @@ -45,7 +47,7 @@ describe('Test a single pod', function () { }) it('Should not have videos', function (done) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err expect(res.body.total).to.equal(0) @@ -62,14 +64,14 @@ describe('Test a single pod', function () { const description = 'my super description' const tags = [ 'tag1', 'tag2', 'tag3' ] const file = 'video_short.webm' - utils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done) + videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done) }) it('Should seed the uploaded video', function (done) { // Yes, this could be long this.timeout(60000) - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err expect(res.body.total).to.equal(1) @@ -84,9 +86,9 @@ describe('Test a single pod', function () { expect(video.author).to.equal('root') expect(video.isLocal).to.be.true expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true - utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -108,7 +110,7 @@ describe('Test a single pod', function () { // Yes, this could be long this.timeout(60000) - utils.getVideo(server.url, videoId, function (err, res) { + videosUtils.getVideo(server.url, videoId, function (err, res) { if (err) throw err const video = res.body @@ -119,9 +121,9 @@ describe('Test a single pod', function () { expect(video.author).to.equal('root') expect(video.isLocal).to.be.true expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true - utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -137,7 +139,7 @@ describe('Test a single pod', function () { }) it('Should search the video by name by default', function (done) { - utils.searchVideo(server.url, 'my', function (err, res) { + videosUtils.searchVideo(server.url, 'my', function (err, res) { if (err) throw err expect(res.body.total).to.equal(1) @@ -151,9 +153,9 @@ describe('Test a single pod', function () { expect(video.author).to.equal('root') expect(video.isLocal).to.be.true expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true - utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -163,7 +165,7 @@ describe('Test a single pod', function () { }) it('Should search the video by podUrl', function (done) { - utils.searchVideo(server.url, '9001', 'podUrl', function (err, res) { + videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) { if (err) throw err expect(res.body.total).to.equal(1) @@ -177,9 +179,9 @@ describe('Test a single pod', function () { expect(video.author).to.equal('root') expect(video.isLocal).to.be.true expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true - utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -189,7 +191,7 @@ describe('Test a single pod', function () { }) it('Should search the video by tag', function (done) { - utils.searchVideo(server.url, 'tag1', 'tags', function (err, res) { + videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) { if (err) throw err expect(res.body.total).to.equal(1) @@ -203,9 +205,9 @@ describe('Test a single pod', function () { expect(video.author).to.equal('root') expect(video.isLocal).to.be.true expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) - expect(utils.dateIsValid(video.createdDate)).to.be.true + expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true - utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -215,7 +217,7 @@ describe('Test a single pod', function () { }) it('Should not find a search by name by default', function (done) { - utils.searchVideo(server.url, 'hello', function (err, res) { + videosUtils.searchVideo(server.url, 'hello', function (err, res) { if (err) throw err expect(res.body.total).to.equal(0) @@ -227,7 +229,7 @@ describe('Test a single pod', function () { }) it('Should not find a search by author', function (done) { - utils.searchVideo(server.url, 'hello', 'author', function (err, res) { + videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) { if (err) throw err expect(res.body.total).to.equal(0) @@ -239,7 +241,7 @@ describe('Test a single pod', function () { }) it('Should not find a search by tag', function (done) { - utils.searchVideo(server.url, 'tag', 'tags', function (err, res) { + videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) { if (err) throw err expect(res.body.total).to.equal(0) @@ -251,7 +253,7 @@ describe('Test a single pod', function () { }) it('Should remove the video', function (done) { - utils.removeVideo(server.url, server.accessToken, videoId, function (err) { + videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) { if (err) throw err fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) { @@ -264,7 +266,7 @@ describe('Test a single pod', function () { }) it('Should not have videos', function (done) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err expect(res.body.total).to.equal(0) @@ -286,12 +288,12 @@ describe('Test a single pod', function () { const description = video + ' description' const tags = [ 'tag1', 'tag2', 'tag3' ] - utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach) + videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach) }, done) }) it('Should have the correct durations', function (done) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err expect(res.body.total).to.equal(6) @@ -312,7 +314,7 @@ describe('Test a single pod', function () { }) it('Should have the correct thumbnails', function (done) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data @@ -323,7 +325,7 @@ describe('Test a single pod', function () { if (err) throw err const videoName = video.name.replace(' name', '') - utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) { + videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) { if (err) throw err expect(test).to.equal(true) @@ -334,7 +336,7 @@ describe('Test a single pod', function () { }) it('Should list only the two first videos', function (done) { - utils.getVideosListPagination(server.url, 0, 2, function (err, res) { + videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) { if (err) throw err const videos = res.body.data @@ -348,7 +350,7 @@ describe('Test a single pod', function () { }) it('Should list only the next three videos', function (done) { - utils.getVideosListPagination(server.url, 2, 3, function (err, res) { + videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) { if (err) throw err const videos = res.body.data @@ -363,7 +365,7 @@ describe('Test a single pod', function () { }) it('Should list the last video', function (done) { - utils.getVideosListPagination(server.url, 5, 6, function (err, res) { + videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) { if (err) throw err const videos = res.body.data @@ -376,7 +378,7 @@ describe('Test a single pod', function () { }) it('Should search the first video', function (done) { - utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) { if (err) throw err const videos = res.body.data @@ -389,7 +391,7 @@ describe('Test a single pod', function () { }) it('Should search the last two videos', function (done) { - utils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) { if (err) throw err const videos = res.body.data @@ -403,7 +405,7 @@ describe('Test a single pod', function () { }) it('Should search all the webm videos', function (done) { - utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) { if (err) throw err const videos = res.body.data @@ -415,7 +417,7 @@ describe('Test a single pod', function () { }) it('Should search all the root author videos', function (done) { - utils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) { if (err) throw err const videos = res.body.data @@ -427,7 +429,7 @@ describe('Test a single pod', function () { }) it('Should search all the 9001 port videos', function (done) { - utils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) { if (err) throw err const videos = res.body.data @@ -439,7 +441,7 @@ describe('Test a single pod', function () { }) it('Should search all the localhost videos', function (done) { - utils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) { if (err) throw err const videos = res.body.data @@ -452,7 +454,7 @@ describe('Test a single pod', function () { it('Should search the good magnetUri video', function (done) { const video = videosListBase[0] - utils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) { + videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) { if (err) throw err const videos = res.body.data @@ -465,7 +467,7 @@ describe('Test a single pod', function () { }) it('Should list and sort by name in descending order', function (done) { - utils.getVideosListSort(server.url, '-name', function (err, res) { + videosUtils.getVideosListSort(server.url, '-name', function (err, res) { if (err) throw err const videos = res.body.data @@ -483,7 +485,7 @@ describe('Test a single pod', function () { }) it('Should search and sort by name in ascending order', function (done) { - utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) { + videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) { if (err) throw err const videos = res.body.data @@ -505,7 +507,7 @@ describe('Test a single pod', function () { // Keep the logs if the test failed if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/users.js b/server/tests/api/users.js index e1d4a8cf4..6f9eef181 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js @@ -5,11 +5,14 @@ const expect = chai.expect const pathUtils = require('path') const series = require('async/series') +const loginUtils = require('../utils/login') +const podsUtils = require('../utils/pods') +const serversUtils = require('../utils/servers') +const usersUtils = require('../utils/users') +const videosUtils = require('../utils/videos') const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) webtorrent.silent = true -const utils = require('./utils') - describe('Test users', function () { let server = null let accessToken = null @@ -22,10 +25,10 @@ describe('Test users', function () { series([ function (next) { - utils.flushTests(next) + serversUtils.flushTests(next) }, function (next) { - utils.runServer(1, function (server1) { + serversUtils.runServer(1, function (server1) { server = server1 next() }) @@ -41,7 +44,7 @@ describe('Test users', function () { it('Should not login with an invalid client id', function (done) { const client = { id: 'client', password: server.client.secret } - utils.login(server.url, client, server.user, 400, function (err, res) { + loginUtils.login(server.url, client, server.user, 400, function (err, res) { if (err) throw err expect(res.body.error).to.equal('invalid_client') @@ -51,7 +54,7 @@ describe('Test users', function () { it('Should not login with an invalid client password', function (done) { const client = { id: server.client.id, password: 'coucou' } - utils.login(server.url, client, server.user, 400, function (err, res) { + loginUtils.login(server.url, client, server.user, 400, function (err, res) { if (err) throw err expect(res.body.error).to.equal('invalid_client') @@ -61,7 +64,7 @@ describe('Test users', function () { it('Should not login with an invalid username', function (done) { const user = { username: 'captain crochet', password: server.user.password } - utils.login(server.url, server.client, user, 400, function (err, res) { + loginUtils.login(server.url, server.client, user, 400, function (err, res) { if (err) throw err expect(res.body.error).to.equal('invalid_grant') @@ -71,7 +74,7 @@ describe('Test users', function () { it('Should not login with an invalid password', function (done) { const user = { username: server.user.username, password: 'mewthree' } - utils.login(server.url, server.client, user, 400, function (err, res) { + loginUtils.login(server.url, server.client, user, 400, function (err, res) { if (err) throw err expect(res.body.error).to.equal('invalid_grant') @@ -86,21 +89,21 @@ describe('Test users', function () { const description = 'my super description' const tags = [ 'tag1', 'tag2' ] const video = 'video_short.webm' - utils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done) + videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done) }) it('Should not be able to make friends', function (done) { accessToken = 'mysupertoken' - utils.makeFriends(server.url, accessToken, 401, done) + podsUtils.makeFriends(server.url, accessToken, 401, done) }) it('Should not be able to quit friends', function (done) { accessToken = 'mysupertoken' - utils.quitFriends(server.url, accessToken, 401, done) + podsUtils.quitFriends(server.url, accessToken, 401, done) }) it('Should be able to login', function (done) { - utils.login(server.url, server.client, server.user, 200, function (err, res) { + loginUtils.login(server.url, server.client, server.user, 200, function (err, res) { if (err) throw err accessToken = res.body.access_token @@ -113,10 +116,10 @@ describe('Test users', function () { const description = 'my super description' const tags = [ 'tag1', 'tag2' ] const video = 'video_short.webm' - utils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, function (err, res) { + videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, function (err, res) { if (err) throw err - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const video = res.body.data[0] @@ -133,17 +136,17 @@ describe('Test users', function () { const description = 'my super description 2' const tags = [ 'tag1' ] const video = 'video_short.webm' - utils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) + videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) }) it('Should not be able to remove the video with an incorrect token', function (done) { - utils.removeVideo(server.url, 'bad_token', videoId, 401, done) + videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done) }) it('Should not be able to remove the video with the token of another account') it('Should be able to remove the video with the correct token', function (done) { - utils.removeVideo(server.url, accessToken, videoId, done) + videosUtils.removeVideo(server.url, accessToken, videoId, done) }) it('Should logout (revoke token)') @@ -161,7 +164,7 @@ describe('Test users', function () { it('Should be able to upload a video again') it('Should be able to create a new user', function (done) { - utils.createUser(server.url, accessToken, 'user_1', 'super password', done) + usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done) }) it('Should be able to login with this user', function (done) { @@ -170,7 +173,7 @@ describe('Test users', function () { password: 'super password' } - utils.loginAndGetAccessToken(server, function (err, token) { + loginUtils.loginAndGetAccessToken(server, function (err, token) { if (err) throw err accessTokenUser = token @@ -180,7 +183,7 @@ describe('Test users', function () { }) it('Should be able to get the user informations', function (done) { - utils.getUserInformation(server.url, accessTokenUser, function (err, res) { + usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) { if (err) throw err const user = res.body @@ -199,11 +202,11 @@ describe('Test users', function () { const description = 'my super description' const tags = [ 'tag1', 'tag2', 'tag3' ] const file = 'video_short.webm' - utils.uploadVideo(server.url, accessTokenUser, name, description, tags, file, done) + videosUtils.uploadVideo(server.url, accessTokenUser, name, description, tags, file, done) }) it('Should list all the users', function (done) { - utils.getUsersList(server.url, function (err, res) { + usersUtils.getUsersList(server.url, function (err, res) { if (err) throw err const users = res.body.data @@ -223,25 +226,25 @@ describe('Test users', function () { }) it('Should update the user password', function (done) { - utils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) { + usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) { if (err) throw err server.user.password = 'new password' - utils.login(server.url, server.client, server.user, 200, done) + loginUtils.login(server.url, server.client, server.user, 200, done) }) }) it('Should be able to remove this user', function (done) { - utils.removeUser(server.url, accessToken, 'user_1', done) + usersUtils.removeUser(server.url, accessToken, 'user_1', done) }) it('Should not be able to login with this user', function (done) { // server.user is already set to user 1 - utils.login(server.url, server.client, server.user, 400, done) + loginUtils.login(server.url, server.client, server.user, 400, done) }) it('Should not have videos of this user', function (done) { - utils.getVideosList(server.url, function (err, res) { + videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err expect(res.body.total).to.equal(1) @@ -257,7 +260,7 @@ describe('Test users', function () { // Keep the logs if the test failed if (this.ok) { - utils.flushTests(done) + serversUtils.flushTests(done) } else { done() } diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js deleted file mode 100644 index 8871f1f84..000000000 --- a/server/tests/api/utils.js +++ /dev/null @@ -1,492 +0,0 @@ -'use strict' - -const childProcess = require('child_process') -const exec = childProcess.exec -const fork = childProcess.fork -const fs = require('fs') -const pathUtils = require('path') -const request = require('supertest') - -const testUtils = { - createUser: createUser, - dateIsValid: dateIsValid, - flushTests: flushTests, - getAllVideosListBy: getAllVideosListBy, - getClient: getClient, - getFriendsList: getFriendsList, - getUserInformation: getUserInformation, - getUsersList: getUsersList, - getVideo: getVideo, - getVideosList: getVideosList, - getVideosListPagination: getVideosListPagination, - getVideosListSort: getVideosListSort, - login: login, - loginAndGetAccessToken: loginAndGetAccessToken, - makeFriends: makeFriends, - quitFriends: quitFriends, - removeUser: removeUser, - removeVideo: removeVideo, - flushAndRunMultipleServers: flushAndRunMultipleServers, - runServer: runServer, - searchVideo: searchVideo, - searchVideoWithPagination: searchVideoWithPagination, - searchVideoWithSort: searchVideoWithSort, - testImage: testImage, - uploadVideo: uploadVideo, - updateUser: updateUser -} - -// ---------------------- Export functions -------------------- - -function createUser (url, accessToken, username, password, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/users' - - request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send({ username: username, password: password }) - .expect(specialStatus) - .end(end) -} - -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 getUserInformation (url, accessToken, end) { - const path = '/api/v1/users/me' - - request(url) - .get(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getUsersList (url, end) { - const path = '/api/v1/users' - - 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/' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideo (url, id, end) { - const path = '/api/v1/videos/' + id - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideosList (url, end) { - const path = '/api/v1/videos' - - 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/) - .end(end) -} - -function login (url, client, user, expectedStatus, end) { - if (!end) { - end = expectedStatus - expectedStatus = 200 - } - - const path = '/api/v1/users/token' - - const body = { - client_id: client.id, - client_secret: client.secret, - username: user.username, - password: user.password, - response_type: 'code', - grant_type: 'password', - scope: 'upload' - } - - request(url) - .post(path) - .type('form') - .send(body) - .expect(expectedStatus) - .end(end) -} - -function loginAndGetAccessToken (server, callback) { - login(server.url, server.client, server.user, 200, function (err, res) { - if (err) return callback(err) - - return callback(null, res.body.access_token) - }) -} - -function makeFriends (url, accessToken, expectedStatus, callback) { - if (!callback) { - callback = expectedStatus - expectedStatus = 204 - } - - const path = '/api/v1/pods/makefriends' - - // The first pod make friend with the third - request(url) - .get(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(expectedStatus) - .end(function (err, res) { - if (err) throw err - - // Wait for the request between pods - setTimeout(callback, 1000) - }) -} - -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') - .set('Authorization', 'Bearer ' + accessToken) - .expect(expectedStatus) - .end(function (err, res) { - if (err) throw err - - // Wait for the request between pods - setTimeout(callback, 1000) - }) -} - -function removeUser (url, token, username, expectedStatus, end) { - if (!end) { - end = expectedStatus - expectedStatus = 204 - } - - const path = '/api/v1/users' - - request(url) - .delete(path + '/' + username) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) - .end(end) -} - -function removeVideo (url, token, id, expectedStatus, end) { - if (!end) { - end = expectedStatus - expectedStatus = 204 - } - - const path = '/api/v1/videos' - - request(url) - .delete(path + '/' + id) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) - .end(end) -} - -function flushAndRunMultipleServers (totalServers, serversRun) { - let apps = [] - let urls = [] - let i = 0 - - function anotherServerDone (number, app, url) { - apps[number - 1] = app - urls[number - 1] = url - i++ - if (i === totalServers) { - serversRun(apps, urls) - } - } - - flushTests(function () { - for (let j = 1; j <= totalServers; j++) { - // For the virtual buffer - setTimeout(function () { - runServer(j, function (app, url) { - anotherServerDone(j, app, url) - }) - }, 1000 * j) - } - }) -} - -function runServer (number, callback) { - const server = { - app: null, - url: `http://localhost:${9000 + number}`, - client: { - id: null, - secret: null - }, - user: { - username: null, - password: null - } - } - - // These actions are async so we need to be sure that they have both been done - const serverRunString = { - 'Connected to mongodb': false, - 'Server listening on port': false - } - - const regexps = { - client_id: 'Client id: ([a-f0-9]+)', - client_secret: 'Client secret: (.+)', - user_username: 'Username: (.+)', - user_password: 'User password: (.+)' - } - - // Share the environment - const env = Object.create(process.env) - env.NODE_ENV = 'test' - env.NODE_APP_INSTANCE = number - const options = { - silent: true, - env: env, - detached: true - } - - server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) - server.app.stdout.on('data', function onStdout (data) { - let dontContinue = false - - // Capture things if we want to - for (const key of Object.keys(regexps)) { - const regexp = regexps[key] - const matches = data.toString().match(regexp) - if (matches !== null) { - if (key === 'client_id') server.client.id = matches[1] - else if (key === 'client_secret') server.client.secret = matches[1] - else if (key === 'user_username') server.user.username = matches[1] - else if (key === 'user_password') server.user.password = matches[1] - } - } - - // Check if all required sentences are here - for (const key of Object.keys(serverRunString)) { - if (data.toString().indexOf(key) !== -1) serverRunString[key] = true - if (serverRunString[key] === false) dontContinue = true - } - - // If no, there is maybe one thing not already initialized (mongodb...) - if (dontContinue === true) return - - server.app.stdout.removeListener('data', onStdout) - callback(server) - }) -} - -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/) - .end(end) -} - -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) - - 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, tags, fixture, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/videos' - - 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) -} - -function updateUser (url, userId, accessToken, newPassword, end) { - const path = '/api/v1/users/' + userId - - request(url) - .put(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send({ password: newPassword }) - .expect(204) - .end(end) -} - -// --------------------------------------------------------------------------- - -module.exports = testUtils diff --git a/server/tests/utils/clients.js b/server/tests/utils/clients.js new file mode 100644 index 000000000..e3ded493e --- /dev/null +++ b/server/tests/utils/clients.js @@ -0,0 +1,24 @@ +'use strict' + +const request = require('supertest') + +const clientsUtils = { + getClient: getClient +} + +// ---------------------- Export functions -------------------- + +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) +} + +// --------------------------------------------------------------------------- + +module.exports = clientsUtils diff --git a/server/tests/utils/login.js b/server/tests/utils/login.js new file mode 100644 index 000000000..1a5d75bc4 --- /dev/null +++ b/server/tests/utils/login.js @@ -0,0 +1,48 @@ +'use strict' + +const request = require('supertest') + +const loginUtils = { + login: login, + loginAndGetAccessToken: loginAndGetAccessToken +} + +// ---------------------- Export functions -------------------- + +function login (url, client, user, expectedStatus, end) { + if (!end) { + end = expectedStatus + expectedStatus = 200 + } + + const path = '/api/v1/users/token' + + const body = { + client_id: client.id, + client_secret: client.secret, + username: user.username, + password: user.password, + response_type: 'code', + grant_type: 'password', + scope: 'upload' + } + + request(url) + .post(path) + .type('form') + .send(body) + .expect(expectedStatus) + .end(end) +} + +function loginAndGetAccessToken (server, callback) { + login(server.url, server.client, server.user, 200, function (err, res) { + if (err) return callback(err) + + return callback(null, res.body.access_token) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = loginUtils diff --git a/server/tests/utils/miscs.js b/server/tests/utils/miscs.js new file mode 100644 index 000000000..5414cd561 --- /dev/null +++ b/server/tests/utils/miscs.js @@ -0,0 +1,21 @@ +'use strict' + +const miscsUtils = { + dateIsValid: dateIsValid +} + +// ---------------------- 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 +} + +// --------------------------------------------------------------------------- + +module.exports = miscsUtils diff --git a/server/tests/utils/pods.js b/server/tests/utils/pods.js new file mode 100644 index 000000000..366492110 --- /dev/null +++ b/server/tests/utils/pods.js @@ -0,0 +1,70 @@ +'use strict' + +const request = require('supertest') + +const podsUtils = { + getFriendsList: getFriendsList, + makeFriends: makeFriends, + quitFriends: quitFriends +} + +// ---------------------- Export functions -------------------- + +function getFriendsList (url, end) { + const path = '/api/v1/pods/' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function makeFriends (url, accessToken, expectedStatus, end) { + if (!end) { + end = expectedStatus + expectedStatus = 204 + } + + const path = '/api/v1/pods/makefriends' + + // The first pod make friend with the third + request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) + .end(function (err, res) { + if (err) throw err + + // Wait for the request between pods + setTimeout(end, 1000) + }) +} + +function quitFriends (url, accessToken, expectedStatus, end) { + if (!end) { + end = 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') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) + .end(function (err, res) { + if (err) throw err + + // Wait for the request between pods + setTimeout(end, 1000) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = podsUtils diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js new file mode 100644 index 000000000..ee7cd8c0a --- /dev/null +++ b/server/tests/utils/servers.js @@ -0,0 +1,115 @@ +'use strict' + +const childProcess = require('child_process') +const exec = childProcess.exec +const fork = childProcess.fork +const pathUtils = require('path') + +const serversUtils = { + flushAndRunMultipleServers: flushAndRunMultipleServers, + flushTests: flushTests, + runServer: runServer +} + +// ---------------------- Export functions -------------------- + +function flushAndRunMultipleServers (totalServers, serversRun) { + let apps = [] + let urls = [] + let i = 0 + + function anotherServerDone (number, app, url) { + apps[number - 1] = app + urls[number - 1] = url + i++ + if (i === totalServers) { + serversRun(apps, urls) + } + } + + flushTests(function () { + for (let j = 1; j <= totalServers; j++) { + // For the virtual buffer + setTimeout(function () { + runServer(j, function (app, url) { + anotherServerDone(j, app, url) + }) + }, 1000 * j) + } + }) +} + +function flushTests (callback) { + exec('npm run clean:server:test', callback) +} + +function runServer (number, callback) { + const server = { + app: null, + url: `http://localhost:${9000 + number}`, + client: { + id: null, + secret: null + }, + user: { + username: null, + password: null + } + } + + // These actions are async so we need to be sure that they have both been done + const serverRunString = { + 'Connected to mongodb': false, + 'Server listening on port': false + } + + const regexps = { + client_id: 'Client id: ([a-f0-9]+)', + client_secret: 'Client secret: (.+)', + user_username: 'Username: (.+)', + user_password: 'User password: (.+)' + } + + // Share the environment + const env = Object.create(process.env) + env.NODE_ENV = 'test' + env.NODE_APP_INSTANCE = number + const options = { + silent: true, + env: env, + detached: true + } + + server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) + server.app.stdout.on('data', function onStdout (data) { + let dontContinue = false + + // Capture things if we want to + for (const key of Object.keys(regexps)) { + const regexp = regexps[key] + const matches = data.toString().match(regexp) + if (matches !== null) { + if (key === 'client_id') server.client.id = matches[1] + else if (key === 'client_secret') server.client.secret = matches[1] + else if (key === 'user_username') server.user.username = matches[1] + else if (key === 'user_password') server.user.password = matches[1] + } + } + + // Check if all required sentences are here + for (const key of Object.keys(serverRunString)) { + if (data.toString().indexOf(key) !== -1) serverRunString[key] = true + if (serverRunString[key] === false) dontContinue = true + } + + // If no, there is maybe one thing not already initialized (mongodb...) + if (dontContinue === true) return + + server.app.stdout.removeListener('data', onStdout) + callback(server) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = serversUtils diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js new file mode 100644 index 000000000..ed7a9d672 --- /dev/null +++ b/server/tests/utils/users.js @@ -0,0 +1,85 @@ +'use strict' + +const request = require('supertest') + +const usersUtils = { + createUser: createUser, + getUserInformation: getUserInformation, + getUsersList: getUsersList, + removeUser: removeUser, + updateUser: updateUser +} + +// ---------------------- Export functions -------------------- + +function createUser (url, accessToken, username, password, specialStatus, end) { + if (!end) { + end = specialStatus + specialStatus = 204 + } + + const path = '/api/v1/users' + + request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send({ username: username, password: password }) + .expect(specialStatus) + .end(end) +} + +function getUserInformation (url, accessToken, end) { + const path = '/api/v1/users/me' + + request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function getUsersList (url, end) { + const path = '/api/v1/users' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function removeUser (url, token, username, expectedStatus, end) { + if (!end) { + end = expectedStatus + expectedStatus = 204 + } + + const path = '/api/v1/users' + + request(url) + .delete(path + '/' + username) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(expectedStatus) + .end(end) +} + +function updateUser (url, userId, accessToken, newPassword, end) { + const path = '/api/v1/users/' + userId + + request(url) + .put(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send({ password: newPassword }) + .expect(204) + .end(end) +} + +// --------------------------------------------------------------------------- + +module.exports = usersUtils diff --git a/server/tests/utils/videos.js b/server/tests/utils/videos.js new file mode 100644 index 000000000..90ee9621e --- /dev/null +++ b/server/tests/utils/videos.js @@ -0,0 +1,199 @@ +'use strict' + +const fs = require('fs') +const pathUtils = require('path') +const request = require('supertest') + +const videosUtils = { + getAllVideosListBy: getAllVideosListBy, + getVideo: getVideo, + getVideosList: getVideosList, + getVideosListPagination: getVideosListPagination, + getVideosListSort: getVideosListSort, + removeVideo: removeVideo, + searchVideo: searchVideo, + searchVideoWithPagination: searchVideoWithPagination, + searchVideoWithSort: searchVideoWithSort, + testVideoImage: testVideoImage, + uploadVideo: uploadVideo +} + +// ---------------------- Export functions -------------------- + +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 getVideo (url, id, end) { + const path = '/api/v1/videos/' + id + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function getVideosList (url, end) { + const path = '/api/v1/videos' + + 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/) + .end(end) +} + +function removeVideo (url, token, id, expectedStatus, end) { + if (!end) { + end = expectedStatus + expectedStatus = 204 + } + + const path = '/api/v1/videos' + + request(url) + .delete(path + '/' + id) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(expectedStatus) + .end(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/) + .end(end) +} + +function testVideoImage (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) + + fs.readFile(pathUtils.join(__dirname, '..', 'api', '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, tags, fixture, specialStatus, end) { + if (!end) { + end = specialStatus + specialStatus = 204 + } + + const path = '/api/v1/videos' + + 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, '..', 'api', 'fixtures', fixture) + } + + req.attach('videofile', filepath) + .expect(specialStatus) + .end(end) +} + +// --------------------------------------------------------------------------- + +module.exports = videosUtils