From 8df87ce792be7b0ccdad4cb703e5ca3be66d214a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 7 Sep 2017 17:58:09 +0200 Subject: Convert real world tools to typescript --- server/tests/cli/index.ts | 1 + server/tests/real-world/tools/get-access-token.js | 45 ------------ server/tests/real-world/tools/get-access-token.ts | 49 +++++++++++++ server/tests/real-world/tools/upload-directory.js | 67 ------------------ server/tests/real-world/tools/upload-directory.ts | 83 ++++++++++++++++++++++ server/tests/real-world/tools/upload.js | 79 --------------------- server/tests/real-world/tools/upload.ts | 85 +++++++++++++++++++++++ server/tests/utils/cli.ts | 2 +- server/tests/utils/clients.ts | 2 +- server/tests/utils/videos.ts | 5 +- 10 files changed, 224 insertions(+), 194 deletions(-) delete mode 100644 server/tests/real-world/tools/get-access-token.js create mode 100644 server/tests/real-world/tools/get-access-token.ts delete mode 100644 server/tests/real-world/tools/upload-directory.js create mode 100644 server/tests/real-world/tools/upload-directory.ts delete mode 100644 server/tests/real-world/tools/upload.js create mode 100644 server/tests/real-world/tools/upload.ts (limited to 'server') diff --git a/server/tests/cli/index.ts b/server/tests/cli/index.ts index e5a19ff10..ecef9bd24 100644 --- a/server/tests/cli/index.ts +++ b/server/tests/cli/index.ts @@ -1,2 +1,3 @@ // Order of the tests we want to execute import './reset-password' +import './update-host' diff --git a/server/tests/real-world/tools/get-access-token.js b/server/tests/real-world/tools/get-access-token.js deleted file mode 100644 index 483cefa95..000000000 --- a/server/tests/real-world/tools/get-access-token.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict' - -const program = require('commander') - -const utilsClient = require('../../utils/clients') -const utilsLogin = require('../../utils/login') - -program - .option('-u, --url ', 'Server url') - .option('-n, --username ', 'Username') - .option('-p, --password ', 'Password') - .parse(process.argv) - -if ( - !program.url || - !program.username || - !program.password -) { - throw new Error('All arguments are required.') -} - -const server = { - url: program.url, - user: { - username: program.username, - password: program.password - }, - client: { - id: null, - secret: null - } -} - -utilsClient.getClient(program.url, function (err, res) { - if (err) throw err - - server.client.id = res.body.client_id - server.client.secret = res.body.client_secret - - utilsLogin.loginAndGetAccessToken(server, function (err, accessToken) { - if (err) throw err - - console.log(accessToken) - }) -}) diff --git a/server/tests/real-world/tools/get-access-token.ts b/server/tests/real-world/tools/get-access-token.ts new file mode 100644 index 000000000..a2f076039 --- /dev/null +++ b/server/tests/real-world/tools/get-access-token.ts @@ -0,0 +1,49 @@ +import * as program from 'commander' +import * as Promise from 'bluebird' + +import { + getClient, + loginAndGetAccessToken +} from '../../utils' + +program + .option('-u, --url ', 'Server url') + .option('-n, --username ', 'Username') + .option('-p, --password ', 'Password') + .parse(process.argv) + +if ( + !program['url'] || + !program['username'] || + !program['password'] +) { + throw new Error('All arguments are required.') +} + +const server = { + url: program['url'], + user: { + username: program['username'], + password: program['password'] + }, + client: { + id: null, + secret: null + } +} + +getClient(program.url) + .then(res => { + server.client.id = res.body.client_id + server.client.secret = res.body.client_secret + + return loginAndGetAccessToken(server) + }) + .then(accessToken => { + console.log(accessToken) + process.exit(0) + }) + .catch(err => { + console.error(err) + process.exit(-1) + }) diff --git a/server/tests/real-world/tools/upload-directory.js b/server/tests/real-world/tools/upload-directory.js deleted file mode 100644 index aed7a1fd8..000000000 --- a/server/tests/real-world/tools/upload-directory.js +++ /dev/null @@ -1,67 +0,0 @@ -'use strict' - -const program = require('commander') -const eachSeries = require('async/eachSeries') -const exec = require('child_process').exec -const fs = require('fs') -const path = require('path') - -program - .option('-u, --url ', 'Server url') - .option('-n, --username ', 'Username') - .option('-p, --password ', 'Password') - .option('-i, --directory ', 'Videos directory absolute path') - .option('-d, --description ', 'Video description') - .option('-t, --tags ', 'Video tags', list) - .parse(process.argv) - -if ( - !program.url || - !program.username || - !program.password || - !program.directory || - !program.description || - !program.tags -) { - throw new Error('All arguments are required.') -} - -exec('node ./get-access-token -u "' + program.url + '" -n "' + program.username + '" -p "' + program.password + '"', function (err, stdout) { - if (err) throw err - - const accessToken = stdout.replace('\n', '') - - fs.readdir(program.directory, function (err, files) { - if (err) throw err - - eachSeries(files, function (file, callbackEach) { - const video = { - tags: program.tags, - name: file, - description: program.description - } - - let command = 'node ./upload' - command += ' -u "' + program.url + '"' - command += ' -a "' + accessToken + '"' - command += ' -n "' + video.name + '"' - command += ' -d "' + video.description + '"' - command += ' -t "' + video.tags.join(',') + '"' - command += ' -f "' + path.join(program.directory, file) + '"' - - exec(command, function (err, stdout) { - if (err) console.log(err) - - console.log(stdout) - - return callbackEach() - }) - }) - }) -}) - -// ---------------------------------------------------------------------------- - -function list (val) { - return val.split(',') -} diff --git a/server/tests/real-world/tools/upload-directory.ts b/server/tests/real-world/tools/upload-directory.ts new file mode 100644 index 000000000..a8ab1669d --- /dev/null +++ b/server/tests/real-world/tools/upload-directory.ts @@ -0,0 +1,83 @@ +import * as program from 'commander' +import * as Promise from 'bluebird' +import { isAbsolute } from 'path' +import { join } from 'path' + +import { readdirPromise } from '../../../helpers/core-utils' +import { execCLI } from '../../utils' + +program + .option('-u, --url ', 'Server url') + .option('-U, --username ', 'Username') + .option('-p, --password ', 'Password') + .option('-i, --input ', 'Videos directory absolute path') + .option('-d, --description ', 'Video descriptions') + .option('-c, --category ', 'Video categories') + .option('-l, --licence ', 'Video licences') + .option('-t, --tags ', 'Video tags', list) + .parse(process.argv) + +if ( + !program['url'] || + !program['username'] || + !program['password'] || + !program['input'] || + !program['description'] || + !program['category'] || + !program['licence'] || + !program['tags'] +) { + throw new Error('All arguments are required.') +} + +if (isAbsolute(program['input']) === false) { + throw new Error('Input path should be absolute.') +} + +let command = `npm run ts-node -- ${__dirname}/get-access-token.ts` +command += ` -u "${program['url']}"` +command += ` -n "${program['username']}"` +command += ` -p "${program['password']}"` + +execCLI(command) + .then(stdout => { + const accessToken = stdout.replace('\n', '') + + console.log(accessToken) + + return readdirPromise(program['input']).then(files => ({ accessToken, files })) + }) + .then(({ accessToken, files }) => { + return Promise.each(files, file => { + const video = { + tags: program['tags'], + name: file, + description: program['description'], + category: program['category'], + licence: program['licence'] + } + + let command = `npm run ts-node -- ${__dirname}/upload.ts` + command += ` -u "${program['url']}"` + command += ` -a "${accessToken}"` + command += ` -n "${video.name}"` + command += ` -d "${video.description}"` + command += ` -c "${video.category}"` + command += ` -l "${video.licence}"` + command += ` -t "${video.tags.join(',')}"` + command += ` -f "${join(program['input'], file)}"` + + return execCLI(command).then(stdout => console.log(stdout)) + }) + }) + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +// ---------------------------------------------------------------------------- + +function list (val) { + return val.split(',') +} diff --git a/server/tests/real-world/tools/upload.js b/server/tests/real-world/tools/upload.js deleted file mode 100644 index efb91e228..000000000 --- a/server/tests/real-world/tools/upload.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict' - -const program = require('commander') -const fs = require('fs') - -const utils = require('../../utils/videos') - -program - .option('-u, --url ', 'Server url') - .option('-a, --access-token ', 'Access token') - .option('-n, --name ', 'Video name') - .option('-x, --nsfw', 'Video is Not Safe For Work') - .option('-c, --category ', 'Category number') - .option('-l, --licence ', 'Licence number') - .option('-g, --language ', 'Language number') - .option('-d, --description ', 'Video description') - .option('-t, --tags ', 'Video tags', list) - .option('-f, --file ', 'Video absolute file path') - .parse(process.argv) - -if ( - !program.url || - !program.accessToken || - !program.name || - !program.category || - !program.licence || - !program.language || - !program.nsfw || - !program.description || - !program.tags || - !Array.isArray(program.tags) || - program.tags.length === 0 || - !program.file -) { - throw new Error('All arguments are required.') -} - -fs.access(program.file, fs.F_OK, function (err) { - if (err) throw err - - upload( - program.url, - program.accessToken, - program.name, - program.category, - program.licence, - program.language, - program.nsfw, - program.description, - program.tags, - program.file - ) -}) - -// ---------------------------------------------------------------------------- - -function list (val) { - return val.split(',') -} - -function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) { - console.log('Uploading %s video...', program.name) - - const videoAttributes = { - name, - category, - licence, - language, - nsfw, - description, - tags, - fixture - } - utils.uploadVideo(url, accessToken, videoAttributes, function (err) { - if (err) throw err - - console.log('Video uploaded.') - }) -} diff --git a/server/tests/real-world/tools/upload.ts b/server/tests/real-world/tools/upload.ts new file mode 100644 index 000000000..81bc0d415 --- /dev/null +++ b/server/tests/real-world/tools/upload.ts @@ -0,0 +1,85 @@ +import * as program from 'commander' +import { access, constants } from 'fs' +import { isAbsolute } from 'path' +import { promisify } from 'util' + +const accessPromise = promisify(access) + +import { uploadVideo } from '../../utils' + +program + .option('-u, --url ', 'Server url') + .option('-a, --access-token ', 'Access token') + .option('-n, --name ', 'Video name') + .option('-N, --nsfw', 'Video is Not Safe For Work') + .option('-c, --category ', 'Category number') + .option('-l, --licence ', 'Licence number') + .option('-L, --language ', 'Language number') + .option('-d, --description ', 'Video description') + .option('-t, --tags ', 'Video tags', list) + .option('-f, --file ', 'Video absolute file path') + .parse(process.argv) + +if (!program['tags']) program['tags'] = [] +if (!program['nsfw']) program['nsfw'] = false + +if ( + !program['url'] || + !program['accessToken'] || + !program['name'] || + !program['category'] || + !program['licence'] || + !program['description'] || + !program['file'] +) { + throw new Error('All arguments but tags, language and nsfw are required.') +} + +if (isAbsolute(program['file']) === false) { + throw new Error('File path should be absolute.') +} + +accessPromise(program['file'], constants.F_OK) + .then(() => { + return upload( + program['url'], + program['accessToken'], + program['name'], + program['category'], + program['licence'], + program['language'], + program['nsfw'], + program['description'], + program['tags'], + program['file'] + ) + }) + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +// ---------------------------------------------------------------------------- + +function list (val) { + return val.split(',') +} + +function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) { + console.log('Uploading %s video...', program['name']) + + const videoAttributes = { + name, + category, + licence, + language, + nsfw, + description, + tags, + fixture + } + return uploadVideo(url, accessToken, videoAttributes).then(() => { + console.log(`Video ${name} uploaded.`) + }) +} diff --git a/server/tests/utils/cli.ts b/server/tests/utils/cli.ts index 5f07a832e..4098fdf6f 100644 --- a/server/tests/utils/cli.ts +++ b/server/tests/utils/cli.ts @@ -7,7 +7,7 @@ function getEnvCli (server?: ServerInfo) { } async function execCLI (command: string) { - return new Promise((res, rej) => { + return new Promise((res, rej) => { exec(command, (err, stdout, stderr) => { if (err) return rej(err) diff --git a/server/tests/utils/clients.ts b/server/tests/utils/clients.ts index 22676bb38..a8c5b51c5 100644 --- a/server/tests/utils/clients.ts +++ b/server/tests/utils/clients.ts @@ -1,7 +1,7 @@ import * as request from 'supertest' function getClient (url: string) { - const path = '/api/v1/clients/local' + const path = '/api/v1/oauth-clients/local' return request(url) .get(path) diff --git a/server/tests/utils/videos.ts b/server/tests/utils/videos.ts index 509a2430a..83271beca 100644 --- a/server/tests/utils/videos.ts +++ b/server/tests/utils/videos.ts @@ -185,10 +185,13 @@ function uploadVideo (url: string, accessToken: string, videoAttributesArg: Vide .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) + if (attributes.language !== undefined) { + req.field('language', attributes.language.toString()) + } + for (let i = 0; i < attributes.tags.length; i++) { req.field('tags[' + i + ']', attributes.tags[i]) } -- cgit v1.2.3