From 0e1dc3e7c69995c691e1dd82e3c2bc68748661ca Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 4 Sep 2017 21:21:47 +0200 Subject: Convert tests to typescript --- server/tests/utils/clients.js | 24 --- server/tests/utils/clients.ts | 17 ++ server/tests/utils/config.js | 24 --- server/tests/utils/config.ts | 17 ++ server/tests/utils/index.ts | 12 ++ server/tests/utils/login.js | 57 ------ server/tests/utils/login.ts | 59 ++++++ server/tests/utils/miscs.js | 23 --- server/tests/utils/miscs.ts | 52 +++++ server/tests/utils/pods.js | 116 ------------ server/tests/utils/pods.ts | 89 +++++++++ server/tests/utils/request-schedulers.js | 25 --- server/tests/utils/request-schedulers.ts | 18 ++ server/tests/utils/requests.js | 73 ------- server/tests/utils/requests.ts | 92 +++++++++ server/tests/utils/servers.js | 117 ------------ server/tests/utils/servers.ts | 153 +++++++++++++++ server/tests/utils/users.js | 144 -------------- server/tests/utils/users.ts | 115 ++++++++++++ server/tests/utils/video-abuses.js | 73 ------- server/tests/utils/video-abuses.ts | 58 ++++++ server/tests/utils/video-blacklists.js | 29 --- server/tests/utils/video-blacklists.ts | 17 ++ server/tests/utils/videos.js | 313 ------------------------------- server/tests/utils/videos.ts | 254 +++++++++++++++++++++++++ 25 files changed, 953 insertions(+), 1018 deletions(-) delete mode 100644 server/tests/utils/clients.js create mode 100644 server/tests/utils/clients.ts delete mode 100644 server/tests/utils/config.js create mode 100644 server/tests/utils/config.ts create mode 100644 server/tests/utils/index.ts delete mode 100644 server/tests/utils/login.js create mode 100644 server/tests/utils/login.ts delete mode 100644 server/tests/utils/miscs.js create mode 100644 server/tests/utils/miscs.ts delete mode 100644 server/tests/utils/pods.js create mode 100644 server/tests/utils/pods.ts delete mode 100644 server/tests/utils/request-schedulers.js create mode 100644 server/tests/utils/request-schedulers.ts delete mode 100644 server/tests/utils/requests.js create mode 100644 server/tests/utils/requests.ts delete mode 100644 server/tests/utils/servers.js create mode 100644 server/tests/utils/servers.ts delete mode 100644 server/tests/utils/users.js create mode 100644 server/tests/utils/users.ts delete mode 100644 server/tests/utils/video-abuses.js create mode 100644 server/tests/utils/video-abuses.ts delete mode 100644 server/tests/utils/video-blacklists.js create mode 100644 server/tests/utils/video-blacklists.ts delete mode 100644 server/tests/utils/videos.js create mode 100644 server/tests/utils/videos.ts (limited to 'server/tests/utils') diff --git a/server/tests/utils/clients.js b/server/tests/utils/clients.js deleted file mode 100644 index b3ae18d01..000000000 --- a/server/tests/utils/clients.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict' - -const request = require('supertest') - -const clientsUtils = { - getClient: getClient -} - -// ---------------------- Export functions -------------------- - -function getClient (url, end) { - const path = '/api/v1/clients/local' - - 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/clients.ts b/server/tests/utils/clients.ts new file mode 100644 index 000000000..22676bb38 --- /dev/null +++ b/server/tests/utils/clients.ts @@ -0,0 +1,17 @@ +import * as request from 'supertest' + +function getClient (url: string) { + const path = '/api/v1/clients/local' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +// --------------------------------------------------------------------------- + +export { + getClient +} diff --git a/server/tests/utils/config.js b/server/tests/utils/config.js deleted file mode 100644 index 0a507a60f..000000000 --- a/server/tests/utils/config.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict' - -const request = require('supertest') - -const configsUtils = { - getConfig -} - -// ---------------------- Export functions -------------------- - -function getConfig (url, end) { - const path = '/api/v1/config' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -// --------------------------------------------------------------------------- - -module.exports = configsUtils diff --git a/server/tests/utils/config.ts b/server/tests/utils/config.ts new file mode 100644 index 000000000..d09c19c60 --- /dev/null +++ b/server/tests/utils/config.ts @@ -0,0 +1,17 @@ +import * as request from 'supertest' + +function getConfig (url: string) { + const path = '/api/v1/config' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +// --------------------------------------------------------------------------- + +export { + getConfig +} diff --git a/server/tests/utils/index.ts b/server/tests/utils/index.ts new file mode 100644 index 000000000..9077b0568 --- /dev/null +++ b/server/tests/utils/index.ts @@ -0,0 +1,12 @@ +export * from './clients' +export * from './config' +export * from './login' +export * from './miscs' +export * from './pods' +export * from './request-schedulers' +export * from './requests' +export * from './servers' +export * from './users' +export * from './video-abuses' +export * from './video-blacklists' +export * from './videos' diff --git a/server/tests/utils/login.js b/server/tests/utils/login.js deleted file mode 100644 index c984c0baf..000000000 --- a/server/tests/utils/login.js +++ /dev/null @@ -1,57 +0,0 @@ -'use strict' - -const request = require('supertest') - -const loginUtils = { - login, - loginAndGetAccessToken, - getUserAccessToken -} - -// ---------------------- 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) - }) -} - -function getUserAccessToken (server, user, callback) { - login(server.url, server.client, 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/login.ts b/server/tests/utils/login.ts new file mode 100644 index 000000000..c9d4aed44 --- /dev/null +++ b/server/tests/utils/login.ts @@ -0,0 +1,59 @@ +import * as request from 'supertest' + +import { ServerInfo } from './servers' + +type Client = { id: string, secret: string } +type User = { username: string, password: string } +type Server = { url: string, client: Client, user: User } + +function login (url: string, client: Client, user: User, 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' + } + + return request(url) + .post(path) + .type('form') + .send(body) + .expect(expectedStatus) +} + +async function loginAndGetAccessToken (server: Server) { + const res = await login(server.url, server.client, server.user, 200) + + return res.body.access_token as string +} + +async function getUserAccessToken (server, user) { + const res = await login(server.url, server.client, user, 200) + + return res.body.access_token as string +} + +function setAccessTokensToServers (servers: ServerInfo[]) { + const tasks: Promise[] = [] + + for (const server of servers) { + const p = loginAndGetAccessToken(server).then(t => server.accessToken = t) + tasks.push(p) + } + + return Promise.all(tasks) +} + +// --------------------------------------------------------------------------- + +export { + login, + loginAndGetAccessToken, + getUserAccessToken, + setAccessTokensToServers +} diff --git a/server/tests/utils/miscs.js b/server/tests/utils/miscs.js deleted file mode 100644 index c4b661496..000000000 --- a/server/tests/utils/miscs.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -const miscsUtils = { - dateIsValid -} - -// ---------------------- Export functions -------------------- - -function dateIsValid (dateString, interval) { - const dateToCheck = new Date(dateString) - const now = new Date() - - // Check if the interval is more than 2 minutes - if (!interval) interval = 120000 - - if (now - dateToCheck > interval) return false - - return true -} - -// --------------------------------------------------------------------------- - -module.exports = miscsUtils diff --git a/server/tests/utils/miscs.ts b/server/tests/utils/miscs.ts new file mode 100644 index 000000000..424b0db98 --- /dev/null +++ b/server/tests/utils/miscs.ts @@ -0,0 +1,52 @@ +import * as WebTorrent from 'webtorrent' +import { readFile, readdir } from 'fs' + +let webtorrent = new WebTorrent() + +function readFilePromise (path: string) { + return new Promise((res, rej) => { + readFile(path, (err, data) => { + if (err) return rej(err) + + return res(data) + }) + }) +} + +function readdirPromise (path: string) { + return new Promise((res, rej) => { + readdir(path, (err, files) => { + if (err) return rej(err) + + return res(files) + }) + }) +} + + // Default interval -> 2 minutes +function dateIsValid (dateString: string, interval = 120000) { + const dateToCheck = new Date(dateString) + const now = new Date() + + return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval +} + +function wait (milliseconds: number) { + return new Promise(resolve => setTimeout(resolve, milliseconds)) +} + +function webtorrentAdd (torrent: string, refreshWebTorrent = false) { + if (refreshWebTorrent === true) webtorrent = new WebTorrent() + + return new Promise(res => webtorrent.add(torrent, res)) +} + +// --------------------------------------------------------------------------- + +export { + readFilePromise, + readdirPromise, + dateIsValid, + wait, + webtorrentAdd +} diff --git a/server/tests/utils/pods.js b/server/tests/utils/pods.js deleted file mode 100644 index cdabb64a6..000000000 --- a/server/tests/utils/pods.js +++ /dev/null @@ -1,116 +0,0 @@ -'use strict' - -const request = require('supertest') - -const podsUtils = { - getFriendsList, - makeFriends, - quitFriends, - quitOneFriend -} - -// ---------------------- 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 - } - - // Which pod makes friends with which pod - const friendsMatrix = { - 'http://localhost:9001': [ - 'localhost:9002' - ], - 'http://localhost:9002': [ - 'localhost:9003' - ], - 'http://localhost:9003': [ - 'localhost:9001' - ], - 'http://localhost:9004': [ - 'localhost:9002' - ], - 'http://localhost:9005': [ - 'localhost:9001', - 'localhost:9004' - ], - 'http://localhost:9006': [ - 'localhost:9001', - 'localhost:9002', - 'localhost:9003' - ] - } - const path = '/api/v1/pods/makefriends' - - // The first pod make friend with the third - request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send({ 'hosts': friendsMatrix[url] }) - .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) - }) -} - -function quitOneFriend (url, accessToken, friendId, expectedStatus, end) { - if (!end) { - end = expectedStatus - expectedStatus = 204 - } - - const path = '/api/v1/pods/' + friendId - - request(url) - .delete(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(expectedStatus) - .end(function (err, res) { - if (err) throw err - - end() - }) -} - -// --------------------------------------------------------------------------- - -module.exports = podsUtils diff --git a/server/tests/utils/pods.ts b/server/tests/utils/pods.ts new file mode 100644 index 000000000..0bea6db97 --- /dev/null +++ b/server/tests/utils/pods.ts @@ -0,0 +1,89 @@ +import * as request from 'supertest' + +import { wait } from './miscs' + +function getFriendsList (url: string) { + const path = '/api/v1/pods/' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +async function makeFriends (url: string, accessToken: string, expectedStatus = 204) { + // Which pod makes friends with which pod + const friendsMatrix = { + 'http://localhost:9001': [ + 'localhost:9002' + ], + 'http://localhost:9002': [ + 'localhost:9003' + ], + 'http://localhost:9003': [ + 'localhost:9001' + ], + 'http://localhost:9004': [ + 'localhost:9002' + ], + 'http://localhost:9005': [ + 'localhost:9001', + 'localhost:9004' + ], + 'http://localhost:9006': [ + 'localhost:9001', + 'localhost:9002', + 'localhost:9003' + ] + } + const path = '/api/v1/pods/makefriends' + + // The first pod make friend with the third + const res = await request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send({ 'hosts': friendsMatrix[url] }) + .expect(expectedStatus) + + // Wait request propagation + await wait(1000) + + return res +} + +async function quitFriends (url: string, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/pods/quitfriends' + + // The first pod make friend with the third + const res = await request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) + + // Wait request propagation + await wait(1000) + + return res +} + +function quitOneFriend (url: string, accessToken: string, friendId: number, expectedStatus = 204) { + const path = '/api/v1/pods/' + friendId + + return request(url) + .delete(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +// --------------------------------------------------------------------------- + +export { + getFriendsList, + makeFriends, + quitFriends, + quitOneFriend +} diff --git a/server/tests/utils/request-schedulers.js b/server/tests/utils/request-schedulers.js deleted file mode 100644 index 16835ce47..000000000 --- a/server/tests/utils/request-schedulers.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' - -const request = require('supertest') - -const requestsStatsUtils = { - getRequestsStats -} - -// ---------------------- Export functions -------------------- - -function getRequestsStats (server, accessToken, callback) { - const path = '/api/v1/requests/stats' - - request(server.url) - .get(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(200) - .expect('Content-Type', /json/) - .end(callback) -} - -// --------------------------------------------------------------------------- - -module.exports = requestsStatsUtils diff --git a/server/tests/utils/request-schedulers.ts b/server/tests/utils/request-schedulers.ts new file mode 100644 index 000000000..ae8227b79 --- /dev/null +++ b/server/tests/utils/request-schedulers.ts @@ -0,0 +1,18 @@ +import * as request from 'supertest' + +function getRequestsStats (server: { url: string }, accessToken: string) { + const path = '/api/v1/requests/stats' + + return request(server.url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +// --------------------------------------------------------------------------- + +export { + getRequestsStats +} diff --git a/server/tests/utils/requests.js b/server/tests/utils/requests.js deleted file mode 100644 index 84cf3483f..000000000 --- a/server/tests/utils/requests.js +++ /dev/null @@ -1,73 +0,0 @@ -'use strict' - -const request = require('supertest') - -const requestsUtils = { - makePostUploadRequest, - makePostBodyRequest, - makePutBodyRequest -} - -// ---------------------- Export functions -------------------- - -function makePostUploadRequest (url, path, token, fields, attaches, done, statusCodeExpected) { - if (!statusCodeExpected) statusCodeExpected = 400 - - const req = request(url) - .post(path) - .set('Accept', 'application/json') - - if (token) req.set('Authorization', 'Bearer ' + token) - - Object.keys(fields).forEach(function (field) { - const value = fields[field] - - if (Array.isArray(value)) { - for (let i = 0; i < value.length; i++) { - req.field(field + '[' + i + ']', value[i]) - } - } else { - req.field(field, value) - } - }) - - Object.keys(attaches).forEach(function (attach) { - const value = attaches[attach] - req.attach(attach, value) - }) - - req.expect(statusCodeExpected) - .end(done) -} - -function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) { - if (!statusCodeExpected) statusCodeExpected = 400 - - const req = request(url) - .post(path) - .set('Accept', 'application/json') - - if (token) req.set('Authorization', 'Bearer ' + token) - - req.send(fields) - .expect(statusCodeExpected) - .end(done) -} - -function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) { - if (!statusCodeExpected) statusCodeExpected = 400 - - const req = request(url) - .put(path) - .set('Accept', 'application/json') - - if (token) req.set('Authorization', 'Bearer ' + token) - - req.send(fields) - .expect(statusCodeExpected) - .end(done) -} - -// --------------------------------------------------------------------------- - -module.exports = requestsUtils diff --git a/server/tests/utils/requests.ts b/server/tests/utils/requests.ts new file mode 100644 index 000000000..52b7a4c29 --- /dev/null +++ b/server/tests/utils/requests.ts @@ -0,0 +1,92 @@ +import * as request from 'supertest' + +function makeGetRequest (url: string, path: string) { + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function makePostUploadRequest (options: { + url: string, + path: string, + token: string, + fields: { [ fieldName: string ]: any }, + attaches: { [ attachName: string ]: any }, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .post(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + Object.keys(options.fields).forEach(field => { + const value = options.fields[field] + + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + req.field(field + '[' + i + ']', value[i]) + } + } else { + req.field(field, value) + } + }) + + Object.keys(options.attaches).forEach(attach => { + const value = options.attaches[attach] + req.attach(attach, value) + }) + + return req.expect(options.statusCodeExpected) +} + +function makePostBodyRequest (options: { + url: string, + path: string, + token?: string, + fields: { [ fieldName: string ]: any }, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .post(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + return req.send(options.fields) + .expect(options.statusCodeExpected) +} + +function makePutBodyRequest (options: { + url: string, + path: string, + token: string, + fields: { [ fieldName: string ]: any }, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .put(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + return req.send(options.fields) + .expect(options.statusCodeExpected) +} + +// --------------------------------------------------------------------------- + +export { + makeGetRequest, + makePostUploadRequest, + makePostBodyRequest, + makePutBodyRequest +} diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js deleted file mode 100644 index c753c1f1d..000000000 --- a/server/tests/utils/servers.js +++ /dev/null @@ -1,117 +0,0 @@ -'use strict' - -const childProcess = require('child_process') -const exec = childProcess.exec -const fork = childProcess.fork -const pathUtils = require('path') - -const serversUtils = { - flushAndRunMultipleServers, - flushTests, - 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 - 1)) - } - }) -} - -function flushTests (callback) { - exec('npm run clean:server:test', callback) -} - -function runServer (number, callback) { - const server = { - app: null, - url: `http://localhost:${9000 + number}`, - host: `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 = { - 'Server listening on port': false - } - const key = 'Database peertube_test' + number + ' is ready' - serverRunString[key] = false - - const regexps = { - client_id: 'Client id: (.+)', - 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, '..', '..', '..', 'dist', '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 (client/user credentials generation...) - if (dontContinue === true) return - - server.app.stdout.removeListener('data', onStdout) - callback(server) - }) -} - -// --------------------------------------------------------------------------- - -module.exports = serversUtils diff --git a/server/tests/utils/servers.ts b/server/tests/utils/servers.ts new file mode 100644 index 000000000..272a8935e --- /dev/null +++ b/server/tests/utils/servers.ts @@ -0,0 +1,153 @@ +import { ChildProcess, exec, fork } from 'child_process' +import { join } from 'path' + +interface ServerInfo { + app: ChildProcess, + url: string + host: string + + client: { + id: string, + secret: string + } + + user: { + username: string, + password: string, + email?: string + } + + accessToken?: string + + video?: { + id: number + uuid: string + } + + remoteVideo?: { + id: number + uuid: string + } +} + +async function flushAndRunMultipleServers (totalServers) { + let apps = [] + let i = 0 + + return new Promise(res => { + function anotherServerDone (serverNumber, app) { + apps[serverNumber - 1] = app + i++ + if (i === totalServers) { + return res(apps) + } + } + + flushTests() + .then(() => { + for (let j = 1; j <= totalServers; j++) { + // For the virtual buffer + setTimeout(() => { + runServer(j).then(app => anotherServerDone(j, app)) + }, 1000 * (j - 1)) + } + }) + }) +} + +function flushTests () { + return new Promise((res, rej) => { + return exec('npm run clean:server:test', err => { + if (err) return rej(err) + + return res() + }) + }) +} + +function runServer (serverNumber: number) { + const server: ServerInfo = { + app: null, + url: `http://localhost:${9000 + serverNumber}`, + host: `localhost:${9000 + serverNumber}`, + 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 = { + 'Server listening on port': false + } + const key = 'Database peertube_test' + serverNumber + ' is ready' + serverRunString[key] = false + + const regexps = { + client_id: 'Client id: (.+)', + 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'] = serverNumber.toString() + const options = { + silent: true, + env: env, + detached: true + } + + return new Promise(res => { + server.app = fork(join(__dirname, '..', '..', '..', 'dist', '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 (client/user credentials generation...) + if (dontContinue === true) return + + server.app.stdout.removeListener('data', onStdout) + res(server) + }) + }) +} + +function killallServers (servers: ServerInfo[]) { + for (const server of servers) { + process.kill(-server.app.pid) + } +} + +// --------------------------------------------------------------------------- + +export { + ServerInfo, + flushAndRunMultipleServers, + flushTests, + runServer, + killallServers +} diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js deleted file mode 100644 index 310dc0c6b..000000000 --- a/server/tests/utils/users.js +++ /dev/null @@ -1,144 +0,0 @@ -'use strict' - -const request = require('supertest') - -const usersUtils = { - createUser, - registerUser, - getUserInformation, - getUserVideoRating, - getUsersList, - getUsersListPaginationAndSort, - removeUser, - updateUser -} - -// ---------------------- Export functions -------------------- - -function createUser (url, accessToken, username, password, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/users' - const body = { - username, - password, - email: username + '@example.com' - } - - request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send(body) - .expect(specialStatus) - .end(end) -} - -function registerUser (url, username, password, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/users/register' - const body = { - username, - password, - email: username + '@example.com' - } - - request(url) - .post(path) - .set('Accept', 'application/json') - .send(body) - .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 getUserVideoRating (url, accessToken, videoId, end) { - const path = '/api/v1/users/me/videos/' + videoId + '/rating' - - 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 getUsersListPaginationAndSort (url, start, count, sort, end) { - const path = '/api/v1/users' - - request(url) - .get(path) - .query({ start: start }) - .query({ count: count }) - .query({ sort: sort }) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function removeUser (url, userId, accessToken, expectedStatus, end) { - if (!end) { - end = expectedStatus - expectedStatus = 204 - } - - const path = '/api/v1/users' - - request(url) - .delete(path + '/' + userId) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(expectedStatus) - .end(end) -} - -function updateUser (url, userId, accessToken, newPassword, displayNSFW, end) { - const path = '/api/v1/users/' + userId - - const toSend = {} - if (newPassword !== undefined && newPassword !== null) toSend.password = newPassword - if (displayNSFW !== undefined && displayNSFW !== null) toSend.displayNSFW = displayNSFW - - request(url) - .put(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send(toSend) - .expect(204) - .end(end) -} - -// --------------------------------------------------------------------------- - -module.exports = usersUtils diff --git a/server/tests/utils/users.ts b/server/tests/utils/users.ts new file mode 100644 index 000000000..6e4b5f2f5 --- /dev/null +++ b/server/tests/utils/users.ts @@ -0,0 +1,115 @@ +import * as request from 'supertest' + +function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) { + const path = '/api/v1/users' + const body = { + username, + password, + email: username + '@example.com' + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send(body) + .expect(specialStatus) +} + +function registerUser (url: string, username: string, password: string, specialStatus = 204) { + const path = '/api/v1/users/register' + const body = { + username, + password, + email: username + '@example.com' + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .send(body) + .expect(specialStatus) +} + +function getUserInformation (url: string, accessToken: string) { + const path = '/api/v1/users/me' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getUserVideoRating (url: string, accessToken: string, videoId: number) { + const path = '/api/v1/users/me/videos/' + videoId + '/rating' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getUsersList (url: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function getUsersListPaginationAndSort (url: string, start: number, count: number, sort: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .query({ start }) + .query({ count }) + .query({ sort }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/users' + + return request(url) + .delete(path + '/' + userId) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) { + const path = '/api/v1/users/' + userId + + const toSend = {} + if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword + if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW + + return request(url) + .put(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send(toSend) + .expect(204) +} + +// --------------------------------------------------------------------------- + +export { + createUser, + registerUser, + getUserInformation, + getUserVideoRating, + getUsersList, + getUsersListPaginationAndSort, + removeUser, + updateUser +} diff --git a/server/tests/utils/video-abuses.js b/server/tests/utils/video-abuses.js deleted file mode 100644 index c4dd87990..000000000 --- a/server/tests/utils/video-abuses.js +++ /dev/null @@ -1,73 +0,0 @@ -'use strict' - -const request = require('supertest') - -const videosAbuseUtils = { - getVideoAbusesList, - getVideoAbusesListPagination, - getVideoAbusesListSort, - reportVideoAbuse -} - -// ---------------------- Export functions -------------------- - -function reportVideoAbuse (url, token, videoId, reason, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/videos/' + videoId + '/abuse' - - request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .send({ reason }) - .expect(specialStatus) - .end(end) -} - -function getVideoAbusesList (url, token, end) { - const path = '/api/v1/videos/abuse' - - request(url) - .get(path) - .query({ sort: 'createdAt' }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideoAbusesListPagination (url, token, start, count, end) { - const path = '/api/v1/videos/abuse' - - request(url) - .get(path) - .query({ start: start }) - .query({ count: count }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideoAbusesListSort (url, token, sort, end) { - const path = '/api/v1/videos/abuse' - - request(url) - .get(path) - .query({ sort: sort }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -// --------------------------------------------------------------------------- - -module.exports = videosAbuseUtils diff --git a/server/tests/utils/video-abuses.ts b/server/tests/utils/video-abuses.ts new file mode 100644 index 000000000..f7ee958d7 --- /dev/null +++ b/server/tests/utils/video-abuses.ts @@ -0,0 +1,58 @@ +import * as request from 'supertest' + +function reportVideoAbuse (url: string, token: string, videoId: number, reason: string, specialStatus = 204) { + const path = '/api/v1/videos/' + videoId + '/abuse' + + return request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .send({ reason }) + .expect(specialStatus) +} + +function getVideoAbusesList (url: string, token: string) { + const path = '/api/v1/videos/abuse' + + return request(url) + .get(path) + .query({ sort: 'createdAt' }) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideoAbusesListPagination (url: string, token: string, start: number, count: number) { + const path = '/api/v1/videos/abuse' + + return request(url) + .get(path) + .query({ start: start }) + .query({ count: count }) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideoAbusesListSort (url: string, token: string, sort: string) { + const path = '/api/v1/videos/abuse' + + return request(url) + .get(path) + .query({ sort: sort }) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(200) + .expect('Content-Type', /json/) +} + +// --------------------------------------------------------------------------- + +export { + reportVideoAbuse, + getVideoAbusesList, + getVideoAbusesListPagination, + getVideoAbusesListSort +} diff --git a/server/tests/utils/video-blacklists.js b/server/tests/utils/video-blacklists.js deleted file mode 100644 index 0a58dd631..000000000 --- a/server/tests/utils/video-blacklists.js +++ /dev/null @@ -1,29 +0,0 @@ -'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 diff --git a/server/tests/utils/video-blacklists.ts b/server/tests/utils/video-blacklists.ts new file mode 100644 index 000000000..6812d3ad4 --- /dev/null +++ b/server/tests/utils/video-blacklists.ts @@ -0,0 +1,17 @@ +import * as request from 'supertest' + +function addVideoToBlacklist (url: string, token: string, videoId: number, specialStatus = 204) { + const path = '/api/v1/videos/' + videoId + '/blacklist' + + return request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(specialStatus) +} + +// --------------------------------------------------------------------------- + +export { + addVideoToBlacklist +} diff --git a/server/tests/utils/videos.js b/server/tests/utils/videos.js deleted file mode 100644 index cb3be6897..000000000 --- a/server/tests/utils/videos.js +++ /dev/null @@ -1,313 +0,0 @@ -'use strict' - -const fs = require('fs') -const pathUtils = require('path') -const request = require('supertest') - -const videosUtils = { - getVideoCategories, - getVideoLicences, - getVideoLanguages, - getAllVideosListBy, - getVideo, - getVideosList, - getVideosListPagination, - getVideosListSort, - removeVideo, - searchVideo, - searchVideoWithPagination, - searchVideoWithSort, - testVideoImage, - uploadVideo, - updateVideo, - rateVideo -} - -// ---------------------- Export functions -------------------- - -function getVideoCategories (url, end) { - const path = '/api/v1/videos/categories' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideoLicences (url, end) { - const path = '/api/v1/videos/licences' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getVideoLanguages (url, end) { - const path = '/api/v1/videos/languages' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) -} - -function getAllVideosListBy (url, end) { - const path = '/api/v1/videos' - - request(url) - .get(path) - .query({ sort: 'createdAt' }) - .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, sort, end) { - if (!end) { - end = sort - sort = null - } - - const path = '/api/v1/videos' - - const req = request(url) - .get(path) - .query({ start: start }) - .query({ count: count }) - - if (sort) req.query({ sort }) - - req.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, sort, end) { - if (!end) { - end = sort - sort = null - } - - const path = '/api/v1/videos' - - const req = request(url) - .get(path + '/search/' + search) - .query({ start: start }) - .query({ count: count }) - .query({ field: field }) - - if (sort) req.query({ sort }) - - req.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, imageName, 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', imageName + '.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, videoAttributesArg, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/videos' - - // Default attributes - let attributes = { - name: 'my super video', - category: 5, - licence: 4, - language: 3, - nsfw: true, - description: 'my super description', - tags: [ 'tag' ], - fixture: 'video_short.webm' - } - attributes = Object.assign(attributes, videoAttributesArg) - - const req = request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .field('name', attributes.name) - .field('category', attributes.category) - .field('licence', attributes.licence) - .field('language', attributes.language) - .field('nsfw', attributes.nsfw) - .field('description', attributes.description) - - for (let i = 0; i < attributes.tags.length; i++) { - req.field('tags[' + i + ']', attributes.tags[i]) - } - - let filepath = '' - if (pathUtils.isAbsolute(attributes.fixture)) { - filepath = attributes.fixture - } else { - filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture) - } - - req.attach('videofile', filepath) - .expect(specialStatus) - .end(end) -} - -function updateVideo (url, accessToken, id, attributes, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/videos/' + id - const body = {} - - if (attributes.name) body.name = attributes.name - if (attributes.category) body.category = attributes.category - if (attributes.licence) body.licence = attributes.licence - if (attributes.language) body.language = attributes.language - if (attributes.nsfw) body.nsfw = attributes.nsfw - if (attributes.description) body.description = attributes.description - if (attributes.tags) body.tags = attributes.tags - - request(url) - .put(path) - .send(body) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(specialStatus) - .end(end) -} - -function rateVideo (url, accessToken, id, rating, specialStatus, end) { - if (!end) { - end = specialStatus - specialStatus = 204 - } - - const path = '/api/v1/videos/' + id + '/rate' - - request(url) - .put(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .send({ rating }) - .expect(specialStatus) - .end(end) -} - -// --------------------------------------------------------------------------- - -module.exports = videosUtils diff --git a/server/tests/utils/videos.ts b/server/tests/utils/videos.ts new file mode 100644 index 000000000..42b7dd05a --- /dev/null +++ b/server/tests/utils/videos.ts @@ -0,0 +1,254 @@ +import * as request from 'supertest' +import { join, isAbsolute } from 'path' + +import { makeGetRequest } from './requests' +import { readFilePromise } from './miscs' + +type VideoAttributes = { + name?: string + category?: number + licence?: number + language?: number + nsfw?: boolean + description?: string + tags?: string[] + fixture?: string +} + +function getVideoCategories (url: string) { + const path = '/api/v1/videos/categories' + + return makeGetRequest(url, path) +} + +function getVideoLicences (url: string) { + const path = '/api/v1/videos/licences' + + return makeGetRequest(url, path) +} + +function getVideoLanguages (url: string) { + const path = '/api/v1/videos/languages' + + return makeGetRequest(url, path) +} + +function getAllVideosListBy (url: string) { + const path = '/api/v1/videos' + + return request(url) + .get(path) + .query({ sort: 'createdAt' }) + .query({ start: 0 }) + .query({ count: 10000 }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideo (url: string, id: number | string) { + const path = '/api/v1/videos/' + id + + return request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideosList (url: string) { + const path = '/api/v1/videos' + + return request(url) + .get(path) + .query({ sort: 'name' }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideosListPagination (url: string, start: number, count: number, sort?: string) { + const path = '/api/v1/videos' + + const req = request(url) + .get(path) + .query({ start: start }) + .query({ count: count }) + + if (sort) req.query({ sort }) + + return req.set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function getVideosListSort (url: string, sort: string) { + const path = '/api/v1/videos' + + return request(url) + .get(path) + .query({ sort: sort }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function removeVideo (url: string, token: string, id: number, expectedStatus = 204) { + const path = '/api/v1/videos' + + return request(url) + .delete(path + '/' + id) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + token) + .expect(expectedStatus) +} + +function searchVideo (url: string, search: string, field?: string) { + const path = '/api/v1/videos' + const req = request(url) + .get(path + '/search/' + search) + .set('Accept', 'application/json') + + if (field) req.query({ field }) + + return req.expect(200) + .expect('Content-Type', /json/) +} + +function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) { + const path = '/api/v1/videos' + + const req = request(url) + .get(path + '/search/' + search) + .query({ start }) + .query({ count }) + .query({ field }) + + if (sort) req.query({ sort }) + + return req.set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +function searchVideoWithSort (url: string, search: string, sort: string) { + const path = '/api/v1/videos' + + return request(url) + .get(path + '/search/' + search) + .query({ sort }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + +async function testVideoImage (url: string, imageName: string, imagePath: string) { + // 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']) { + const res = await request(url) + .get(imagePath) + .expect(200) + + const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg')) + + return data.equals(res.body) + } else { + console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') + return true + } +} + +function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) { + const path = '/api/v1/videos' + + // Default attributes + let attributes = { + name: 'my super video', + category: 5, + licence: 4, + language: 3, + nsfw: true, + description: 'my super description', + tags: [ 'tag' ], + fixture: 'video_short.webm' + } + attributes = Object.assign(attributes, videoAttributesArg) + + const req = request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .field('name', attributes.name) + .field('category', attributes.category.toString()) + .field('licence', attributes.licence.toString()) + .field('language', attributes.language.toString()) + .field('nsfw', JSON.stringify(attributes.nsfw)) + .field('description', attributes.description) + + for (let i = 0; i < attributes.tags.length; i++) { + req.field('tags[' + i + ']', attributes.tags[i]) + } + + let filepath = '' + if (isAbsolute(attributes.fixture)) { + filepath = attributes.fixture + } else { + filepath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture) + } + + return req.attach('videofile', filepath) + .expect(specialStatus) +} + +function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) { + const path = '/api/v1/videos/' + id + const body = {} + + if (attributes.name) body['name'] = attributes.name + if (attributes.category) body['category'] = attributes.category + if (attributes.licence) body['licence'] = attributes.licence + if (attributes.language) body['language'] = attributes.language + if (attributes.nsfw) body['nsfw'] = attributes.nsfw + if (attributes.description) body['description'] = attributes.description + if (attributes.tags) body['tags'] = attributes.tags + + return request(url) + .put(path) + .send(body) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) +} + +function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { + const path = '/api/v1/videos/' + id + '/rate' + + return request(url) + .put(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send({ rating }) + .expect(specialStatus) +} + +// --------------------------------------------------------------------------- + +export { + getVideoCategories, + getVideoLicences, + getVideoLanguages, + getAllVideosListBy, + getVideo, + getVideosList, + getVideosListPagination, + getVideosListSort, + removeVideo, + searchVideo, + searchVideoWithPagination, + searchVideoWithSort, + testVideoImage, + uploadVideo, + updateVideo, + rateVideo +} -- cgit v1.2.3