aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-04 21:21:47 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-04 21:30:18 +0200
commit0e1dc3e7c69995c691e1dd82e3c2bc68748661ca (patch)
treef2a4b5cffc72e33c902b67083bbaa35b6f22f0ca /server/tests/utils
parentb0f9f39ed70299a208d1b388c72de8b7f3510cb7 (diff)
downloadPeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.gz
PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.zst
PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.zip
Convert tests to typescript
Diffstat (limited to 'server/tests/utils')
-rw-r--r--server/tests/utils/clients.js24
-rw-r--r--server/tests/utils/clients.ts17
-rw-r--r--server/tests/utils/config.js24
-rw-r--r--server/tests/utils/config.ts17
-rw-r--r--server/tests/utils/index.ts12
-rw-r--r--server/tests/utils/login.js57
-rw-r--r--server/tests/utils/login.ts59
-rw-r--r--server/tests/utils/miscs.js23
-rw-r--r--server/tests/utils/miscs.ts52
-rw-r--r--server/tests/utils/pods.js116
-rw-r--r--server/tests/utils/pods.ts89
-rw-r--r--server/tests/utils/request-schedulers.js25
-rw-r--r--server/tests/utils/request-schedulers.ts18
-rw-r--r--server/tests/utils/requests.js73
-rw-r--r--server/tests/utils/requests.ts92
-rw-r--r--server/tests/utils/servers.js117
-rw-r--r--server/tests/utils/servers.ts153
-rw-r--r--server/tests/utils/users.js144
-rw-r--r--server/tests/utils/users.ts115
-rw-r--r--server/tests/utils/video-abuses.js73
-rw-r--r--server/tests/utils/video-abuses.ts58
-rw-r--r--server/tests/utils/video-blacklists.js29
-rw-r--r--server/tests/utils/video-blacklists.ts17
-rw-r--r--server/tests/utils/videos.js313
-rw-r--r--server/tests/utils/videos.ts254
25 files changed, 953 insertions, 1018 deletions
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const clientsUtils = {
6 getClient: getClient
7}
8
9// ---------------------- Export functions --------------------
10
11function getClient (url, end) {
12 const path = '/api/v1/clients/local'
13
14 request(url)
15 .get(path)
16 .set('Accept', 'application/json')
17 .expect(200)
18 .expect('Content-Type', /json/)
19 .end(end)
20}
21
22// ---------------------------------------------------------------------------
23
24module.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 @@
1import * as request from 'supertest'
2
3function getClient (url: string) {
4 const path = '/api/v1/clients/local'
5
6 return request(url)
7 .get(path)
8 .set('Accept', 'application/json')
9 .expect(200)
10 .expect('Content-Type', /json/)
11}
12
13// ---------------------------------------------------------------------------
14
15export {
16 getClient
17}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const configsUtils = {
6 getConfig
7}
8
9// ---------------------- Export functions --------------------
10
11function getConfig (url, end) {
12 const path = '/api/v1/config'
13
14 request(url)
15 .get(path)
16 .set('Accept', 'application/json')
17 .expect(200)
18 .expect('Content-Type', /json/)
19 .end(end)
20}
21
22// ---------------------------------------------------------------------------
23
24module.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 @@
1import * as request from 'supertest'
2
3function getConfig (url: string) {
4 const path = '/api/v1/config'
5
6 return request(url)
7 .get(path)
8 .set('Accept', 'application/json')
9 .expect(200)
10 .expect('Content-Type', /json/)
11}
12
13// ---------------------------------------------------------------------------
14
15export {
16 getConfig
17}
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 @@
1export * from './clients'
2export * from './config'
3export * from './login'
4export * from './miscs'
5export * from './pods'
6export * from './request-schedulers'
7export * from './requests'
8export * from './servers'
9export * from './users'
10export * from './video-abuses'
11export * from './video-blacklists'
12export * 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 @@
1'use strict'
2
3const request = require('supertest')
4
5const loginUtils = {
6 login,
7 loginAndGetAccessToken,
8 getUserAccessToken
9}
10
11// ---------------------- Export functions --------------------
12
13function login (url, client, user, expectedStatus, end) {
14 if (!end) {
15 end = expectedStatus
16 expectedStatus = 200
17 }
18
19 const path = '/api/v1/users/token'
20
21 const body = {
22 client_id: client.id,
23 client_secret: client.secret,
24 username: user.username,
25 password: user.password,
26 response_type: 'code',
27 grant_type: 'password',
28 scope: 'upload'
29 }
30
31 request(url)
32 .post(path)
33 .type('form')
34 .send(body)
35 .expect(expectedStatus)
36 .end(end)
37}
38
39function loginAndGetAccessToken (server, callback) {
40 login(server.url, server.client, server.user, 200, function (err, res) {
41 if (err) return callback(err)
42
43 return callback(null, res.body.access_token)
44 })
45}
46
47function getUserAccessToken (server, user, callback) {
48 login(server.url, server.client, user, 200, function (err, res) {
49 if (err) return callback(err)
50
51 return callback(null, res.body.access_token)
52 })
53}
54
55// ---------------------------------------------------------------------------
56
57module.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 @@
1import * as request from 'supertest'
2
3import { ServerInfo } from './servers'
4
5type Client = { id: string, secret: string }
6type User = { username: string, password: string }
7type Server = { url: string, client: Client, user: User }
8
9function login (url: string, client: Client, user: User, expectedStatus = 200) {
10 const path = '/api/v1/users/token'
11
12 const body = {
13 client_id: client.id,
14 client_secret: client.secret,
15 username: user.username,
16 password: user.password,
17 response_type: 'code',
18 grant_type: 'password',
19 scope: 'upload'
20 }
21
22 return request(url)
23 .post(path)
24 .type('form')
25 .send(body)
26 .expect(expectedStatus)
27}
28
29async function loginAndGetAccessToken (server: Server) {
30 const res = await login(server.url, server.client, server.user, 200)
31
32 return res.body.access_token as string
33}
34
35async function getUserAccessToken (server, user) {
36 const res = await login(server.url, server.client, user, 200)
37
38 return res.body.access_token as string
39}
40
41function setAccessTokensToServers (servers: ServerInfo[]) {
42 const tasks: Promise<any>[] = []
43
44 for (const server of servers) {
45 const p = loginAndGetAccessToken(server).then(t => server.accessToken = t)
46 tasks.push(p)
47 }
48
49 return Promise.all(tasks)
50}
51
52// ---------------------------------------------------------------------------
53
54export {
55 login,
56 loginAndGetAccessToken,
57 getUserAccessToken,
58 setAccessTokensToServers
59}
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 @@
1'use strict'
2
3const miscsUtils = {
4 dateIsValid
5}
6
7// ---------------------- Export functions --------------------
8
9function dateIsValid (dateString, interval) {
10 const dateToCheck = new Date(dateString)
11 const now = new Date()
12
13 // Check if the interval is more than 2 minutes
14 if (!interval) interval = 120000
15
16 if (now - dateToCheck > interval) return false
17
18 return true
19}
20
21// ---------------------------------------------------------------------------
22
23module.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 @@
1import * as WebTorrent from 'webtorrent'
2import { readFile, readdir } from 'fs'
3
4let webtorrent = new WebTorrent()
5
6function readFilePromise (path: string) {
7 return new Promise<Buffer>((res, rej) => {
8 readFile(path, (err, data) => {
9 if (err) return rej(err)
10
11 return res(data)
12 })
13 })
14}
15
16function readdirPromise (path: string) {
17 return new Promise<string[]>((res, rej) => {
18 readdir(path, (err, files) => {
19 if (err) return rej(err)
20
21 return res(files)
22 })
23 })
24}
25
26 // Default interval -> 2 minutes
27function dateIsValid (dateString: string, interval = 120000) {
28 const dateToCheck = new Date(dateString)
29 const now = new Date()
30
31 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
32}
33
34function wait (milliseconds: number) {
35 return new Promise(resolve => setTimeout(resolve, milliseconds))
36}
37
38function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
39 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
40
41 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
42}
43
44// ---------------------------------------------------------------------------
45
46export {
47 readFilePromise,
48 readdirPromise,
49 dateIsValid,
50 wait,
51 webtorrentAdd
52}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const podsUtils = {
6 getFriendsList,
7 makeFriends,
8 quitFriends,
9 quitOneFriend
10}
11
12// ---------------------- Export functions --------------------
13
14function getFriendsList (url, end) {
15 const path = '/api/v1/pods/'
16
17 request(url)
18 .get(path)
19 .set('Accept', 'application/json')
20 .expect(200)
21 .expect('Content-Type', /json/)
22 .end(end)
23}
24
25function makeFriends (url, accessToken, expectedStatus, end) {
26 if (!end) {
27 end = expectedStatus
28 expectedStatus = 204
29 }
30
31 // Which pod makes friends with which pod
32 const friendsMatrix = {
33 'http://localhost:9001': [
34 'localhost:9002'
35 ],
36 'http://localhost:9002': [
37 'localhost:9003'
38 ],
39 'http://localhost:9003': [
40 'localhost:9001'
41 ],
42 'http://localhost:9004': [
43 'localhost:9002'
44 ],
45 'http://localhost:9005': [
46 'localhost:9001',
47 'localhost:9004'
48 ],
49 'http://localhost:9006': [
50 'localhost:9001',
51 'localhost:9002',
52 'localhost:9003'
53 ]
54 }
55 const path = '/api/v1/pods/makefriends'
56
57 // The first pod make friend with the third
58 request(url)
59 .post(path)
60 .set('Accept', 'application/json')
61 .set('Authorization', 'Bearer ' + accessToken)
62 .send({ 'hosts': friendsMatrix[url] })
63 .expect(expectedStatus)
64 .end(function (err, res) {
65 if (err) throw err
66
67 // Wait for the request between pods
68 setTimeout(end, 1000)
69 })
70}
71
72function quitFriends (url, accessToken, expectedStatus, end) {
73 if (!end) {
74 end = expectedStatus
75 expectedStatus = 204
76 }
77
78 const path = '/api/v1/pods/quitfriends'
79
80 // The first pod make friend with the third
81 request(url)
82 .get(path)
83 .set('Accept', 'application/json')
84 .set('Authorization', 'Bearer ' + accessToken)
85 .expect(expectedStatus)
86 .end(function (err, res) {
87 if (err) throw err
88
89 // Wait for the request between pods
90 setTimeout(end, 1000)
91 })
92}
93
94function quitOneFriend (url, accessToken, friendId, expectedStatus, end) {
95 if (!end) {
96 end = expectedStatus
97 expectedStatus = 204
98 }
99
100 const path = '/api/v1/pods/' + friendId
101
102 request(url)
103 .delete(path)
104 .set('Accept', 'application/json')
105 .set('Authorization', 'Bearer ' + accessToken)
106 .expect(expectedStatus)
107 .end(function (err, res) {
108 if (err) throw err
109
110 end()
111 })
112}
113
114// ---------------------------------------------------------------------------
115
116module.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 @@
1import * as request from 'supertest'
2
3import { wait } from './miscs'
4
5function getFriendsList (url: string) {
6 const path = '/api/v1/pods/'
7
8 return request(url)
9 .get(path)
10 .set('Accept', 'application/json')
11 .expect(200)
12 .expect('Content-Type', /json/)
13}
14
15async function makeFriends (url: string, accessToken: string, expectedStatus = 204) {
16 // Which pod makes friends with which pod
17 const friendsMatrix = {
18 'http://localhost:9001': [
19 'localhost:9002'
20 ],
21 'http://localhost:9002': [
22 'localhost:9003'
23 ],
24 'http://localhost:9003': [
25 'localhost:9001'
26 ],
27 'http://localhost:9004': [
28 'localhost:9002'
29 ],
30 'http://localhost:9005': [
31 'localhost:9001',
32 'localhost:9004'
33 ],
34 'http://localhost:9006': [
35 'localhost:9001',
36 'localhost:9002',
37 'localhost:9003'
38 ]
39 }
40 const path = '/api/v1/pods/makefriends'
41
42 // The first pod make friend with the third
43 const res = await request(url)
44 .post(path)
45 .set('Accept', 'application/json')
46 .set('Authorization', 'Bearer ' + accessToken)
47 .send({ 'hosts': friendsMatrix[url] })
48 .expect(expectedStatus)
49
50 // Wait request propagation
51 await wait(1000)
52
53 return res
54}
55
56async function quitFriends (url: string, accessToken: string, expectedStatus = 204) {
57 const path = '/api/v1/pods/quitfriends'
58
59 // The first pod make friend with the third
60 const res = await request(url)
61 .get(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
64 .expect(expectedStatus)
65
66 // Wait request propagation
67 await wait(1000)
68
69 return res
70}
71
72function quitOneFriend (url: string, accessToken: string, friendId: number, expectedStatus = 204) {
73 const path = '/api/v1/pods/' + friendId
74
75 return request(url)
76 .delete(path)
77 .set('Accept', 'application/json')
78 .set('Authorization', 'Bearer ' + accessToken)
79 .expect(expectedStatus)
80}
81
82// ---------------------------------------------------------------------------
83
84export {
85 getFriendsList,
86 makeFriends,
87 quitFriends,
88 quitOneFriend
89}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const requestsStatsUtils = {
6 getRequestsStats
7}
8
9// ---------------------- Export functions --------------------
10
11function getRequestsStats (server, accessToken, callback) {
12 const path = '/api/v1/requests/stats'
13
14 request(server.url)
15 .get(path)
16 .set('Accept', 'application/json')
17 .set('Authorization', 'Bearer ' + accessToken)
18 .expect(200)
19 .expect('Content-Type', /json/)
20 .end(callback)
21}
22
23// ---------------------------------------------------------------------------
24
25module.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 @@
1import * as request from 'supertest'
2
3function getRequestsStats (server: { url: string }, accessToken: string) {
4 const path = '/api/v1/requests/stats'
5
6 return request(server.url)
7 .get(path)
8 .set('Accept', 'application/json')
9 .set('Authorization', 'Bearer ' + accessToken)
10 .expect(200)
11 .expect('Content-Type', /json/)
12}
13
14// ---------------------------------------------------------------------------
15
16export {
17 getRequestsStats
18}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const requestsUtils = {
6 makePostUploadRequest,
7 makePostBodyRequest,
8 makePutBodyRequest
9}
10
11// ---------------------- Export functions --------------------
12
13function makePostUploadRequest (url, path, token, fields, attaches, done, statusCodeExpected) {
14 if (!statusCodeExpected) statusCodeExpected = 400
15
16 const req = request(url)
17 .post(path)
18 .set('Accept', 'application/json')
19
20 if (token) req.set('Authorization', 'Bearer ' + token)
21
22 Object.keys(fields).forEach(function (field) {
23 const value = fields[field]
24
25 if (Array.isArray(value)) {
26 for (let i = 0; i < value.length; i++) {
27 req.field(field + '[' + i + ']', value[i])
28 }
29 } else {
30 req.field(field, value)
31 }
32 })
33
34 Object.keys(attaches).forEach(function (attach) {
35 const value = attaches[attach]
36 req.attach(attach, value)
37 })
38
39 req.expect(statusCodeExpected)
40 .end(done)
41}
42
43function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) {
44 if (!statusCodeExpected) statusCodeExpected = 400
45
46 const req = request(url)
47 .post(path)
48 .set('Accept', 'application/json')
49
50 if (token) req.set('Authorization', 'Bearer ' + token)
51
52 req.send(fields)
53 .expect(statusCodeExpected)
54 .end(done)
55}
56
57function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) {
58 if (!statusCodeExpected) statusCodeExpected = 400
59
60 const req = request(url)
61 .put(path)
62 .set('Accept', 'application/json')
63
64 if (token) req.set('Authorization', 'Bearer ' + token)
65
66 req.send(fields)
67 .expect(statusCodeExpected)
68 .end(done)
69}
70
71// ---------------------------------------------------------------------------
72
73module.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 @@
1import * as request from 'supertest'
2
3function makeGetRequest (url: string, path: string) {
4 return request(url)
5 .get(path)
6 .set('Accept', 'application/json')
7 .expect(200)
8 .expect('Content-Type', /json/)
9}
10
11function makePostUploadRequest (options: {
12 url: string,
13 path: string,
14 token: string,
15 fields: { [ fieldName: string ]: any },
16 attaches: { [ attachName: string ]: any },
17 statusCodeExpected?: number
18}) {
19 if (!options.statusCodeExpected) options.statusCodeExpected = 400
20
21 const req = request(options.url)
22 .post(options.path)
23 .set('Accept', 'application/json')
24
25 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
26
27 Object.keys(options.fields).forEach(field => {
28 const value = options.fields[field]
29
30 if (Array.isArray(value)) {
31 for (let i = 0; i < value.length; i++) {
32 req.field(field + '[' + i + ']', value[i])
33 }
34 } else {
35 req.field(field, value)
36 }
37 })
38
39 Object.keys(options.attaches).forEach(attach => {
40 const value = options.attaches[attach]
41 req.attach(attach, value)
42 })
43
44 return req.expect(options.statusCodeExpected)
45}
46
47function makePostBodyRequest (options: {
48 url: string,
49 path: string,
50 token?: string,
51 fields: { [ fieldName: string ]: any },
52 statusCodeExpected?: number
53}) {
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400
55
56 const req = request(options.url)
57 .post(options.path)
58 .set('Accept', 'application/json')
59
60 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
61
62 return req.send(options.fields)
63 .expect(options.statusCodeExpected)
64}
65
66function makePutBodyRequest (options: {
67 url: string,
68 path: string,
69 token: string,
70 fields: { [ fieldName: string ]: any },
71 statusCodeExpected?: number
72}) {
73 if (!options.statusCodeExpected) options.statusCodeExpected = 400
74
75 const req = request(options.url)
76 .put(options.path)
77 .set('Accept', 'application/json')
78
79 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
80
81 return req.send(options.fields)
82 .expect(options.statusCodeExpected)
83}
84
85// ---------------------------------------------------------------------------
86
87export {
88 makeGetRequest,
89 makePostUploadRequest,
90 makePostBodyRequest,
91 makePutBodyRequest
92}
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 @@
1'use strict'
2
3const childProcess = require('child_process')
4const exec = childProcess.exec
5const fork = childProcess.fork
6const pathUtils = require('path')
7
8const serversUtils = {
9 flushAndRunMultipleServers,
10 flushTests,
11 runServer
12}
13
14// ---------------------- Export functions --------------------
15
16function flushAndRunMultipleServers (totalServers, serversRun) {
17 let apps = []
18 let urls = []
19 let i = 0
20
21 function anotherServerDone (number, app, url) {
22 apps[number - 1] = app
23 urls[number - 1] = url
24 i++
25 if (i === totalServers) {
26 serversRun(apps, urls)
27 }
28 }
29
30 flushTests(function () {
31 for (let j = 1; j <= totalServers; j++) {
32 // For the virtual buffer
33 setTimeout(function () {
34 runServer(j, function (app, url) {
35 anotherServerDone(j, app, url)
36 })
37 }, 1000 * (j - 1))
38 }
39 })
40}
41
42function flushTests (callback) {
43 exec('npm run clean:server:test', callback)
44}
45
46function runServer (number, callback) {
47 const server = {
48 app: null,
49 url: `http://localhost:${9000 + number}`,
50 host: `localhost:${9000 + number}`,
51 client: {
52 id: null,
53 secret: null
54 },
55 user: {
56 username: null,
57 password: null
58 }
59 }
60
61 // These actions are async so we need to be sure that they have both been done
62 const serverRunString = {
63 'Server listening on port': false
64 }
65 const key = 'Database peertube_test' + number + ' is ready'
66 serverRunString[key] = false
67
68 const regexps = {
69 client_id: 'Client id: (.+)',
70 client_secret: 'Client secret: (.+)',
71 user_username: 'Username: (.+)',
72 user_password: 'User password: (.+)'
73 }
74
75 // Share the environment
76 const env = Object.create(process.env)
77 env.NODE_ENV = 'test'
78 env.NODE_APP_INSTANCE = number
79 const options = {
80 silent: true,
81 env: env,
82 detached: true
83 }
84
85 server.app = fork(pathUtils.join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
86 server.app.stdout.on('data', function onStdout (data) {
87 let dontContinue = false
88
89 // Capture things if we want to
90 for (const key of Object.keys(regexps)) {
91 const regexp = regexps[key]
92 const matches = data.toString().match(regexp)
93 if (matches !== null) {
94 if (key === 'client_id') server.client.id = matches[1]
95 else if (key === 'client_secret') server.client.secret = matches[1]
96 else if (key === 'user_username') server.user.username = matches[1]
97 else if (key === 'user_password') server.user.password = matches[1]
98 }
99 }
100
101 // Check if all required sentences are here
102 for (const key of Object.keys(serverRunString)) {
103 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
104 if (serverRunString[key] === false) dontContinue = true
105 }
106
107 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
108 if (dontContinue === true) return
109
110 server.app.stdout.removeListener('data', onStdout)
111 callback(server)
112 })
113}
114
115// ---------------------------------------------------------------------------
116
117module.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 @@
1import { ChildProcess, exec, fork } from 'child_process'
2import { join } from 'path'
3
4interface ServerInfo {
5 app: ChildProcess,
6 url: string
7 host: string
8
9 client: {
10 id: string,
11 secret: string
12 }
13
14 user: {
15 username: string,
16 password: string,
17 email?: string
18 }
19
20 accessToken?: string
21
22 video?: {
23 id: number
24 uuid: string
25 }
26
27 remoteVideo?: {
28 id: number
29 uuid: string
30 }
31}
32
33async function flushAndRunMultipleServers (totalServers) {
34 let apps = []
35 let i = 0
36
37 return new Promise<ServerInfo[]>(res => {
38 function anotherServerDone (serverNumber, app) {
39 apps[serverNumber - 1] = app
40 i++
41 if (i === totalServers) {
42 return res(apps)
43 }
44 }
45
46 flushTests()
47 .then(() => {
48 for (let j = 1; j <= totalServers; j++) {
49 // For the virtual buffer
50 setTimeout(() => {
51 runServer(j).then(app => anotherServerDone(j, app))
52 }, 1000 * (j - 1))
53 }
54 })
55 })
56}
57
58function flushTests () {
59 return new Promise<void>((res, rej) => {
60 return exec('npm run clean:server:test', err => {
61 if (err) return rej(err)
62
63 return res()
64 })
65 })
66}
67
68function runServer (serverNumber: number) {
69 const server: ServerInfo = {
70 app: null,
71 url: `http://localhost:${9000 + serverNumber}`,
72 host: `localhost:${9000 + serverNumber}`,
73 client: {
74 id: null,
75 secret: null
76 },
77 user: {
78 username: null,
79 password: null
80 }
81 }
82
83 // These actions are async so we need to be sure that they have both been done
84 const serverRunString = {
85 'Server listening on port': false
86 }
87 const key = 'Database peertube_test' + serverNumber + ' is ready'
88 serverRunString[key] = false
89
90 const regexps = {
91 client_id: 'Client id: (.+)',
92 client_secret: 'Client secret: (.+)',
93 user_username: 'Username: (.+)',
94 user_password: 'User password: (.+)'
95 }
96
97 // Share the environment
98 const env = Object.create(process.env)
99 env['NODE_ENV'] = 'test'
100 env['NODE_APP_INSTANCE'] = serverNumber.toString()
101 const options = {
102 silent: true,
103 env: env,
104 detached: true
105 }
106
107 return new Promise<ServerInfo>(res => {
108 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
109 server.app.stdout.on('data', function onStdout (data) {
110 let dontContinue = false
111
112 // Capture things if we want to
113 for (const key of Object.keys(regexps)) {
114 const regexp = regexps[key]
115 const matches = data.toString().match(regexp)
116 if (matches !== null) {
117 if (key === 'client_id') server.client.id = matches[1]
118 else if (key === 'client_secret') server.client.secret = matches[1]
119 else if (key === 'user_username') server.user.username = matches[1]
120 else if (key === 'user_password') server.user.password = matches[1]
121 }
122 }
123
124 // Check if all required sentences are here
125 for (const key of Object.keys(serverRunString)) {
126 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
127 if (serverRunString[key] === false) dontContinue = true
128 }
129
130 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
131 if (dontContinue === true) return
132
133 server.app.stdout.removeListener('data', onStdout)
134 res(server)
135 })
136 })
137}
138
139function killallServers (servers: ServerInfo[]) {
140 for (const server of servers) {
141 process.kill(-server.app.pid)
142 }
143}
144
145// ---------------------------------------------------------------------------
146
147export {
148 ServerInfo,
149 flushAndRunMultipleServers,
150 flushTests,
151 runServer,
152 killallServers
153}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const usersUtils = {
6 createUser,
7 registerUser,
8 getUserInformation,
9 getUserVideoRating,
10 getUsersList,
11 getUsersListPaginationAndSort,
12 removeUser,
13 updateUser
14}
15
16// ---------------------- Export functions --------------------
17
18function createUser (url, accessToken, username, password, specialStatus, end) {
19 if (!end) {
20 end = specialStatus
21 specialStatus = 204
22 }
23
24 const path = '/api/v1/users'
25 const body = {
26 username,
27 password,
28 email: username + '@example.com'
29 }
30
31 request(url)
32 .post(path)
33 .set('Accept', 'application/json')
34 .set('Authorization', 'Bearer ' + accessToken)
35 .send(body)
36 .expect(specialStatus)
37 .end(end)
38}
39
40function registerUser (url, username, password, specialStatus, end) {
41 if (!end) {
42 end = specialStatus
43 specialStatus = 204
44 }
45
46 const path = '/api/v1/users/register'
47 const body = {
48 username,
49 password,
50 email: username + '@example.com'
51 }
52
53 request(url)
54 .post(path)
55 .set('Accept', 'application/json')
56 .send(body)
57 .expect(specialStatus)
58 .end(end)
59}
60
61function getUserInformation (url, accessToken, end) {
62 const path = '/api/v1/users/me'
63
64 request(url)
65 .get(path)
66 .set('Accept', 'application/json')
67 .set('Authorization', 'Bearer ' + accessToken)
68 .expect(200)
69 .expect('Content-Type', /json/)
70 .end(end)
71}
72
73function getUserVideoRating (url, accessToken, videoId, end) {
74 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
75
76 request(url)
77 .get(path)
78 .set('Accept', 'application/json')
79 .set('Authorization', 'Bearer ' + accessToken)
80 .expect(200)
81 .expect('Content-Type', /json/)
82 .end(end)
83}
84
85function getUsersList (url, end) {
86 const path = '/api/v1/users'
87
88 request(url)
89 .get(path)
90 .set('Accept', 'application/json')
91 .expect(200)
92 .expect('Content-Type', /json/)
93 .end(end)
94}
95
96function getUsersListPaginationAndSort (url, start, count, sort, end) {
97 const path = '/api/v1/users'
98
99 request(url)
100 .get(path)
101 .query({ start: start })
102 .query({ count: count })
103 .query({ sort: sort })
104 .set('Accept', 'application/json')
105 .expect(200)
106 .expect('Content-Type', /json/)
107 .end(end)
108}
109
110function removeUser (url, userId, accessToken, expectedStatus, end) {
111 if (!end) {
112 end = expectedStatus
113 expectedStatus = 204
114 }
115
116 const path = '/api/v1/users'
117
118 request(url)
119 .delete(path + '/' + userId)
120 .set('Accept', 'application/json')
121 .set('Authorization', 'Bearer ' + accessToken)
122 .expect(expectedStatus)
123 .end(end)
124}
125
126function updateUser (url, userId, accessToken, newPassword, displayNSFW, end) {
127 const path = '/api/v1/users/' + userId
128
129 const toSend = {}
130 if (newPassword !== undefined && newPassword !== null) toSend.password = newPassword
131 if (displayNSFW !== undefined && displayNSFW !== null) toSend.displayNSFW = displayNSFW
132
133 request(url)
134 .put(path)
135 .set('Accept', 'application/json')
136 .set('Authorization', 'Bearer ' + accessToken)
137 .send(toSend)
138 .expect(204)
139 .end(end)
140}
141
142// ---------------------------------------------------------------------------
143
144module.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 @@
1import * as request from 'supertest'
2
3function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) {
4 const path = '/api/v1/users'
5 const body = {
6 username,
7 password,
8 email: username + '@example.com'
9 }
10
11 return request(url)
12 .post(path)
13 .set('Accept', 'application/json')
14 .set('Authorization', 'Bearer ' + accessToken)
15 .send(body)
16 .expect(specialStatus)
17}
18
19function registerUser (url: string, username: string, password: string, specialStatus = 204) {
20 const path = '/api/v1/users/register'
21 const body = {
22 username,
23 password,
24 email: username + '@example.com'
25 }
26
27 return request(url)
28 .post(path)
29 .set('Accept', 'application/json')
30 .send(body)
31 .expect(specialStatus)
32}
33
34function getUserInformation (url: string, accessToken: string) {
35 const path = '/api/v1/users/me'
36
37 return request(url)
38 .get(path)
39 .set('Accept', 'application/json')
40 .set('Authorization', 'Bearer ' + accessToken)
41 .expect(200)
42 .expect('Content-Type', /json/)
43}
44
45function getUserVideoRating (url: string, accessToken: string, videoId: number) {
46 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
47
48 return request(url)
49 .get(path)
50 .set('Accept', 'application/json')
51 .set('Authorization', 'Bearer ' + accessToken)
52 .expect(200)
53 .expect('Content-Type', /json/)
54}
55
56function getUsersList (url: string) {
57 const path = '/api/v1/users'
58
59 return request(url)
60 .get(path)
61 .set('Accept', 'application/json')
62 .expect(200)
63 .expect('Content-Type', /json/)
64}
65
66function getUsersListPaginationAndSort (url: string, start: number, count: number, sort: string) {
67 const path = '/api/v1/users'
68
69 return request(url)
70 .get(path)
71 .query({ start })
72 .query({ count })
73 .query({ sort })
74 .set('Accept', 'application/json')
75 .expect(200)
76 .expect('Content-Type', /json/)
77}
78
79function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) {
80 const path = '/api/v1/users'
81
82 return request(url)
83 .delete(path + '/' + userId)
84 .set('Accept', 'application/json')
85 .set('Authorization', 'Bearer ' + accessToken)
86 .expect(expectedStatus)
87}
88
89function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) {
90 const path = '/api/v1/users/' + userId
91
92 const toSend = {}
93 if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword
94 if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW
95
96 return request(url)
97 .put(path)
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + accessToken)
100 .send(toSend)
101 .expect(204)
102}
103
104// ---------------------------------------------------------------------------
105
106export {
107 createUser,
108 registerUser,
109 getUserInformation,
110 getUserVideoRating,
111 getUsersList,
112 getUsersListPaginationAndSort,
113 removeUser,
114 updateUser
115}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const videosAbuseUtils = {
6 getVideoAbusesList,
7 getVideoAbusesListPagination,
8 getVideoAbusesListSort,
9 reportVideoAbuse
10}
11
12// ---------------------- Export functions --------------------
13
14function reportVideoAbuse (url, token, videoId, reason, specialStatus, end) {
15 if (!end) {
16 end = specialStatus
17 specialStatus = 204
18 }
19
20 const path = '/api/v1/videos/' + videoId + '/abuse'
21
22 request(url)
23 .post(path)
24 .set('Accept', 'application/json')
25 .set('Authorization', 'Bearer ' + token)
26 .send({ reason })
27 .expect(specialStatus)
28 .end(end)
29}
30
31function getVideoAbusesList (url, token, end) {
32 const path = '/api/v1/videos/abuse'
33
34 request(url)
35 .get(path)
36 .query({ sort: 'createdAt' })
37 .set('Accept', 'application/json')
38 .set('Authorization', 'Bearer ' + token)
39 .expect(200)
40 .expect('Content-Type', /json/)
41 .end(end)
42}
43
44function getVideoAbusesListPagination (url, token, start, count, end) {
45 const path = '/api/v1/videos/abuse'
46
47 request(url)
48 .get(path)
49 .query({ start: start })
50 .query({ count: count })
51 .set('Accept', 'application/json')
52 .set('Authorization', 'Bearer ' + token)
53 .expect(200)
54 .expect('Content-Type', /json/)
55 .end(end)
56}
57
58function getVideoAbusesListSort (url, token, sort, end) {
59 const path = '/api/v1/videos/abuse'
60
61 request(url)
62 .get(path)
63 .query({ sort: sort })
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + token)
66 .expect(200)
67 .expect('Content-Type', /json/)
68 .end(end)
69}
70
71// ---------------------------------------------------------------------------
72
73module.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 @@
1import * as request from 'supertest'
2
3function reportVideoAbuse (url: string, token: string, videoId: number, reason: string, specialStatus = 204) {
4 const path = '/api/v1/videos/' + videoId + '/abuse'
5
6 return request(url)
7 .post(path)
8 .set('Accept', 'application/json')
9 .set('Authorization', 'Bearer ' + token)
10 .send({ reason })
11 .expect(specialStatus)
12}
13
14function getVideoAbusesList (url: string, token: string) {
15 const path = '/api/v1/videos/abuse'
16
17 return request(url)
18 .get(path)
19 .query({ sort: 'createdAt' })
20 .set('Accept', 'application/json')
21 .set('Authorization', 'Bearer ' + token)
22 .expect(200)
23 .expect('Content-Type', /json/)
24}
25
26function getVideoAbusesListPagination (url: string, token: string, start: number, count: number) {
27 const path = '/api/v1/videos/abuse'
28
29 return request(url)
30 .get(path)
31 .query({ start: start })
32 .query({ count: count })
33 .set('Accept', 'application/json')
34 .set('Authorization', 'Bearer ' + token)
35 .expect(200)
36 .expect('Content-Type', /json/)
37}
38
39function getVideoAbusesListSort (url: string, token: string, sort: string) {
40 const path = '/api/v1/videos/abuse'
41
42 return request(url)
43 .get(path)
44 .query({ sort: sort })
45 .set('Accept', 'application/json')
46 .set('Authorization', 'Bearer ' + token)
47 .expect(200)
48 .expect('Content-Type', /json/)
49}
50
51// ---------------------------------------------------------------------------
52
53export {
54 reportVideoAbuse,
55 getVideoAbusesList,
56 getVideoAbusesListPagination,
57 getVideoAbusesListSort
58}
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 @@
1'use strict'
2
3const request = require('supertest')
4
5const videosBlacklistsUtils = {
6 addVideoToBlacklist
7}
8
9// ---------------------- Export functions --------------------
10
11function addVideoToBlacklist (url, token, videoId, specialStatus, end) {
12 if (!end) {
13 end = specialStatus
14 specialStatus = 204
15 }
16
17 const path = '/api/v1/videos/' + videoId + '/blacklist'
18
19 request(url)
20 .post(path)
21 .set('Accept', 'application/json')
22 .set('Authorization', 'Bearer ' + token)
23 .expect(specialStatus)
24 .end(end)
25}
26
27// ---------------------------------------------------------------------------
28
29module.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 @@
1import * as request from 'supertest'
2
3function addVideoToBlacklist (url: string, token: string, videoId: number, specialStatus = 204) {
4 const path = '/api/v1/videos/' + videoId + '/blacklist'
5
6 return request(url)
7 .post(path)
8 .set('Accept', 'application/json')
9 .set('Authorization', 'Bearer ' + token)
10 .expect(specialStatus)
11}
12
13// ---------------------------------------------------------------------------
14
15export {
16 addVideoToBlacklist
17}
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 @@
1'use strict'
2
3const fs = require('fs')
4const pathUtils = require('path')
5const request = require('supertest')
6
7const videosUtils = {
8 getVideoCategories,
9 getVideoLicences,
10 getVideoLanguages,
11 getAllVideosListBy,
12 getVideo,
13 getVideosList,
14 getVideosListPagination,
15 getVideosListSort,
16 removeVideo,
17 searchVideo,
18 searchVideoWithPagination,
19 searchVideoWithSort,
20 testVideoImage,
21 uploadVideo,
22 updateVideo,
23 rateVideo
24}
25
26// ---------------------- Export functions --------------------
27
28function getVideoCategories (url, end) {
29 const path = '/api/v1/videos/categories'
30
31 request(url)
32 .get(path)
33 .set('Accept', 'application/json')
34 .expect(200)
35 .expect('Content-Type', /json/)
36 .end(end)
37}
38
39function getVideoLicences (url, end) {
40 const path = '/api/v1/videos/licences'
41
42 request(url)
43 .get(path)
44 .set('Accept', 'application/json')
45 .expect(200)
46 .expect('Content-Type', /json/)
47 .end(end)
48}
49
50function getVideoLanguages (url, end) {
51 const path = '/api/v1/videos/languages'
52
53 request(url)
54 .get(path)
55 .set('Accept', 'application/json')
56 .expect(200)
57 .expect('Content-Type', /json/)
58 .end(end)
59}
60
61function getAllVideosListBy (url, end) {
62 const path = '/api/v1/videos'
63
64 request(url)
65 .get(path)
66 .query({ sort: 'createdAt' })
67 .query({ start: 0 })
68 .query({ count: 10000 })
69 .set('Accept', 'application/json')
70 .expect(200)
71 .expect('Content-Type', /json/)
72 .end(end)
73}
74
75function getVideo (url, id, end) {
76 const path = '/api/v1/videos/' + id
77
78 request(url)
79 .get(path)
80 .set('Accept', 'application/json')
81 .expect(200)
82 .expect('Content-Type', /json/)
83 .end(end)
84}
85
86function getVideosList (url, end) {
87 const path = '/api/v1/videos'
88
89 request(url)
90 .get(path)
91 .query({ sort: 'name' })
92 .set('Accept', 'application/json')
93 .expect(200)
94 .expect('Content-Type', /json/)
95 .end(end)
96}
97
98function getVideosListPagination (url, start, count, sort, end) {
99 if (!end) {
100 end = sort
101 sort = null
102 }
103
104 const path = '/api/v1/videos'
105
106 const req = request(url)
107 .get(path)
108 .query({ start: start })
109 .query({ count: count })
110
111 if (sort) req.query({ sort })
112
113 req.set('Accept', 'application/json')
114 .expect(200)
115 .expect('Content-Type', /json/)
116 .end(end)
117}
118
119function getVideosListSort (url, sort, end) {
120 const path = '/api/v1/videos'
121
122 request(url)
123 .get(path)
124 .query({ sort: sort })
125 .set('Accept', 'application/json')
126 .expect(200)
127 .expect('Content-Type', /json/)
128 .end(end)
129}
130
131function removeVideo (url, token, id, expectedStatus, end) {
132 if (!end) {
133 end = expectedStatus
134 expectedStatus = 204
135 }
136
137 const path = '/api/v1/videos'
138
139 request(url)
140 .delete(path + '/' + id)
141 .set('Accept', 'application/json')
142 .set('Authorization', 'Bearer ' + token)
143 .expect(expectedStatus)
144 .end(end)
145}
146
147function searchVideo (url, search, field, end) {
148 if (!end) {
149 end = field
150 field = null
151 }
152
153 const path = '/api/v1/videos'
154 const req = request(url)
155 .get(path + '/search/' + search)
156 .set('Accept', 'application/json')
157
158 if (field) req.query({ field: field })
159 req.expect(200)
160 .expect('Content-Type', /json/)
161 .end(end)
162}
163
164function searchVideoWithPagination (url, search, field, start, count, sort, end) {
165 if (!end) {
166 end = sort
167 sort = null
168 }
169
170 const path = '/api/v1/videos'
171
172 const req = request(url)
173 .get(path + '/search/' + search)
174 .query({ start: start })
175 .query({ count: count })
176 .query({ field: field })
177
178 if (sort) req.query({ sort })
179
180 req.set('Accept', 'application/json')
181 .expect(200)
182 .expect('Content-Type', /json/)
183 .end(end)
184}
185
186function searchVideoWithSort (url, search, sort, end) {
187 const path = '/api/v1/videos'
188
189 request(url)
190 .get(path + '/search/' + search)
191 .query({ sort: sort })
192 .set('Accept', 'application/json')
193 .expect(200)
194 .expect('Content-Type', /json/)
195 .end(end)
196}
197
198function testVideoImage (url, imageName, imagePath, callback) {
199 // Don't test images if the node env is not set
200 // Because we need a special ffmpeg version for this test
201 if (process.env.NODE_TEST_IMAGE) {
202 request(url)
203 .get(imagePath)
204 .expect(200)
205 .end(function (err, res) {
206 if (err) return callback(err)
207
208 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'), function (err, data) {
209 if (err) return callback(err)
210
211 callback(null, data.equals(res.body))
212 })
213 })
214 } else {
215 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
216 callback(null, true)
217 }
218}
219
220function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
221 if (!end) {
222 end = specialStatus
223 specialStatus = 204
224 }
225
226 const path = '/api/v1/videos'
227
228 // Default attributes
229 let attributes = {
230 name: 'my super video',
231 category: 5,
232 licence: 4,
233 language: 3,
234 nsfw: true,
235 description: 'my super description',
236 tags: [ 'tag' ],
237 fixture: 'video_short.webm'
238 }
239 attributes = Object.assign(attributes, videoAttributesArg)
240
241 const req = request(url)
242 .post(path)
243 .set('Accept', 'application/json')
244 .set('Authorization', 'Bearer ' + accessToken)
245 .field('name', attributes.name)
246 .field('category', attributes.category)
247 .field('licence', attributes.licence)
248 .field('language', attributes.language)
249 .field('nsfw', attributes.nsfw)
250 .field('description', attributes.description)
251
252 for (let i = 0; i < attributes.tags.length; i++) {
253 req.field('tags[' + i + ']', attributes.tags[i])
254 }
255
256 let filepath = ''
257 if (pathUtils.isAbsolute(attributes.fixture)) {
258 filepath = attributes.fixture
259 } else {
260 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
261 }
262
263 req.attach('videofile', filepath)
264 .expect(specialStatus)
265 .end(end)
266}
267
268function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
269 if (!end) {
270 end = specialStatus
271 specialStatus = 204
272 }
273
274 const path = '/api/v1/videos/' + id
275 const body = {}
276
277 if (attributes.name) body.name = attributes.name
278 if (attributes.category) body.category = attributes.category
279 if (attributes.licence) body.licence = attributes.licence
280 if (attributes.language) body.language = attributes.language
281 if (attributes.nsfw) body.nsfw = attributes.nsfw
282 if (attributes.description) body.description = attributes.description
283 if (attributes.tags) body.tags = attributes.tags
284
285 request(url)
286 .put(path)
287 .send(body)
288 .set('Accept', 'application/json')
289 .set('Authorization', 'Bearer ' + accessToken)
290 .expect(specialStatus)
291 .end(end)
292}
293
294function rateVideo (url, accessToken, id, rating, specialStatus, end) {
295 if (!end) {
296 end = specialStatus
297 specialStatus = 204
298 }
299
300 const path = '/api/v1/videos/' + id + '/rate'
301
302 request(url)
303 .put(path)
304 .set('Accept', 'application/json')
305 .set('Authorization', 'Bearer ' + accessToken)
306 .send({ rating })
307 .expect(specialStatus)
308 .end(end)
309}
310
311// ---------------------------------------------------------------------------
312
313module.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 @@
1import * as request from 'supertest'
2import { join, isAbsolute } from 'path'
3
4import { makeGetRequest } from './requests'
5import { readFilePromise } from './miscs'
6
7type VideoAttributes = {
8 name?: string
9 category?: number
10 licence?: number
11 language?: number
12 nsfw?: boolean
13 description?: string
14 tags?: string[]
15 fixture?: string
16}
17
18function getVideoCategories (url: string) {
19 const path = '/api/v1/videos/categories'
20
21 return makeGetRequest(url, path)
22}
23
24function getVideoLicences (url: string) {
25 const path = '/api/v1/videos/licences'
26
27 return makeGetRequest(url, path)
28}
29
30function getVideoLanguages (url: string) {
31 const path = '/api/v1/videos/languages'
32
33 return makeGetRequest(url, path)
34}
35
36function getAllVideosListBy (url: string) {
37 const path = '/api/v1/videos'
38
39 return request(url)
40 .get(path)
41 .query({ sort: 'createdAt' })
42 .query({ start: 0 })
43 .query({ count: 10000 })
44 .set('Accept', 'application/json')
45 .expect(200)
46 .expect('Content-Type', /json/)
47}
48
49function getVideo (url: string, id: number | string) {
50 const path = '/api/v1/videos/' + id
51
52 return request(url)
53 .get(path)
54 .set('Accept', 'application/json')
55 .expect(200)
56 .expect('Content-Type', /json/)
57}
58
59function getVideosList (url: string) {
60 const path = '/api/v1/videos'
61
62 return request(url)
63 .get(path)
64 .query({ sort: 'name' })
65 .set('Accept', 'application/json')
66 .expect(200)
67 .expect('Content-Type', /json/)
68}
69
70function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
71 const path = '/api/v1/videos'
72
73 const req = request(url)
74 .get(path)
75 .query({ start: start })
76 .query({ count: count })
77
78 if (sort) req.query({ sort })
79
80 return req.set('Accept', 'application/json')
81 .expect(200)
82 .expect('Content-Type', /json/)
83}
84
85function getVideosListSort (url: string, sort: string) {
86 const path = '/api/v1/videos'
87
88 return request(url)
89 .get(path)
90 .query({ sort: sort })
91 .set('Accept', 'application/json')
92 .expect(200)
93 .expect('Content-Type', /json/)
94}
95
96function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
97 const path = '/api/v1/videos'
98
99 return request(url)
100 .delete(path + '/' + id)
101 .set('Accept', 'application/json')
102 .set('Authorization', 'Bearer ' + token)
103 .expect(expectedStatus)
104}
105
106function searchVideo (url: string, search: string, field?: string) {
107 const path = '/api/v1/videos'
108 const req = request(url)
109 .get(path + '/search/' + search)
110 .set('Accept', 'application/json')
111
112 if (field) req.query({ field })
113
114 return req.expect(200)
115 .expect('Content-Type', /json/)
116}
117
118function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
119 const path = '/api/v1/videos'
120
121 const req = request(url)
122 .get(path + '/search/' + search)
123 .query({ start })
124 .query({ count })
125 .query({ field })
126
127 if (sort) req.query({ sort })
128
129 return req.set('Accept', 'application/json')
130 .expect(200)
131 .expect('Content-Type', /json/)
132}
133
134function searchVideoWithSort (url: string, search: string, sort: string) {
135 const path = '/api/v1/videos'
136
137 return request(url)
138 .get(path + '/search/' + search)
139 .query({ sort })
140 .set('Accept', 'application/json')
141 .expect(200)
142 .expect('Content-Type', /json/)
143}
144
145async function testVideoImage (url: string, imageName: string, imagePath: string) {
146 // Don't test images if the node env is not set
147 // Because we need a special ffmpeg version for this test
148 if (process.env['NODE_TEST_IMAGE']) {
149 const res = await request(url)
150 .get(imagePath)
151 .expect(200)
152
153 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
154
155 return data.equals(res.body)
156 } else {
157 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
158 return true
159 }
160}
161
162function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
163 const path = '/api/v1/videos'
164
165 // Default attributes
166 let attributes = {
167 name: 'my super video',
168 category: 5,
169 licence: 4,
170 language: 3,
171 nsfw: true,
172 description: 'my super description',
173 tags: [ 'tag' ],
174 fixture: 'video_short.webm'
175 }
176 attributes = Object.assign(attributes, videoAttributesArg)
177
178 const req = request(url)
179 .post(path)
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + accessToken)
182 .field('name', attributes.name)
183 .field('category', attributes.category.toString())
184 .field('licence', attributes.licence.toString())
185 .field('language', attributes.language.toString())
186 .field('nsfw', JSON.stringify(attributes.nsfw))
187 .field('description', attributes.description)
188
189 for (let i = 0; i < attributes.tags.length; i++) {
190 req.field('tags[' + i + ']', attributes.tags[i])
191 }
192
193 let filepath = ''
194 if (isAbsolute(attributes.fixture)) {
195 filepath = attributes.fixture
196 } else {
197 filepath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
198 }
199
200 return req.attach('videofile', filepath)
201 .expect(specialStatus)
202}
203
204function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
205 const path = '/api/v1/videos/' + id
206 const body = {}
207
208 if (attributes.name) body['name'] = attributes.name
209 if (attributes.category) body['category'] = attributes.category
210 if (attributes.licence) body['licence'] = attributes.licence
211 if (attributes.language) body['language'] = attributes.language
212 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
213 if (attributes.description) body['description'] = attributes.description
214 if (attributes.tags) body['tags'] = attributes.tags
215
216 return request(url)
217 .put(path)
218 .send(body)
219 .set('Accept', 'application/json')
220 .set('Authorization', 'Bearer ' + accessToken)
221 .expect(specialStatus)
222}
223
224function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
225 const path = '/api/v1/videos/' + id + '/rate'
226
227 return request(url)
228 .put(path)
229 .set('Accept', 'application/json')
230 .set('Authorization', 'Bearer ' + accessToken)
231 .send({ rating })
232 .expect(specialStatus)
233}
234
235// ---------------------------------------------------------------------------
236
237export {
238 getVideoCategories,
239 getVideoLicences,
240 getVideoLanguages,
241 getAllVideosListBy,
242 getVideo,
243 getVideosList,
244 getVideosListPagination,
245 getVideosListSort,
246 removeVideo,
247 searchVideo,
248 searchVideoWithPagination,
249 searchVideoWithSort,
250 testVideoImage,
251 uploadVideo,
252 updateVideo,
253 rateVideo
254}