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 getFriendsList: getFriendsList
,
15 getVideosList: getVideosList
,
16 getVideosListPagination: getVideosListPagination
,
17 getVideosListSort: getVideosListSort
,
19 loginAndGetAccessToken: loginAndGetAccessToken
,
20 makeFriends: makeFriends
,
21 quitFriends: quitFriends
,
22 removeVideo: removeVideo
,
23 flushAndRunMultipleServers: flushAndRunMultipleServers
,
25 searchVideo: searchVideo
,
26 searchVideoWithPagination: searchVideoWithPagination
,
27 searchVideoWithSort: searchVideoWithSort
,
29 uploadVideo: uploadVideo
32 // ---------------------- Export functions --------------------
34 function dateIsValid (dateString
) {
35 const dateToCheck
= new Date(dateString
)
36 const now
= new Date()
38 // Check if the interval is more than 2 minutes
39 if (now
- dateToCheck
> 120000) return false
44 function flushTests (callback
) {
45 exec('npm run clean:server:test', callback
)
48 function getFriendsList (url
, end
) {
49 const path
= '/api/v1/pods/'
53 .set('Accept', 'application/json')
55 .expect('Content-Type', /json/)
59 function getVideo (url
, id
, end
) {
60 const path
= '/api/v1/videos/' + id
64 .set('Accept', 'application/json')
66 .expect('Content-Type', /json/)
70 function getVideosList (url
, end
) {
71 const path
= '/api/v1/videos'
75 .query({ sort: 'name' })
76 .set('Accept', 'application/json')
78 .expect('Content-Type', /json/)
82 function getVideosListPagination (url
, start
, count
, end
) {
83 const path
= '/api/v1/videos'
87 .query({ start: start
})
88 .query({ count: count
})
89 .set('Accept', 'application/json')
91 .expect('Content-Type', /json/)
95 function getVideosListSort (url
, sort
, end
) {
96 const path
= '/api/v1/videos'
100 .query({ sort: sort
})
101 .set('Accept', 'application/json')
103 .expect('Content-Type', /json/)
107 function login (url
, client
, user
, expectedStatus
, end
) {
113 const path
= '/api/v1/users/token'
116 client_id: client
.id
,
117 client_secret: client
.secret
,
118 username: user
.username
,
119 password: user
.password
,
120 response_type: 'code',
121 grant_type: 'password',
129 .expect(expectedStatus
)
133 function loginAndGetAccessToken (server
, callback
) {
134 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
135 if (err
) return callback(err
)
137 return callback(null, res
.body
.access_token
)
141 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
143 callback
= expectedStatus
147 const path
= '/api/v1/pods/makefriends'
149 // The first pod make friend with the third
152 .set('Accept', 'application/json')
153 .set('Authorization', 'Bearer ' + accessToken
)
154 .expect(expectedStatus
)
155 .end(function (err
, res
) {
158 // Wait for the request between pods
159 setTimeout(callback
, 1000)
163 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
165 callback
= expectedStatus
169 const path
= '/api/v1/pods/quitfriends'
171 // The first pod make friend with the third
174 .set('Accept', 'application/json')
175 .set('Authorization', 'Bearer ' + accessToken
)
176 .expect(expectedStatus
)
177 .end(function (err
, res
) {
180 // Wait for the request between pods
181 setTimeout(callback
, 1000)
185 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
191 const path
= '/api/v1/videos'
194 .delete(path
+ '/' + id
)
195 .set('Accept', 'application/json')
196 .set('Authorization', 'Bearer ' + token
)
197 .expect(expectedStatus
)
201 function flushAndRunMultipleServers (totalServers
, serversRun
) {
206 function anotherServerDone (number
, app
, url
) {
207 apps
[number
- 1] = app
208 urls
[number
- 1] = url
210 if (i
=== totalServers
) {
211 serversRun(apps
, urls
)
215 flushTests(function () {
216 for (let j
= 1; j
<= totalServers
; j
++) {
217 // For the virtual buffer
218 setTimeout(function () {
219 runServer(j
, function (app
, url
) {
220 anotherServerDone(j
, app
, url
)
227 function runServer (number
, callback
) {
230 url: `http://localhost:${9000 + number}`,
241 // These actions are async so we need to be sure that they have both been done
242 const serverRunString
= {
243 'Connected to mongodb': false,
244 'Server listening on port': false
248 client_id: 'Client id: ([a-f0-9]+)',
249 client_secret: 'Client secret: (.+)',
250 user_username: 'Username: (.+)',
251 user_password: 'User password: (.+)'
254 // Share the environment
255 const env
= Object
.create(process
.env
)
256 env
.NODE_ENV
= 'test'
257 env
.NODE_APP_INSTANCE
= number
264 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
265 server
.app
.stdout
.on('data', function onStdout (data
) {
266 let dontContinue
= false
268 // Capture things if we want to
269 for (const key
of Object
.keys(regexps
)) {
270 const regexp
= regexps
[key
]
271 const matches
= data
.toString().match(regexp
)
272 if (matches
!== null) {
273 if (key
=== 'client_id') server
.client
.id
= matches
[1]
274 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
275 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
276 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
280 // Check if all required sentences are here
281 for (const key
of Object
.keys(serverRunString
)) {
282 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
283 if (serverRunString
[key
] === false) dontContinue
= true
286 // If no, there is maybe one thing not already initialized (mongodb...)
287 if (dontContinue
=== true) return
289 server
.app
.stdout
.removeListener('data', onStdout
)
294 function searchVideo (url
, search
, field
, end
) {
300 const path
= '/api/v1/videos'
301 const req
= request(url
)
302 .get(path
+ '/search/' + search
)
303 .set('Accept', 'application/json')
305 if (field
) req
.query({ field: field
})
307 .expect('Content-Type', /json/)
311 function searchVideoWithPagination (url
, search
, field
, start
, count
, end
) {
312 const path
= '/api/v1/videos'
315 .get(path
+ '/search/' + search
)
316 .query({ start: start
})
317 .query({ count: count
})
318 .query({ field: field
})
319 .set('Accept', 'application/json')
321 .expect('Content-Type', /json/)
325 function searchVideoWithSort (url
, search
, sort
, end
) {
326 const path
= '/api/v1/videos'
329 .get(path
+ '/search/' + search
)
330 .query({ sort: sort
})
331 .set('Accept', 'application/json')
333 .expect('Content-Type', /json/)
337 function testImage (url
, videoName
, imagePath
, callback
) {
341 .end(function (err
, res
) {
342 if (err
) return callback(err
)
344 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
345 if (err
) return callback(err
)
347 callback(null, data
.equals(res
.body
))
352 function uploadVideo (url
, accessToken
, name
, description
, tags
, fixture
, specialStatus
, end
) {
358 const path
= '/api/v1/videos'
360 const req
= request(url
)
362 .set('Accept', 'application/json')
363 .set('Authorization', 'Bearer ' + accessToken
)
365 .field('description', description
)
367 for (let i
= 0; i
< tags
.length
; i
++) {
368 req
.field('tags[' + i
+ ']', tags
[i
])
371 req
.attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
372 .expect(specialStatus
)
376 // ---------------------------------------------------------------------------
378 module
.exports
= testUtils