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 dateIsValid: dateIsValid
,
12 flushTests: flushTests
,
13 getAllVideosListBy: getAllVideosListBy
,
15 getFriendsList: getFriendsList
,
17 getVideosList: getVideosList
,
18 getVideosListPagination: getVideosListPagination
,
19 getVideosListSort: getVideosListSort
,
21 loginAndGetAccessToken: loginAndGetAccessToken
,
22 makeFriends: makeFriends
,
23 quitFriends: quitFriends
,
24 removeVideo: removeVideo
,
25 flushAndRunMultipleServers: flushAndRunMultipleServers
,
27 searchVideo: searchVideo
,
28 searchVideoWithPagination: searchVideoWithPagination
,
29 searchVideoWithSort: searchVideoWithSort
,
31 uploadVideo: uploadVideo
34 // ---------------------- Export functions --------------------
36 function dateIsValid (dateString
) {
37 const dateToCheck
= new Date(dateString
)
38 const now
= new Date()
40 // Check if the interval is more than 2 minutes
41 if (now
- dateToCheck
> 120000) return false
46 function flushTests (callback
) {
47 exec('npm run clean:server:test', callback
)
50 function getAllVideosListBy (url
, end
) {
51 const path
= '/api/v1/videos'
55 .query({ sort: 'createdDate' })
57 .query({ count: 10000 })
58 .set('Accept', 'application/json')
60 .expect('Content-Type', /json/)
64 function getClient (url
, end
) {
65 const path
= '/api/v1/users/client'
69 .set('Accept', 'application/json')
71 .expect('Content-Type', /json/)
75 function getFriendsList (url
, end
) {
76 const path
= '/api/v1/pods/'
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function getVideo (url
, id
, end
) {
87 const path
= '/api/v1/videos/' + id
91 .set('Accept', 'application/json')
93 .expect('Content-Type', /json/)
97 function getVideosList (url
, end
) {
98 const path
= '/api/v1/videos'
102 .query({ sort: 'name' })
103 .set('Accept', 'application/json')
105 .expect('Content-Type', /json/)
109 function getVideosListPagination (url
, start
, count
, end
) {
110 const path
= '/api/v1/videos'
114 .query({ start: start
})
115 .query({ count: count
})
116 .set('Accept', 'application/json')
118 .expect('Content-Type', /json/)
122 function getVideosListSort (url
, sort
, end
) {
123 const path
= '/api/v1/videos'
127 .query({ sort: sort
})
128 .set('Accept', 'application/json')
130 .expect('Content-Type', /json/)
134 function login (url
, client
, user
, expectedStatus
, end
) {
140 const path
= '/api/v1/users/token'
143 client_id: client
.id
,
144 client_secret: client
.secret
,
145 username: user
.username
,
146 password: user
.password
,
147 response_type: 'code',
148 grant_type: 'password',
156 .expect(expectedStatus
)
160 function loginAndGetAccessToken (server
, callback
) {
161 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
162 if (err
) return callback(err
)
164 return callback(null, res
.body
.access_token
)
168 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
170 callback
= expectedStatus
174 const path
= '/api/v1/pods/makefriends'
176 // The first pod make friend with the third
179 .set('Accept', 'application/json')
180 .set('Authorization', 'Bearer ' + accessToken
)
181 .expect(expectedStatus
)
182 .end(function (err
, res
) {
185 // Wait for the request between pods
186 setTimeout(callback
, 1000)
190 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
192 callback
= expectedStatus
196 const path
= '/api/v1/pods/quitfriends'
198 // The first pod make friend with the third
201 .set('Accept', 'application/json')
202 .set('Authorization', 'Bearer ' + accessToken
)
203 .expect(expectedStatus
)
204 .end(function (err
, res
) {
207 // Wait for the request between pods
208 setTimeout(callback
, 1000)
212 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
218 const path
= '/api/v1/videos'
221 .delete(path
+ '/' + id
)
222 .set('Accept', 'application/json')
223 .set('Authorization', 'Bearer ' + token
)
224 .expect(expectedStatus
)
228 function flushAndRunMultipleServers (totalServers
, serversRun
) {
233 function anotherServerDone (number
, app
, url
) {
234 apps
[number
- 1] = app
235 urls
[number
- 1] = url
237 if (i
=== totalServers
) {
238 serversRun(apps
, urls
)
242 flushTests(function () {
243 for (let j
= 1; j
<= totalServers
; j
++) {
244 // For the virtual buffer
245 setTimeout(function () {
246 runServer(j
, function (app
, url
) {
247 anotherServerDone(j
, app
, url
)
254 function runServer (number
, callback
) {
257 url: `http://localhost:${9000 + number}`,
268 // These actions are async so we need to be sure that they have both been done
269 const serverRunString
= {
270 'Connected to mongodb': false,
271 'Server listening on port': false
275 client_id: 'Client id: ([a-f0-9]+)',
276 client_secret: 'Client secret: (.+)',
277 user_username: 'Username: (.+)',
278 user_password: 'User password: (.+)'
281 // Share the environment
282 const env
= Object
.create(process
.env
)
283 env
.NODE_ENV
= 'test'
284 env
.NODE_APP_INSTANCE
= number
291 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
292 server
.app
.stdout
.on('data', function onStdout (data
) {
293 let dontContinue
= false
295 // Capture things if we want to
296 for (const key
of Object
.keys(regexps
)) {
297 const regexp
= regexps
[key
]
298 const matches
= data
.toString().match(regexp
)
299 if (matches
!== null) {
300 if (key
=== 'client_id') server
.client
.id
= matches
[1]
301 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
302 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
303 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
307 // Check if all required sentences are here
308 for (const key
of Object
.keys(serverRunString
)) {
309 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
310 if (serverRunString
[key
] === false) dontContinue
= true
313 // If no, there is maybe one thing not already initialized (mongodb...)
314 if (dontContinue
=== true) return
316 server
.app
.stdout
.removeListener('data', onStdout
)
321 function searchVideo (url
, search
, field
, end
) {
327 const path
= '/api/v1/videos'
328 const req
= request(url
)
329 .get(path
+ '/search/' + search
)
330 .set('Accept', 'application/json')
332 if (field
) req
.query({ field: field
})
334 .expect('Content-Type', /json/)
338 function searchVideoWithPagination (url
, search
, field
, start
, count
, end
) {
339 const path
= '/api/v1/videos'
342 .get(path
+ '/search/' + search
)
343 .query({ start: start
})
344 .query({ count: count
})
345 .query({ field: field
})
346 .set('Accept', 'application/json')
348 .expect('Content-Type', /json/)
352 function searchVideoWithSort (url
, search
, sort
, end
) {
353 const path
= '/api/v1/videos'
356 .get(path
+ '/search/' + search
)
357 .query({ sort: sort
})
358 .set('Accept', 'application/json')
360 .expect('Content-Type', /json/)
364 function testImage (url
, videoName
, imagePath
, callback
) {
365 // Don't test images if the node env is not set
366 // Because we need a special ffmpeg version for this test
367 if (process
.env
.NODE_TEST_IMAGE
) {
371 .end(function (err
, res
) {
372 if (err
) return callback(err
)
374 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
375 if (err
) return callback(err
)
377 callback(null, data
.equals(res
.body
))
381 console
.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
386 function uploadVideo (url
, accessToken
, name
, description
, tags
, fixture
, specialStatus
, end
) {
392 const path
= '/api/v1/videos'
394 const req
= request(url
)
396 .set('Accept', 'application/json')
397 .set('Authorization', 'Bearer ' + accessToken
)
399 .field('description', description
)
401 for (let i
= 0; i
< tags
.length
; i
++) {
402 req
.field('tags[' + i
+ ']', tags
[i
])
406 if (pathUtils
.isAbsolute(fixture
)) {
409 filepath
= pathUtils
.join(__dirname
, 'fixtures', fixture
)
412 req
.attach('videofile', filepath
)
413 .expect(specialStatus
)
417 // ---------------------------------------------------------------------------
419 module
.exports
= testUtils