diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-07 17:58:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-07 17:58:09 +0200 |
commit | 8df87ce792be7b0ccdad4cb703e5ca3be66d214a (patch) | |
tree | f28b8705da384f99009f1a63942616aa244d6552 /server | |
parent | fdbda9e3d6564ec908733c7019305f6a3c363a9f (diff) | |
download | PeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.tar.gz PeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.tar.zst PeerTube-8df87ce792be7b0ccdad4cb703e5ca3be66d214a.zip |
Convert real world tools to typescript
Diffstat (limited to 'server')
-rw-r--r-- | server/tests/cli/index.ts | 1 | ||||
-rw-r--r-- | server/tests/real-world/tools/get-access-token.js | 45 | ||||
-rw-r--r-- | server/tests/real-world/tools/get-access-token.ts | 49 | ||||
-rw-r--r-- | server/tests/real-world/tools/upload-directory.js | 67 | ||||
-rw-r--r-- | server/tests/real-world/tools/upload-directory.ts | 83 | ||||
-rw-r--r-- | server/tests/real-world/tools/upload.js | 79 | ||||
-rw-r--r-- | server/tests/real-world/tools/upload.ts | 85 | ||||
-rw-r--r-- | server/tests/utils/cli.ts | 2 | ||||
-rw-r--r-- | server/tests/utils/clients.ts | 2 | ||||
-rw-r--r-- | server/tests/utils/videos.ts | 5 |
10 files changed, 224 insertions, 194 deletions
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 @@ | |||
1 | // Order of the tests we want to execute | 1 | // Order of the tests we want to execute |
2 | import './reset-password' | 2 | import './reset-password' |
3 | 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 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const program = require('commander') | ||
4 | |||
5 | const utilsClient = require('../../utils/clients') | ||
6 | const utilsLogin = require('../../utils/login') | ||
7 | |||
8 | program | ||
9 | .option('-u, --url <url>', 'Server url') | ||
10 | .option('-n, --username <username>', 'Username') | ||
11 | .option('-p, --password <token>', 'Password') | ||
12 | .parse(process.argv) | ||
13 | |||
14 | if ( | ||
15 | !program.url || | ||
16 | !program.username || | ||
17 | !program.password | ||
18 | ) { | ||
19 | throw new Error('All arguments are required.') | ||
20 | } | ||
21 | |||
22 | const 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 | |||
34 | utilsClient.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 @@ | |||
1 | import * as program from 'commander' | ||
2 | import * as Promise from 'bluebird' | ||
3 | |||
4 | import { | ||
5 | getClient, | ||
6 | loginAndGetAccessToken | ||
7 | } from '../../utils' | ||
8 | |||
9 | program | ||
10 | .option('-u, --url <url>', 'Server url') | ||
11 | .option('-n, --username <username>', 'Username') | ||
12 | .option('-p, --password <token>', 'Password') | ||
13 | .parse(process.argv) | ||
14 | |||
15 | if ( | ||
16 | !program['url'] || | ||
17 | !program['username'] || | ||
18 | !program['password'] | ||
19 | ) { | ||
20 | throw new Error('All arguments are required.') | ||
21 | } | ||
22 | |||
23 | const 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 | |||
35 | getClient(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 | |||
3 | const program = require('commander') | ||
4 | const eachSeries = require('async/eachSeries') | ||
5 | const exec = require('child_process').exec | ||
6 | const fs = require('fs') | ||
7 | const path = require('path') | ||
8 | |||
9 | program | ||
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 | |||
18 | if ( | ||
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 | |||
29 | exec('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 | |||
65 | function 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 @@ | |||
1 | import * as program from 'commander' | ||
2 | import * as Promise from 'bluebird' | ||
3 | import { isAbsolute } from 'path' | ||
4 | import { join } from 'path' | ||
5 | |||
6 | import { readdirPromise } from '../../../helpers/core-utils' | ||
7 | import { execCLI } from '../../utils' | ||
8 | |||
9 | program | ||
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 | |||
20 | if ( | ||
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 | |||
33 | if (isAbsolute(program['input']) === false) { | ||
34 | throw new Error('Input path should be absolute.') | ||
35 | } | ||
36 | |||
37 | let command = `npm run ts-node -- ${__dirname}/get-access-token.ts` | ||
38 | command += ` -u "${program['url']}"` | ||
39 | command += ` -n "${program['username']}"` | ||
40 | command += ` -p "${program['password']}"` | ||
41 | |||
42 | execCLI(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 | |||
81 | function 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 | |||
3 | const program = require('commander') | ||
4 | const fs = require('fs') | ||
5 | |||
6 | const utils = require('../../utils/videos') | ||
7 | |||
8 | program | ||
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 | |||
21 | if ( | ||
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 | |||
38 | fs.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 | |||
57 | function list (val) { | ||
58 | return val.split(',') | ||
59 | } | ||
60 | |||
61 | function 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 @@ | |||
1 | import * as program from 'commander' | ||
2 | import { access, constants } from 'fs' | ||
3 | import { isAbsolute } from 'path' | ||
4 | import { promisify } from 'util' | ||
5 | |||
6 | const accessPromise = promisify(access) | ||
7 | |||
8 | import { uploadVideo } from '../../utils' | ||
9 | |||
10 | program | ||
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 | |||
23 | if (!program['tags']) program['tags'] = [] | ||
24 | if (!program['nsfw']) program['nsfw'] = false | ||
25 | |||
26 | if ( | ||
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 | |||
38 | if (isAbsolute(program['file']) === false) { | ||
39 | throw new Error('File path should be absolute.') | ||
40 | } | ||
41 | |||
42 | accessPromise(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 | |||
65 | function list (val) { | ||
66 | return val.split(',') | ||
67 | } | ||
68 | |||
69 | function 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 | } | ||
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) { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | async function execCLI (command: string) { | 9 | async function execCLI (command: string) { |
10 | return new Promise((res, rej) => { | 10 | return new Promise<string>((res, rej) => { |
11 | exec(command, (err, stdout, stderr) => { | 11 | exec(command, (err, stdout, stderr) => { |
12 | if (err) return rej(err) | 12 | if (err) return rej(err) |
13 | 13 | ||
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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | 2 | ||
3 | function getClient (url: string) { | 3 | function getClient (url: string) { |
4 | const path = '/api/v1/clients/local' | 4 | const path = '/api/v1/oauth-clients/local' |
5 | 5 | ||
6 | return request(url) | 6 | return request(url) |
7 | .get(path) | 7 | .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 | |||
185 | .field('name', attributes.name) | 185 | .field('name', attributes.name) |
186 | .field('category', attributes.category.toString()) | 186 | .field('category', attributes.category.toString()) |
187 | .field('licence', attributes.licence.toString()) | 187 | .field('licence', attributes.licence.toString()) |
188 | .field('language', attributes.language.toString()) | ||
189 | .field('nsfw', JSON.stringify(attributes.nsfw)) | 188 | .field('nsfw', JSON.stringify(attributes.nsfw)) |
190 | .field('description', attributes.description) | 189 | .field('description', attributes.description) |
191 | 190 | ||
191 | if (attributes.language !== undefined) { | ||
192 | req.field('language', attributes.language.toString()) | ||
193 | } | ||
194 | |||
192 | for (let i = 0; i < attributes.tags.length; i++) { | 195 | for (let i = 0; i < attributes.tags.length; i++) { |
193 | req.field('tags[' + i + ']', attributes.tags[i]) | 196 | req.field('tags[' + i + ']', attributes.tags[i]) |
194 | } | 197 | } |