3 const childProcess
= require('child_process')
4 const exec
= childProcess
.exec
5 const fork
= childProcess
.fork
6 const fs
= require('fs')
7 const pathUtils
= require('path')
8 const request
= require('supertest')
11 createUser: createUser
,
12 dateIsValid: dateIsValid
,
13 flushTests: flushTests
,
14 getAllVideosListBy: getAllVideosListBy
,
16 getFriendsList: getFriendsList
,
17 getUserInformation: getUserInformation
,
18 getUsersList: getUsersList
,
20 getVideosList: getVideosList
,
21 getVideosListPagination: getVideosListPagination
,
22 getVideosListSort: getVideosListSort
,
24 loginAndGetAccessToken: loginAndGetAccessToken
,
25 makeFriends: makeFriends
,
26 quitFriends: quitFriends
,
27 removeUser: removeUser
,
28 removeVideo: removeVideo
,
29 flushAndRunMultipleServers: flushAndRunMultipleServers
,
31 searchVideo: searchVideo
,
32 searchVideoWithPagination: searchVideoWithPagination
,
33 searchVideoWithSort: searchVideoWithSort
,
35 uploadVideo: uploadVideo
,
36 updateUser: updateUser
39 // ---------------------- Export functions --------------------
41 function createUser (url
, accessToken
, username
, password
, specialStatus
, end
) {
47 const path
= '/api/v1/users'
51 .set('Accept', 'application/json')
52 .set('Authorization', 'Bearer ' + accessToken
)
53 .send({ username: username
, password: password
})
54 .expect(specialStatus
)
58 function dateIsValid (dateString
) {
59 const dateToCheck
= new Date(dateString
)
60 const now
= new Date()
62 // Check if the interval is more than 2 minutes
63 if (now
- dateToCheck
> 120000) return false
68 function flushTests (callback
) {
69 exec('npm run clean:server:test', callback
)
72 function getAllVideosListBy (url
, end
) {
73 const path
= '/api/v1/videos'
77 .query({ sort: 'createdDate' })
79 .query({ count: 10000 })
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function getClient (url
, end
) {
87 const path
= '/api/v1/users/client'
91 .set('Accept', 'application/json')
93 .expect('Content-Type', /json/)
97 function getUserInformation (url
, accessToken
, end
) {
98 const path
= '/api/v1/users/me'
102 .set('Accept', 'application/json')
103 .set('Authorization', 'Bearer ' + accessToken
)
105 .expect('Content-Type', /json/)
109 function getUsersList (url
, end
) {
110 const path
= '/api/v1/users'
114 .set('Accept', 'application/json')
116 .expect('Content-Type', /json/)
120 function getFriendsList (url
, end
) {
121 const path
= '/api/v1/pods/'
125 .set('Accept', 'application/json')
127 .expect('Content-Type', /json/)
131 function getVideo (url
, id
, end
) {
132 const path
= '/api/v1/videos/' + id
136 .set('Accept', 'application/json')
138 .expect('Content-Type', /json/)
142 function getVideosList (url
, end
) {
143 const path
= '/api/v1/videos'
147 .query({ sort: 'name' })
148 .set('Accept', 'application/json')
150 .expect('Content-Type', /json/)
154 function getVideosListPagination (url
, start
, count
, end
) {
155 const path
= '/api/v1/videos'
159 .query({ start: start
})
160 .query({ count: count
})
161 .set('Accept', 'application/json')
163 .expect('Content-Type', /json/)
167 function getVideosListSort (url
, sort
, end
) {
168 const path
= '/api/v1/videos'
172 .query({ sort: sort
})
173 .set('Accept', 'application/json')
175 .expect('Content-Type', /json/)
179 function login (url
, client
, user
, expectedStatus
, end
) {
185 const path
= '/api/v1/users/token'
188 client_id: client
.id
,
189 client_secret: client
.secret
,
190 username: user
.username
,
191 password: user
.password
,
192 response_type: 'code',
193 grant_type: 'password',
201 .expect(expectedStatus
)
205 function loginAndGetAccessToken (server
, callback
) {
206 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
207 if (err
) return callback(err
)
209 return callback(null, res
.body
.access_token
)
213 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
215 callback
= expectedStatus
219 const path
= '/api/v1/pods/makefriends'
221 // The first pod make friend with the third
224 .set('Accept', 'application/json')
225 .set('Authorization', 'Bearer ' + accessToken
)
226 .expect(expectedStatus
)
227 .end(function (err
, res
) {
230 // Wait for the request between pods
231 setTimeout(callback
, 1000)
235 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
237 callback
= expectedStatus
241 const path
= '/api/v1/pods/quitfriends'
243 // The first pod make friend with the third
246 .set('Accept', 'application/json')
247 .set('Authorization', 'Bearer ' + accessToken
)
248 .expect(expectedStatus
)
249 .end(function (err
, res
) {
252 // Wait for the request between pods
253 setTimeout(callback
, 1000)
257 function removeUser (url
, token
, username
, expectedStatus
, end
) {
263 const path
= '/api/v1/users'
266 .delete(path
+ '/' + username
)
267 .set('Accept', 'application/json')
268 .set('Authorization', 'Bearer ' + token
)
269 .expect(expectedStatus
)
273 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
279 const path
= '/api/v1/videos'
282 .delete(path
+ '/' + id
)
283 .set('Accept', 'application/json')
284 .set('Authorization', 'Bearer ' + token
)
285 .expect(expectedStatus
)
289 function flushAndRunMultipleServers (totalServers
, serversRun
) {
294 function anotherServerDone (number
, app
, url
) {
295 apps
[number
- 1] = app
296 urls
[number
- 1] = url
298 if (i
=== totalServers
) {
299 serversRun(apps
, urls
)
303 flushTests(function () {
304 for (let j
= 1; j
<= totalServers
; j
++) {
305 // For the virtual buffer
306 setTimeout(function () {
307 runServer(j
, function (app
, url
) {
308 anotherServerDone(j
, app
, url
)
315 function runServer (number
, callback
) {
318 url: `http://localhost:${9000 + number}`,
329 // These actions are async so we need to be sure that they have both been done
330 const serverRunString
= {
331 'Connected to mongodb': false,
332 'Server listening on port': false
336 client_id: 'Client id: ([a-f0-9]+)',
337 client_secret: 'Client secret: (.+)',
338 user_username: 'Username: (.+)',
339 user_password: 'User password: (.+)'
342 // Share the environment
343 const env
= Object
.create(process
.env
)
344 env
.NODE_ENV
= 'test'
345 env
.NODE_APP_INSTANCE
= number
352 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
353 server
.app
.stdout
.on('data', function onStdout (data
) {
354 let dontContinue
= false
356 // Capture things if we want to
357 for (const key
of Object
.keys(regexps
)) {
358 const regexp
= regexps
[key
]
359 const matches
= data
.toString().match(regexp
)
360 if (matches
!== null) {
361 if (key
=== 'client_id') server
.client
.id
= matches
[1]
362 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
363 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
364 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
368 // Check if all required sentences are here
369 for (const key
of Object
.keys(serverRunString
)) {
370 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
371 if (serverRunString
[key
] === false) dontContinue
= true
374 // If no, there is maybe one thing not already initialized (mongodb...)
375 if (dontContinue
=== true) return
377 server
.app
.stdout
.removeListener('data', onStdout
)
382 function searchVideo (url
, search
, field
, end
) {
388 const path
= '/api/v1/videos'
389 const req
= request(url
)
390 .get(path
+ '/search/' + search
)
391 .set('Accept', 'application/json')
393 if (field
) req
.query({ field: field
})
395 .expect('Content-Type', /json/)
399 function searchVideoWithPagination (url
, search
, field
, start
, count
, end
) {
400 const path
= '/api/v1/videos'
403 .get(path
+ '/search/' + search
)
404 .query({ start: start
})
405 .query({ count: count
})
406 .query({ field: field
})
407 .set('Accept', 'application/json')
409 .expect('Content-Type', /json/)
413 function searchVideoWithSort (url
, search
, sort
, end
) {
414 const path
= '/api/v1/videos'
417 .get(path
+ '/search/' + search
)
418 .query({ sort: sort
})
419 .set('Accept', 'application/json')
421 .expect('Content-Type', /json/)
425 function testImage (url
, videoName
, imagePath
, callback
) {
426 // Don't test images if the node env is not set
427 // Because we need a special ffmpeg version for this test
428 if (process
.env
.NODE_TEST_IMAGE
) {
432 .end(function (err
, res
) {
433 if (err
) return callback(err
)
435 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
436 if (err
) return callback(err
)
438 callback(null, data
.equals(res
.body
))
442 console
.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
447 function uploadVideo (url
, accessToken
, name
, description
, tags
, fixture
, specialStatus
, end
) {
453 const path
= '/api/v1/videos'
455 const req
= request(url
)
457 .set('Accept', 'application/json')
458 .set('Authorization', 'Bearer ' + accessToken
)
460 .field('description', description
)
462 for (let i
= 0; i
< tags
.length
; i
++) {
463 req
.field('tags[' + i
+ ']', tags
[i
])
467 if (pathUtils
.isAbsolute(fixture
)) {
470 filepath
= pathUtils
.join(__dirname
, 'fixtures', fixture
)
473 req
.attach('videofile', filepath
)
474 .expect(specialStatus
)
478 function updateUser (url
, userId
, accessToken
, newPassword
, end
) {
479 const path
= '/api/v1/users/' + userId
483 .set('Accept', 'application/json')
484 .set('Authorization', 'Bearer ' + accessToken
)
485 .send({ password: newPassword
})
490 // ---------------------------------------------------------------------------
492 module
.exports
= testUtils