aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/server')
-rw-r--r--shared/extra-utils/server/activitypub.ts14
-rw-r--r--shared/extra-utils/server/clients.ts19
-rw-r--r--shared/extra-utils/server/config.ts156
-rw-r--r--shared/extra-utils/server/contact-form.ts28
-rw-r--r--shared/extra-utils/server/follows.ts111
-rw-r--r--shared/extra-utils/server/jobs.ts82
-rw-r--r--shared/extra-utils/server/redundancy.ts17
-rw-r--r--shared/extra-utils/server/servers.ts219
-rw-r--r--shared/extra-utils/server/stats.ts22
9 files changed, 668 insertions, 0 deletions
diff --git a/shared/extra-utils/server/activitypub.ts b/shared/extra-utils/server/activitypub.ts
new file mode 100644
index 000000000..eccb198ca
--- /dev/null
+++ b/shared/extra-utils/server/activitypub.ts
@@ -0,0 +1,14 @@
1import * as request from 'supertest'
2
3function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) {
4 return request(url)
5 .get(path)
6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8')
7 .expect(expectedStatus)
8}
9
10// ---------------------------------------------------------------------------
11
12export {
13 makeActivityPubGetRequest
14}
diff --git a/shared/extra-utils/server/clients.ts b/shared/extra-utils/server/clients.ts
new file mode 100644
index 000000000..273aac747
--- /dev/null
+++ b/shared/extra-utils/server/clients.ts
@@ -0,0 +1,19 @@
1import * as request from 'supertest'
2import * as urlUtil from 'url'
3
4function getClient (url: string) {
5 const path = '/api/v1/oauth-clients/local'
6
7 return request(url)
8 .get(path)
9 .set('Host', urlUtil.parse(url).host)
10 .set('Accept', 'application/json')
11 .expect(200)
12 .expect('Content-Type', /json/)
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 getClient
19}
diff --git a/shared/extra-utils/server/config.ts b/shared/extra-utils/server/config.ts
new file mode 100644
index 000000000..deb77e9c0
--- /dev/null
+++ b/shared/extra-utils/server/config.ts
@@ -0,0 +1,156 @@
1import { makeDeleteRequest, makeGetRequest, makePutBodyRequest } from '../requests/requests'
2import { CustomConfig } from '../../models/server/custom-config.model'
3
4function getConfig (url: string) {
5 const path = '/api/v1/config'
6
7 return makeGetRequest({
8 url,
9 path,
10 statusCodeExpected: 200
11 })
12}
13
14function getAbout (url: string) {
15 const path = '/api/v1/config/about'
16
17 return makeGetRequest({
18 url,
19 path,
20 statusCodeExpected: 200
21 })
22}
23
24function getCustomConfig (url: string, token: string, statusCodeExpected = 200) {
25 const path = '/api/v1/config/custom'
26
27 return makeGetRequest({
28 url,
29 token,
30 path,
31 statusCodeExpected
32 })
33}
34
35function updateCustomConfig (url: string, token: string, newCustomConfig: CustomConfig, statusCodeExpected = 200) {
36 const path = '/api/v1/config/custom'
37
38 return makePutBodyRequest({
39 url,
40 token,
41 path,
42 fields: newCustomConfig,
43 statusCodeExpected
44 })
45}
46
47function updateCustomSubConfig (url: string, token: string, newConfig: any) {
48 const updateParams: CustomConfig = {
49 instance: {
50 name: 'PeerTube updated',
51 shortDescription: 'my short description',
52 description: 'my super description',
53 terms: 'my super terms',
54 defaultClientRoute: '/videos/recently-added',
55 isNSFW: true,
56 defaultNSFWPolicy: 'blur',
57 customizations: {
58 javascript: 'alert("coucou")',
59 css: 'body { background-color: red; }'
60 }
61 },
62 services: {
63 twitter: {
64 username: '@MySuperUsername',
65 whitelisted: true
66 }
67 },
68 cache: {
69 previews: {
70 size: 2
71 },
72 captions: {
73 size: 3
74 }
75 },
76 signup: {
77 enabled: false,
78 limit: 5,
79 requiresEmailVerification: false
80 },
81 admin: {
82 email: 'superadmin1@example.com'
83 },
84 contactForm: {
85 enabled: true
86 },
87 user: {
88 videoQuota: 5242881,
89 videoQuotaDaily: 318742
90 },
91 transcoding: {
92 enabled: true,
93 allowAdditionalExtensions: true,
94 threads: 1,
95 resolutions: {
96 '240p': false,
97 '360p': true,
98 '480p': true,
99 '720p': false,
100 '1080p': false
101 },
102 hls: {
103 enabled: false
104 }
105 },
106 import: {
107 videos: {
108 http: {
109 enabled: false
110 },
111 torrent: {
112 enabled: false
113 }
114 }
115 },
116 autoBlacklist: {
117 videos: {
118 ofUsers: {
119 enabled: false
120 }
121 }
122 },
123 followers: {
124 instance: {
125 enabled: true,
126 manualApproval: false
127 }
128 }
129 }
130
131 Object.assign(updateParams, newConfig)
132
133 return updateCustomConfig(url, token, updateParams)
134}
135
136function deleteCustomConfig (url: string, token: string, statusCodeExpected = 200) {
137 const path = '/api/v1/config/custom'
138
139 return makeDeleteRequest({
140 url,
141 token,
142 path,
143 statusCodeExpected
144 })
145}
146
147// ---------------------------------------------------------------------------
148
149export {
150 getConfig,
151 getCustomConfig,
152 updateCustomConfig,
153 getAbout,
154 deleteCustomConfig,
155 updateCustomSubConfig
156}
diff --git a/shared/extra-utils/server/contact-form.ts b/shared/extra-utils/server/contact-form.ts
new file mode 100644
index 000000000..80394cf99
--- /dev/null
+++ b/shared/extra-utils/server/contact-form.ts
@@ -0,0 +1,28 @@
1import * as request from 'supertest'
2import { ContactForm } from '../../models/server'
3
4function sendContactForm (options: {
5 url: string,
6 fromEmail: string,
7 fromName: string,
8 body: string,
9 expectedStatus?: number
10}) {
11 const path = '/api/v1/server/contact'
12
13 const body: ContactForm = {
14 fromEmail: options.fromEmail,
15 fromName: options.fromName,
16 body: options.body
17 }
18 return request(options.url)
19 .post(path)
20 .send(body)
21 .expect(options.expectedStatus || 204)
22}
23
24// ---------------------------------------------------------------------------
25
26export {
27 sendContactForm
28}
diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts
new file mode 100644
index 000000000..1505804de
--- /dev/null
+++ b/shared/extra-utils/server/follows.ts
@@ -0,0 +1,111 @@
1import * as request from 'supertest'
2import { ServerInfo } from './servers'
3import { waitJobs } from './jobs'
4import { makeGetRequest, makePostBodyRequest } from '..'
5
6function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
7 const path = '/api/v1/server/followers'
8
9 return request(url)
10 .get(path)
11 .query({ start })
12 .query({ count })
13 .query({ sort })
14 .query({ search })
15 .set('Accept', 'application/json')
16 .expect(200)
17 .expect('Content-Type', /json/)
18}
19
20function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
21 const path = '/api/v1/server/followers/' + follower + '/accept'
22
23 return makePostBodyRequest({
24 url,
25 token,
26 path,
27 statusCodeExpected
28 })
29}
30
31function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
32 const path = '/api/v1/server/followers/' + follower + '/reject'
33
34 return makePostBodyRequest({
35 url,
36 token,
37 path,
38 statusCodeExpected
39 })
40}
41
42function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
43 const path = '/api/v1/server/following'
44
45 return request(url)
46 .get(path)
47 .query({ start })
48 .query({ count })
49 .query({ sort })
50 .query({ search })
51 .set('Accept', 'application/json')
52 .expect(200)
53 .expect('Content-Type', /json/)
54}
55
56function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
57 const path = '/api/v1/server/following'
58
59 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
60 return request(follower)
61 .post(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
64 .send({ 'hosts': followingHosts })
65 .expect(expectedStatus)
66}
67
68async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
69 const path = '/api/v1/server/following/' + target.host
70
71 return request(url)
72 .delete(path)
73 .set('Accept', 'application/json')
74 .set('Authorization', 'Bearer ' + accessToken)
75 .expect(expectedStatus)
76}
77
78function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
79 const path = '/api/v1/server/followers/peertube@' + follower.host
80
81 return request(url)
82 .delete(path)
83 .set('Accept', 'application/json')
84 .set('Authorization', 'Bearer ' + accessToken)
85 .expect(expectedStatus)
86}
87
88async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
89 await Promise.all([
90 follow(server1.url, [ server2.url ], server1.accessToken),
91 follow(server2.url, [ server1.url ], server2.accessToken)
92 ])
93
94 // Wait request propagation
95 await waitJobs([ server1, server2 ])
96
97 return true
98}
99
100// ---------------------------------------------------------------------------
101
102export {
103 getFollowersListPaginationAndSort,
104 getFollowingListPaginationAndSort,
105 unfollow,
106 removeFollower,
107 follow,
108 doubleFollow,
109 acceptFollower,
110 rejectFollower
111}
diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts
new file mode 100644
index 000000000..692b5e24d
--- /dev/null
+++ b/shared/extra-utils/server/jobs.ts
@@ -0,0 +1,82 @@
1import * as request from 'supertest'
2import { Job, JobState } from '../../models'
3import { wait } from '../miscs/miscs'
4import { ServerInfo } from './servers'
5
6function getJobsList (url: string, accessToken: string, state: JobState) {
7 const path = '/api/v1/jobs/' + state
8
9 return request(url)
10 .get(path)
11 .set('Accept', 'application/json')
12 .set('Authorization', 'Bearer ' + accessToken)
13 .expect(200)
14 .expect('Content-Type', /json/)
15}
16
17function getJobsListPaginationAndSort (url: string, accessToken: string, state: JobState, start: number, count: number, sort: string) {
18 const path = '/api/v1/jobs/' + state
19
20 return request(url)
21 .get(path)
22 .query({ start })
23 .query({ count })
24 .query({ sort })
25 .set('Accept', 'application/json')
26 .set('Authorization', 'Bearer ' + accessToken)
27 .expect(200)
28 .expect('Content-Type', /json/)
29}
30
31async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
32 const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) : 2000
33 let servers: ServerInfo[]
34
35 if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ]
36 else servers = serversArg as ServerInfo[]
37
38 const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
39 let pendingRequests = false
40
41 function tasksBuilder () {
42 const tasks: Promise<any>[] = []
43 pendingRequests = false
44
45 // Check if each server has pending request
46 for (const server of servers) {
47 for (const state of states) {
48 const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt')
49 .then(res => res.body.data)
50 .then((jobs: Job[]) => jobs.filter(j => j.type !== 'videos-views'))
51 .then(jobs => {
52 if (jobs.length !== 0) pendingRequests = true
53 })
54 tasks.push(p)
55 }
56 }
57
58 return tasks
59 }
60
61 do {
62 await Promise.all(tasksBuilder())
63
64 // Retry, in case of new jobs were created
65 if (pendingRequests === false) {
66 await wait(pendingJobWait)
67 await Promise.all(tasksBuilder())
68 }
69
70 if (pendingRequests) {
71 await wait(1000)
72 }
73 } while (pendingRequests)
74}
75
76// ---------------------------------------------------------------------------
77
78export {
79 getJobsList,
80 waitJobs,
81 getJobsListPaginationAndSort
82}
diff --git a/shared/extra-utils/server/redundancy.ts b/shared/extra-utils/server/redundancy.ts
new file mode 100644
index 000000000..c39ff2c8b
--- /dev/null
+++ b/shared/extra-utils/server/redundancy.ts
@@ -0,0 +1,17 @@
1import { makePutBodyRequest } from '../requests/requests'
2
3async function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) {
4 const path = '/api/v1/server/redundancy/' + host
5
6 return makePutBodyRequest({
7 url,
8 path,
9 token: accessToken,
10 fields: { redundancyAllowed },
11 statusCodeExpected: expectedStatus
12 })
13}
14
15export {
16 updateRedundancy
17}
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
new file mode 100644
index 000000000..5288d253a
--- /dev/null
+++ b/shared/extra-utils/server/servers.ts
@@ -0,0 +1,219 @@
1/* tslint:disable:no-unused-expression */
2
3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path'
5import { root, wait } from '../miscs/miscs'
6import { readdir, readFile } from 'fs-extra'
7import { existsSync } from 'fs'
8import { expect } from 'chai'
9import { VideoChannel } from '../../models/videos'
10
11interface ServerInfo {
12 app: ChildProcess,
13 url: string
14 host: string
15 serverNumber: number
16
17 client: {
18 id: string,
19 secret: string
20 }
21
22 user: {
23 username: string,
24 password: string,
25 email?: string
26 }
27
28 accessToken?: string
29 videoChannel?: VideoChannel
30
31 video?: {
32 id: number
33 uuid: string
34 name: string
35 account: {
36 name: string
37 }
38 }
39
40 remoteVideo?: {
41 id: number
42 uuid: string
43 }
44
45 videos?: { id: number, uuid: string }[]
46}
47
48function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
49 let apps = []
50 let i = 0
51
52 return new Promise<ServerInfo[]>(res => {
53 function anotherServerDone (serverNumber, app) {
54 apps[serverNumber - 1] = app
55 i++
56 if (i === totalServers) {
57 return res(apps)
58 }
59 }
60
61 flushTests()
62 .then(() => {
63 for (let j = 1; j <= totalServers; j++) {
64 runServer(j, configOverride).then(app => anotherServerDone(j, app))
65 }
66 })
67 })
68}
69
70function flushTests () {
71 return new Promise<void>((res, rej) => {
72 return exec('npm run clean:server:test', err => {
73 if (err) return rej(err)
74
75 return res()
76 })
77 })
78}
79
80function runServer (serverNumber: number, configOverride?: Object, args = []) {
81 const server: ServerInfo = {
82 app: null,
83 serverNumber: serverNumber,
84 url: `http://localhost:${9000 + serverNumber}`,
85 host: `localhost:${9000 + serverNumber}`,
86 client: {
87 id: null,
88 secret: null
89 },
90 user: {
91 username: null,
92 password: null
93 }
94 }
95
96 // These actions are async so we need to be sure that they have both been done
97 const serverRunString = {
98 'Server listening': false
99 }
100 const key = 'Database peertube_test' + serverNumber + ' is ready'
101 serverRunString[key] = false
102
103 const regexps = {
104 client_id: 'Client id: (.+)',
105 client_secret: 'Client secret: (.+)',
106 user_username: 'Username: (.+)',
107 user_password: 'User password: (.+)'
108 }
109
110 // Share the environment
111 const env = Object.create(process.env)
112 env['NODE_ENV'] = 'test'
113 env['NODE_APP_INSTANCE'] = serverNumber.toString()
114
115 if (configOverride !== undefined) {
116 env['NODE_CONFIG'] = JSON.stringify(configOverride)
117 }
118
119 const options = {
120 silent: true,
121 env: env,
122 detached: true
123 }
124
125 return new Promise<ServerInfo>(res => {
126 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
127 server.app.stdout.on('data', function onStdout (data) {
128 let dontContinue = false
129
130 // Capture things if we want to
131 for (const key of Object.keys(regexps)) {
132 const regexp = regexps[key]
133 const matches = data.toString().match(regexp)
134 if (matches !== null) {
135 if (key === 'client_id') server.client.id = matches[1]
136 else if (key === 'client_secret') server.client.secret = matches[1]
137 else if (key === 'user_username') server.user.username = matches[1]
138 else if (key === 'user_password') server.user.password = matches[1]
139 }
140 }
141
142 // Check if all required sentences are here
143 for (const key of Object.keys(serverRunString)) {
144 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
145 if (serverRunString[key] === false) dontContinue = true
146 }
147
148 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
149 if (dontContinue === true) return
150
151 server.app.stdout.removeListener('data', onStdout)
152
153 process.on('exit', () => {
154 try {
155 process.kill(server.app.pid)
156 } catch { /* empty */ }
157 })
158
159 res(server)
160 })
161
162 })
163}
164
165async function reRunServer (server: ServerInfo, configOverride?: any) {
166 const newServer = await runServer(server.serverNumber, configOverride)
167 server.app = newServer.app
168
169 return server
170}
171
172async function checkTmpIsEmpty (server: ServerInfo) {
173 return checkDirectoryIsEmpty(server, 'tmp')
174}
175
176async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
177 const testDirectory = 'test' + server.serverNumber
178
179 const directoryPath = join(root(), testDirectory, directory)
180
181 const directoryExists = existsSync(directoryPath)
182 expect(directoryExists).to.be.true
183
184 const files = await readdir(directoryPath)
185 expect(files).to.have.lengthOf(0)
186}
187
188function killallServers (servers: ServerInfo[]) {
189 for (const server of servers) {
190 process.kill(-server.app.pid)
191 }
192}
193
194async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
195 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
196
197 while (true) {
198 const buf = await readFile(logfile)
199
200 const matches = buf.toString().match(new RegExp(str, 'g'))
201 if (matches && matches.length === count) return
202
203 await wait(1000)
204 }
205}
206
207// ---------------------------------------------------------------------------
208
209export {
210 checkDirectoryIsEmpty,
211 checkTmpIsEmpty,
212 ServerInfo,
213 flushAndRunMultipleServers,
214 flushTests,
215 runServer,
216 killallServers,
217 reRunServer,
218 waitUntilLog
219}
diff --git a/shared/extra-utils/server/stats.ts b/shared/extra-utils/server/stats.ts
new file mode 100644
index 000000000..6f079ad18
--- /dev/null
+++ b/shared/extra-utils/server/stats.ts
@@ -0,0 +1,22 @@
1import { makeGetRequest } from '../requests/requests'
2
3function getStats (url: string, useCache = false) {
4 const path = '/api/v1/server/stats'
5
6 const query = {
7 t: useCache ? undefined : new Date().getTime()
8 }
9
10 return makeGetRequest({
11 url,
12 path,
13 query,
14 statusCodeExpected: 200
15 })
16}
17
18// ---------------------------------------------------------------------------
19
20export {
21 getStats
22}