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