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 .set('Accept', 'application/json')
77 .expect('Content-Type', /json/)
81 function getVideosListPagination (url
, start
, count
, end
) {
82 const path
= '/api/v1/videos'
86 .query({ start: start
})
87 .query({ count: count
})
88 .set('Accept', 'application/json')
90 .expect('Content-Type', /json/)
94 function getVideosListSort (url
, sort
, end
) {
95 const path
= '/api/v1/videos'
99 .query({ sort: sort
})
100 .set('Accept', 'application/json')
102 .expect('Content-Type', /json/)
106 function login (url
, client
, user
, expectedStatus
, end
) {
112 const path
= '/api/v1/users/token'
115 client_id: client
.id
,
116 client_secret: client
.secret
,
117 username: user
.username
,
118 password: user
.password
,
119 response_type: 'code',
120 grant_type: 'password',
128 .expect(expectedStatus
)
132 function loginAndGetAccessToken (server
, callback
) {
133 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
134 if (err
) return callback(err
)
136 return callback(null, res
.body
.access_token
)
140 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
142 callback
= expectedStatus
146 const path
= '/api/v1/pods/makefriends'
148 // The first pod make friend with the third
151 .set('Accept', 'application/json')
152 .set('Authorization', 'Bearer ' + accessToken
)
153 .expect(expectedStatus
)
154 .end(function (err
, res
) {
157 // Wait for the request between pods
158 setTimeout(callback
, 1000)
162 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
164 callback
= expectedStatus
168 const path
= '/api/v1/pods/quitfriends'
170 // The first pod make friend with the third
173 .set('Accept', 'application/json')
174 .set('Authorization', 'Bearer ' + accessToken
)
175 .expect(expectedStatus
)
176 .end(function (err
, res
) {
179 // Wait for the request between pods
180 setTimeout(callback
, 1000)
184 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
190 const path
= '/api/v1/videos'
193 .delete(path
+ '/' + id
)
194 .set('Accept', 'application/json')
195 .set('Authorization', 'Bearer ' + token
)
196 .expect(expectedStatus
)
200 function flushAndRunMultipleServers (totalServers
, serversRun
) {
205 function anotherServerDone (number
, app
, url
) {
206 apps
[number
- 1] = app
207 urls
[number
- 1] = url
209 if (i
=== totalServers
) {
210 serversRun(apps
, urls
)
214 flushTests(function () {
215 for (let j
= 1; j
<= totalServers
; j
++) {
216 // For the virtual buffer
217 setTimeout(function () {
218 runServer(j
, function (app
, url
) {
219 anotherServerDone(j
, app
, url
)
226 function runServer (number
, callback
) {
229 url: `http://localhost:${9000 + number}`,
240 // These actions are async so we need to be sure that they have both been done
241 const serverRunString
= {
242 'Connected to mongodb': false,
243 'Server listening on port': false
247 client_id: 'Client id: ([a-f0-9]+)',
248 client_secret: 'Client secret: (.+)',
249 user_username: 'Username: (.+)',
250 user_password: 'User password: (.+)'
253 // Share the environment
254 const env
= Object
.create(process
.env
)
255 env
.NODE_ENV
= 'test'
256 env
.NODE_APP_INSTANCE
= number
263 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
264 server
.app
.stdout
.on('data', function onStdout (data
) {
265 let dontContinue
= false
267 // Capture things if we want to
268 for (const key
of Object
.keys(regexps
)) {
269 const regexp
= regexps
[key
]
270 const matches
= data
.toString().match(regexp
)
271 if (matches
!== null) {
272 if (key
=== 'client_id') server
.client
.id
= matches
[1]
273 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
274 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
275 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
279 // Check if all required sentences are here
280 for (const key
of Object
.keys(serverRunString
)) {
281 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
282 if (serverRunString
[key
] === false) dontContinue
= true
285 // If no, there is maybe one thing not already initialized (mongodb...)
286 if (dontContinue
=== true) return
288 server
.app
.stdout
.removeListener('data', onStdout
)
293 function searchVideo (url
, search
, end
) {
294 const path
= '/api/v1/videos'
297 .get(path
+ '/search/' + search
)
298 .set('Accept', 'application/json')
300 .expect('Content-Type', /json/)
304 function searchVideoWithPagination (url
, search
, start
, count
, end
) {
305 const path
= '/api/v1/videos'
308 .get(path
+ '/search/' + search
)
309 .query({ start: start
})
310 .query({ count: count
})
311 .set('Accept', 'application/json')
313 .expect('Content-Type', /json/)
317 function searchVideoWithSort (url
, search
, sort
, end
) {
318 const path
= '/api/v1/videos'
321 .get(path
+ '/search/' + search
)
322 .query({ sort: sort
})
323 .set('Accept', 'application/json')
325 .expect('Content-Type', /json/)
329 function testImage (url
, videoName
, imagePath
, callback
) {
333 .end(function (err
, res
) {
334 if (err
) return callback(err
)
336 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
337 if (err
) return callback(err
)
339 callback(null, data
.equals(res
.body
))
344 function uploadVideo (url
, accessToken
, name
, description
, fixture
, specialStatus
, end
) {
350 const path
= '/api/v1/videos'
354 .set('Accept', 'application/json')
355 .set('Authorization', 'Bearer ' + accessToken
)
357 .field('description', description
)
358 .attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
359 .expect(specialStatus
)
363 // ---------------------------------------------------------------------------
365 module
.exports
= testUtils