]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/utils.js
c1a01ef372252ca22e069bc672079f48f47268d6
[github/Chocobozzz/PeerTube.git] / server / tests / api / utils.js
1 'use strict'
2
3 const child_process = require('child_process')
4 const exec = child_process.exec
5 const fork = child_process.fork
6 const pathUtils = require('path')
7 const request = require('supertest')
8
9 const testUtils = {
10 flushTests: flushTests,
11 getFriendsList: getFriendsList,
12 getVideo: getVideo,
13 getVideosList: getVideosList,
14 login: login,
15 loginAndGetAccessToken: loginAndGetAccessToken,
16 makeFriends: makeFriends,
17 quitFriends: quitFriends,
18 removeVideo: removeVideo,
19 flushAndRunMultipleServers: flushAndRunMultipleServers,
20 runServer: runServer,
21 searchVideo: searchVideo,
22 uploadVideo: uploadVideo
23 }
24
25 // ---------------------- Export functions --------------------
26
27 function flushTests (callback) {
28 exec('npm run clean:server:test', callback)
29 }
30
31 function getFriendsList (url, end) {
32 const path = '/api/v1/pods/'
33
34 request(url)
35 .get(path)
36 .set('Accept', 'application/json')
37 .expect(200)
38 .expect('Content-Type', /json/)
39 .end(end)
40 }
41
42 function getVideo (url, id, end) {
43 const path = '/api/v1/videos/' + id
44
45 request(url)
46 .get(path)
47 .set('Accept', 'application/json')
48 .expect(200)
49 .expect('Content-Type', /json/)
50 .end(end)
51 }
52
53 function getVideosList (url, end) {
54 const path = '/api/v1/videos'
55
56 request(url)
57 .get(path)
58 .set('Accept', 'application/json')
59 .expect(200)
60 .expect('Content-Type', /json/)
61 .end(end)
62 }
63
64 function login (url, client, user, expected_status, end) {
65 if (!end) {
66 end = expected_status
67 expected_status = 200
68 }
69
70 const path = '/api/v1/users/token'
71
72 const body = {
73 client_id: client.id,
74 client_secret: client.secret,
75 username: user.username,
76 password: user.password,
77 response_type: 'code',
78 grant_type: 'password',
79 scope: 'upload'
80 }
81
82 request(url)
83 .post(path)
84 .type('form')
85 .send(body)
86 .expect(expected_status)
87 .end(end)
88 }
89
90 function loginAndGetAccessToken (server, callback) {
91 login(server.url, server.client, server.user, 200, function (err, res) {
92 if (err) return callback(err)
93
94 return callback(null, res.body.access_token)
95 })
96 }
97
98 function makeFriends (url, expected_status, callback) {
99 if (!callback) {
100 callback = expected_status
101 expected_status = 204
102 }
103
104 const path = '/api/v1/pods/makefriends'
105
106 // The first pod make friend with the third
107 request(url)
108 .get(path)
109 .set('Accept', 'application/json')
110 .expect(expected_status)
111 .end(function (err, res) {
112 if (err) throw err
113
114 // Wait for the request between pods
115 setTimeout(callback, 1000)
116 })
117 }
118
119 function quitFriends (url, callback) {
120 const path = '/api/v1/pods/quitfriends'
121
122 // The first pod make friend with the third
123 request(url)
124 .get(path)
125 .set('Accept', 'application/json')
126 .expect(204)
127 .end(function (err, res) {
128 if (err) throw err
129
130 // Wait for the request between pods
131 setTimeout(callback, 1000)
132 })
133 }
134
135 function removeVideo (url, token, id, expected_status, end) {
136 if (!end) {
137 end = expected_status
138 expected_status = 204
139 }
140
141 const path = '/api/v1/videos'
142
143 request(url)
144 .delete(path + '/' + id)
145 .set('Accept', 'application/json')
146 .set('Authorization', 'Bearer ' + token)
147 .expect(expected_status)
148 .end(end)
149 }
150
151 function flushAndRunMultipleServers (total_servers, serversRun) {
152 let apps = []
153 let urls = []
154 let i = 0
155
156 function anotherServerDone (number, app, url) {
157 apps[number - 1] = app
158 urls[number - 1] = url
159 i++
160 if (i === total_servers) {
161 serversRun(apps, urls)
162 }
163 }
164
165 flushTests(function () {
166 for (let j = 1; j <= total_servers; j++) {
167 // For the virtual buffer
168 setTimeout(function () {
169 runServer(j, function (app, url) {
170 anotherServerDone(j, app, url)
171 })
172 }, 1000 * j)
173 }
174 })
175 }
176
177 function runServer (number, callback) {
178 const server = {
179 app: null,
180 url: `http://localhost:${9000 + number}`,
181 client: {
182 id: null,
183 secret: null
184 },
185 user: {
186 username: null,
187 password: null
188 }
189 }
190
191 // These actions are async so we need to be sure that they have both been done
192 const server_run_string = {
193 'Connected to mongodb': false,
194 'Server listening on port': false
195 }
196
197 const regexps = {
198 client_id: 'Client id: ([a-f0-9]+)',
199 client_secret: 'Client secret: (.+)',
200 user_username: 'Username: (.+)',
201 user_password: 'User password: (.+)'
202 }
203
204 // Share the environment
205 const env = Object.create(process.env)
206 env.NODE_ENV = 'test'
207 env.NODE_APP_INSTANCE = number
208 const options = {
209 silent: true,
210 env: env,
211 detached: true
212 }
213
214 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
215 server.app.stdout.on('data', function onStdout (data) {
216 let dont_continue = false
217
218 // Capture things if we want to
219 for (const key of Object.keys(regexps)) {
220 const regexp = regexps[key]
221 const matches = data.toString().match(regexp)
222 if (matches !== null) {
223 if (key === 'client_id') server.client.id = matches[1]
224 else if (key === 'client_secret') server.client.secret = matches[1]
225 else if (key === 'user_username') server.user.username = matches[1]
226 else if (key === 'user_password') server.user.password = matches[1]
227 }
228 }
229
230 // Check if all required sentences are here
231 for (const key of Object.keys(server_run_string)) {
232 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
233 if (server_run_string[key] === false) dont_continue = true
234 }
235
236 // If no, there is maybe one thing not already initialized (mongodb...)
237 if (dont_continue === true) return
238
239 server.app.stdout.removeListener('data', onStdout)
240 callback(server)
241 })
242 }
243
244 function searchVideo (url, search, end) {
245 const path = '/api/v1/videos'
246
247 request(url)
248 .get(path + '/search/' + search)
249 .set('Accept', 'application/json')
250 .expect(200)
251 .expect('Content-Type', /json/)
252 .end(end)
253 }
254
255 function uploadVideo (url, access_token, name, description, fixture, special_status, end) {
256 if (!end) {
257 end = special_status
258 special_status = 204
259 }
260
261 const path = '/api/v1/videos'
262
263 request(url)
264 .post(path)
265 .set('Accept', 'application/json')
266 .set('Authorization', 'Bearer ' + access_token)
267 .field('name', name)
268 .field('description', description)
269 .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture))
270 .expect(special_status)
271 .end(end)
272 }
273
274 // ---------------------------------------------------------------------------
275
276 module.exports = testUtils