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
,
14 getFriendsList: getFriendsList
,
16 getVideosList: getVideosList
,
17 getVideosListPagination: getVideosListPagination
,
18 getVideosListSort: getVideosListSort
,
20 loginAndGetAccessToken: loginAndGetAccessToken
,
21 makeFriends: makeFriends
,
22 quitFriends: quitFriends
,
23 removeVideo: removeVideo
,
24 flushAndRunMultipleServers: flushAndRunMultipleServers
,
26 searchVideo: searchVideo
,
27 searchVideoWithPagination: searchVideoWithPagination
,
28 searchVideoWithSort: searchVideoWithSort
,
30 uploadVideo: uploadVideo
33 // ---------------------- Export functions --------------------
35 function dateIsValid (dateString
) {
36 const dateToCheck
= new Date(dateString
)
37 const now
= new Date()
39 // Check if the interval is more than 2 minutes
40 if (now
- dateToCheck
> 120000) return false
45 function flushTests (callback
) {
46 exec('npm run clean:server:test', callback
)
49 function getAllVideosListBy (url
, end
) {
50 const path
= '/api/v1/videos'
54 .query({ sort: 'createdDate' })
56 .query({ count: 10000 })
57 .set('Accept', 'application/json')
59 .expect('Content-Type', /json/)
63 function getFriendsList (url
, end
) {
64 const path
= '/api/v1/pods/'
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
74 function getVideo (url
, id
, end
) {
75 const path
= '/api/v1/videos/' + id
79 .set('Accept', 'application/json')
81 .expect('Content-Type', /json/)
85 function getVideosList (url
, end
) {
86 const path
= '/api/v1/videos'
90 .query({ sort: 'name' })
91 .set('Accept', 'application/json')
93 .expect('Content-Type', /json/)
97 function getVideosListPagination (url
, start
, count
, end
) {
98 const path
= '/api/v1/videos'
102 .query({ start: start
})
103 .query({ count: count
})
104 .set('Accept', 'application/json')
106 .expect('Content-Type', /json/)
110 function getVideosListSort (url
, sort
, end
) {
111 const path
= '/api/v1/videos'
115 .query({ sort: sort
})
116 .set('Accept', 'application/json')
118 .expect('Content-Type', /json/)
122 function login (url
, client
, user
, expectedStatus
, end
) {
128 const path
= '/api/v1/users/token'
131 client_id: client
.id
,
132 client_secret: client
.secret
,
133 username: user
.username
,
134 password: user
.password
,
135 response_type: 'code',
136 grant_type: 'password',
144 .expect(expectedStatus
)
148 function loginAndGetAccessToken (server
, callback
) {
149 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
150 if (err
) return callback(err
)
152 return callback(null, res
.body
.access_token
)
156 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
158 callback
= expectedStatus
162 const path
= '/api/v1/pods/makefriends'
164 // The first pod make friend with the third
167 .set('Accept', 'application/json')
168 .set('Authorization', 'Bearer ' + accessToken
)
169 .expect(expectedStatus
)
170 .end(function (err
, res
) {
173 // Wait for the request between pods
174 setTimeout(callback
, 1000)
178 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
180 callback
= expectedStatus
184 const path
= '/api/v1/pods/quitfriends'
186 // The first pod make friend with the third
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + accessToken
)
191 .expect(expectedStatus
)
192 .end(function (err
, res
) {
195 // Wait for the request between pods
196 setTimeout(callback
, 1000)
200 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
206 const path
= '/api/v1/videos'
209 .delete(path
+ '/' + id
)
210 .set('Accept', 'application/json')
211 .set('Authorization', 'Bearer ' + token
)
212 .expect(expectedStatus
)
216 function flushAndRunMultipleServers (totalServers
, serversRun
) {
221 function anotherServerDone (number
, app
, url
) {
222 apps
[number
- 1] = app
223 urls
[number
- 1] = url
225 if (i
=== totalServers
) {
226 serversRun(apps
, urls
)
230 flushTests(function () {
231 for (let j
= 1; j
<= totalServers
; j
++) {
232 // For the virtual buffer
233 setTimeout(function () {
234 runServer(j
, function (app
, url
) {
235 anotherServerDone(j
, app
, url
)
242 function runServer (number
, callback
) {
245 url: `http://localhost:${9000 + number}`,
256 // These actions are async so we need to be sure that they have both been done
257 const serverRunString
= {
258 'Connected to mongodb': false,
259 'Server listening on port': false
263 client_id: 'Client id: ([a-f0-9]+)',
264 client_secret: 'Client secret: (.+)',
265 user_username: 'Username: (.+)',
266 user_password: 'User password: (.+)'
269 // Share the environment
270 const env
= Object
.create(process
.env
)
271 env
.NODE_ENV
= 'test'
272 env
.NODE_APP_INSTANCE
= number
279 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
280 server
.app
.stdout
.on('data', function onStdout (data
) {
281 let dontContinue
= false
283 // Capture things if we want to
284 for (const key
of Object
.keys(regexps
)) {
285 const regexp
= regexps
[key
]
286 const matches
= data
.toString().match(regexp
)
287 if (matches
!== null) {
288 if (key
=== 'client_id') server
.client
.id
= matches
[1]
289 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
290 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
291 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
295 // Check if all required sentences are here
296 for (const key
of Object
.keys(serverRunString
)) {
297 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
298 if (serverRunString
[key
] === false) dontContinue
= true
301 // If no, there is maybe one thing not already initialized (mongodb...)
302 if (dontContinue
=== true) return
304 server
.app
.stdout
.removeListener('data', onStdout
)
309 function searchVideo (url
, search
, field
, end
) {
315 const path
= '/api/v1/videos'
316 const req
= request(url
)
317 .get(path
+ '/search/' + search
)
318 .set('Accept', 'application/json')
320 if (field
) req
.query({ field: field
})
322 .expect('Content-Type', /json/)
326 function searchVideoWithPagination (url
, search
, field
, start
, count
, end
) {
327 const path
= '/api/v1/videos'
330 .get(path
+ '/search/' + search
)
331 .query({ start: start
})
332 .query({ count: count
})
333 .query({ field: field
})
334 .set('Accept', 'application/json')
336 .expect('Content-Type', /json/)
340 function searchVideoWithSort (url
, search
, sort
, end
) {
341 const path
= '/api/v1/videos'
344 .get(path
+ '/search/' + search
)
345 .query({ sort: sort
})
346 .set('Accept', 'application/json')
348 .expect('Content-Type', /json/)
352 function testImage (url
, videoName
, imagePath
, callback
) {
356 .end(function (err
, res
) {
357 if (err
) return callback(err
)
359 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
360 if (err
) return callback(err
)
362 callback(null, data
.equals(res
.body
))
367 function uploadVideo (url
, accessToken
, name
, description
, tags
, fixture
, specialStatus
, end
) {
373 const path
= '/api/v1/videos'
375 const req
= request(url
)
377 .set('Accept', 'application/json')
378 .set('Authorization', 'Bearer ' + accessToken
)
380 .field('description', description
)
382 for (let i
= 0; i
< tags
.length
; i
++) {
383 req
.field('tags[' + i
+ ']', tags
[i
])
386 req
.attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
387 .expect(specialStatus
)
391 // ---------------------------------------------------------------------------
393 module
.exports
= testUtils