aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/real-world/tools
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-07 17:58:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-07 17:58:09 +0200
commit8df87ce792be7b0ccdad4cb703e5ca3be66d214a (patch)
treef28b8705da384f99009f1a63942616aa244d6552 /server/tests/real-world/tools
parentfdbda9e3d6564ec908733c7019305f6a3c363a9f (diff)
downloadPeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.tar.gz
PeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.tar.zst
PeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.zip
Convert real world tools to typescript
Diffstat (limited to 'server/tests/real-world/tools')
-rw-r--r--server/tests/real-world/tools/get-access-token.js45
-rw-r--r--server/tests/real-world/tools/get-access-token.ts49
-rw-r--r--server/tests/real-world/tools/upload-directory.js67
-rw-r--r--server/tests/real-world/tools/upload-directory.ts83
-rw-r--r--server/tests/real-world/tools/upload.js79
-rw-r--r--server/tests/real-world/tools/upload.ts85
6 files changed, 217 insertions, 191 deletions
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 @@
1'use strict'
2
3const program = require('commander')
4
5const utilsClient = require('../../utils/clients')
6const utilsLogin = require('../../utils/login')
7
8program
9 .option('-u, --url <url>', 'Server url')
10 .option('-n, --username <username>', 'Username')
11 .option('-p, --password <token>', 'Password')
12 .parse(process.argv)
13
14if (
15 !program.url ||
16 !program.username ||
17 !program.password
18) {
19 throw new Error('All arguments are required.')
20}
21
22const server = {
23 url: program.url,
24 user: {
25 username: program.username,
26 password: program.password
27 },
28 client: {
29 id: null,
30 secret: null
31 }
32}
33
34utilsClient.getClient(program.url, function (err, res) {
35 if (err) throw err
36
37 server.client.id = res.body.client_id
38 server.client.secret = res.body.client_secret
39
40 utilsLogin.loginAndGetAccessToken(server, function (err, accessToken) {
41 if (err) throw err
42
43 console.log(accessToken)
44 })
45})
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 @@
1import * as program from 'commander'
2import * as Promise from 'bluebird'
3
4import {
5 getClient,
6 loginAndGetAccessToken
7} from '../../utils'
8
9program
10 .option('-u, --url <url>', 'Server url')
11 .option('-n, --username <username>', 'Username')
12 .option('-p, --password <token>', 'Password')
13 .parse(process.argv)
14
15if (
16 !program['url'] ||
17 !program['username'] ||
18 !program['password']
19) {
20 throw new Error('All arguments are required.')
21}
22
23const server = {
24 url: program['url'],
25 user: {
26 username: program['username'],
27 password: program['password']
28 },
29 client: {
30 id: null,
31 secret: null
32 }
33}
34
35getClient(program.url)
36 .then(res => {
37 server.client.id = res.body.client_id
38 server.client.secret = res.body.client_secret
39
40 return loginAndGetAccessToken(server)
41 })
42 .then(accessToken => {
43 console.log(accessToken)
44 process.exit(0)
45 })
46 .catch(err => {
47 console.error(err)
48 process.exit(-1)
49 })
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 @@
1'use strict'
2
3const program = require('commander')
4const eachSeries = require('async/eachSeries')
5const exec = require('child_process').exec
6const fs = require('fs')
7const path = require('path')
8
9program
10 .option('-u, --url <url>', 'Server url')
11 .option('-n, --username <username>', 'Username')
12 .option('-p, --password <token>', 'Password')
13 .option('-i, --directory <directory>', 'Videos directory absolute path')
14 .option('-d, --description <description>', 'Video description')
15 .option('-t, --tags <tags>', 'Video tags', list)
16 .parse(process.argv)
17
18if (
19 !program.url ||
20 !program.username ||
21 !program.password ||
22 !program.directory ||
23 !program.description ||
24 !program.tags
25) {
26 throw new Error('All arguments are required.')
27}
28
29exec('node ./get-access-token -u "' + program.url + '" -n "' + program.username + '" -p "' + program.password + '"', function (err, stdout) {
30 if (err) throw err
31
32 const accessToken = stdout.replace('\n', '')
33
34 fs.readdir(program.directory, function (err, files) {
35 if (err) throw err
36
37 eachSeries(files, function (file, callbackEach) {
38 const video = {
39 tags: program.tags,
40 name: file,
41 description: program.description
42 }
43
44 let command = 'node ./upload'
45 command += ' -u "' + program.url + '"'
46 command += ' -a "' + accessToken + '"'
47 command += ' -n "' + video.name + '"'
48 command += ' -d "' + video.description + '"'
49 command += ' -t "' + video.tags.join(',') + '"'
50 command += ' -f "' + path.join(program.directory, file) + '"'
51
52 exec(command, function (err, stdout) {
53 if (err) console.log(err)
54
55 console.log(stdout)
56
57 return callbackEach()
58 })
59 })
60 })
61})
62
63// ----------------------------------------------------------------------------
64
65function list (val) {
66 return val.split(',')
67}
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 @@
1import * as program from 'commander'
2import * as Promise from 'bluebird'
3import { isAbsolute } from 'path'
4import { join } from 'path'
5
6import { readdirPromise } from '../../../helpers/core-utils'
7import { execCLI } from '../../utils'
8
9program
10 .option('-u, --url <url>', 'Server url')
11 .option('-U, --username <username>', 'Username')
12 .option('-p, --password <token>', 'Password')
13 .option('-i, --input <directory>', 'Videos directory absolute path')
14 .option('-d, --description <description>', 'Video descriptions')
15 .option('-c, --category <category>', 'Video categories')
16 .option('-l, --licence <licence>', 'Video licences')
17 .option('-t, --tags <tags>', 'Video tags', list)
18 .parse(process.argv)
19
20if (
21 !program['url'] ||
22 !program['username'] ||
23 !program['password'] ||
24 !program['input'] ||
25 !program['description'] ||
26 !program['category'] ||
27 !program['licence'] ||
28 !program['tags']
29) {
30 throw new Error('All arguments are required.')
31}
32
33if (isAbsolute(program['input']) === false) {
34 throw new Error('Input path should be absolute.')
35}
36
37let command = `npm run ts-node -- ${__dirname}/get-access-token.ts`
38command += ` -u "${program['url']}"`
39command += ` -n "${program['username']}"`
40command += ` -p "${program['password']}"`
41
42execCLI(command)
43 .then(stdout => {
44 const accessToken = stdout.replace('\n', '')
45
46 console.log(accessToken)
47
48 return readdirPromise(program['input']).then(files => ({ accessToken, files }))
49 })
50 .then(({ accessToken, files }) => {
51 return Promise.each(files, file => {
52 const video = {
53 tags: program['tags'],
54 name: file,
55 description: program['description'],
56 category: program['category'],
57 licence: program['licence']
58 }
59
60 let command = `npm run ts-node -- ${__dirname}/upload.ts`
61 command += ` -u "${program['url']}"`
62 command += ` -a "${accessToken}"`
63 command += ` -n "${video.name}"`
64 command += ` -d "${video.description}"`
65 command += ` -c "${video.category}"`
66 command += ` -l "${video.licence}"`
67 command += ` -t "${video.tags.join(',')}"`
68 command += ` -f "${join(program['input'], file)}"`
69
70 return execCLI(command).then(stdout => console.log(stdout))
71 })
72 })
73 .then(() => process.exit(0))
74 .catch(err => {
75 console.error(err)
76 process.exit(-1)
77 })
78
79// ----------------------------------------------------------------------------
80
81function list (val) {
82 return val.split(',')
83}
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 @@
1'use strict'
2
3const program = require('commander')
4const fs = require('fs')
5
6const utils = require('../../utils/videos')
7
8program
9 .option('-u, --url <url>', 'Server url')
10 .option('-a, --access-token <token>', 'Access token')
11 .option('-n, --name <name>', 'Video name')
12 .option('-x, --nsfw', 'Video is Not Safe For Work')
13 .option('-c, --category <category number>', 'Category number')
14 .option('-l, --licence <licence number>', 'Licence number')
15 .option('-g, --language <language number>', 'Language number')
16 .option('-d, --description <description>', 'Video description')
17 .option('-t, --tags <tags>', 'Video tags', list)
18 .option('-f, --file <file>', 'Video absolute file path')
19 .parse(process.argv)
20
21if (
22 !program.url ||
23 !program.accessToken ||
24 !program.name ||
25 !program.category ||
26 !program.licence ||
27 !program.language ||
28 !program.nsfw ||
29 !program.description ||
30 !program.tags ||
31 !Array.isArray(program.tags) ||
32 program.tags.length === 0 ||
33 !program.file
34) {
35 throw new Error('All arguments are required.')
36}
37
38fs.access(program.file, fs.F_OK, function (err) {
39 if (err) throw err
40
41 upload(
42 program.url,
43 program.accessToken,
44 program.name,
45 program.category,
46 program.licence,
47 program.language,
48 program.nsfw,
49 program.description,
50 program.tags,
51 program.file
52 )
53})
54
55// ----------------------------------------------------------------------------
56
57function list (val) {
58 return val.split(',')
59}
60
61function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
62 console.log('Uploading %s video...', program.name)
63
64 const videoAttributes = {
65 name,
66 category,
67 licence,
68 language,
69 nsfw,
70 description,
71 tags,
72 fixture
73 }
74 utils.uploadVideo(url, accessToken, videoAttributes, function (err) {
75 if (err) throw err
76
77 console.log('Video uploaded.')
78 })
79}
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 @@
1import * as program from 'commander'
2import { access, constants } from 'fs'
3import { isAbsolute } from 'path'
4import { promisify } from 'util'
5
6const accessPromise = promisify(access)
7
8import { uploadVideo } from '../../utils'
9
10program
11 .option('-u, --url <url>', 'Server url')
12 .option('-a, --access-token <token>', 'Access token')
13 .option('-n, --name <name>', 'Video name')
14 .option('-N, --nsfw', 'Video is Not Safe For Work')
15 .option('-c, --category <category number>', 'Category number')
16 .option('-l, --licence <licence number>', 'Licence number')
17 .option('-L, --language <language number>', 'Language number')
18 .option('-d, --description <description>', 'Video description')
19 .option('-t, --tags <tags>', 'Video tags', list)
20 .option('-f, --file <file>', 'Video absolute file path')
21 .parse(process.argv)
22
23if (!program['tags']) program['tags'] = []
24if (!program['nsfw']) program['nsfw'] = false
25
26if (
27 !program['url'] ||
28 !program['accessToken'] ||
29 !program['name'] ||
30 !program['category'] ||
31 !program['licence'] ||
32 !program['description'] ||
33 !program['file']
34) {
35 throw new Error('All arguments but tags, language and nsfw are required.')
36}
37
38if (isAbsolute(program['file']) === false) {
39 throw new Error('File path should be absolute.')
40}
41
42accessPromise(program['file'], constants.F_OK)
43 .then(() => {
44 return upload(
45 program['url'],
46 program['accessToken'],
47 program['name'],
48 program['category'],
49 program['licence'],
50 program['language'],
51 program['nsfw'],
52 program['description'],
53 program['tags'],
54 program['file']
55 )
56 })
57 .then(() => process.exit(0))
58 .catch(err => {
59 console.error(err)
60 process.exit(-1)
61 })
62
63// ----------------------------------------------------------------------------
64
65function list (val) {
66 return val.split(',')
67}
68
69function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
70 console.log('Uploading %s video...', program['name'])
71
72 const videoAttributes = {
73 name,
74 category,
75 licence,
76 language,
77 nsfw,
78 description,
79 tags,
80 fixture
81 }
82 return uploadVideo(url, accessToken, videoAttributes).then(() => {
83 console.log(`Video ${name} uploaded.`)
84 })
85}