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
,
18 loginAndGetAccessToken: loginAndGetAccessToken
,
19 makeFriends: makeFriends
,
20 quitFriends: quitFriends
,
21 removeVideo: removeVideo
,
22 flushAndRunMultipleServers: flushAndRunMultipleServers
,
24 searchVideo: searchVideo
,
25 searchVideoWithPagination: searchVideoWithPagination
,
27 uploadVideo: uploadVideo
30 // ---------------------- Export functions --------------------
32 function dateIsValid (dateString
) {
33 const dateToCheck
= new Date(dateString
)
34 const now
= new Date()
36 // Check if the interval is more than 2 minutes
37 if (now
- dateToCheck
> 120000) return false
42 function flushTests (callback
) {
43 exec('npm run clean:server:test', callback
)
46 function getFriendsList (url
, end
) {
47 const path
= '/api/v1/pods/'
51 .set('Accept', 'application/json')
53 .expect('Content-Type', /json/)
57 function getVideo (url
, id
, end
) {
58 const path
= '/api/v1/videos/' + id
62 .set('Accept', 'application/json')
64 .expect('Content-Type', /json/)
68 function getVideosList (url
, end
) {
69 const path
= '/api/v1/videos'
73 .set('Accept', 'application/json')
75 .expect('Content-Type', /json/)
79 function getVideosListPagination (url
, start
, count
, end
) {
80 const path
= '/api/v1/videos'
84 .query({ start: start
})
85 .query({ count: count
})
86 .set('Accept', 'application/json')
88 .expect('Content-Type', /json/)
92 function login (url
, client
, user
, expectedStatus
, end
) {
98 const path
= '/api/v1/users/token'
101 client_id: client
.id
,
102 client_secret: client
.secret
,
103 username: user
.username
,
104 password: user
.password
,
105 response_type: 'code',
106 grant_type: 'password',
114 .expect(expectedStatus
)
118 function loginAndGetAccessToken (server
, callback
) {
119 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
120 if (err
) return callback(err
)
122 return callback(null, res
.body
.access_token
)
126 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
128 callback
= expectedStatus
132 const path
= '/api/v1/pods/makefriends'
134 // The first pod make friend with the third
137 .set('Accept', 'application/json')
138 .set('Authorization', 'Bearer ' + accessToken
)
139 .expect(expectedStatus
)
140 .end(function (err
, res
) {
143 // Wait for the request between pods
144 setTimeout(callback
, 1000)
148 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
150 callback
= expectedStatus
154 const path
= '/api/v1/pods/quitfriends'
156 // The first pod make friend with the third
159 .set('Accept', 'application/json')
160 .set('Authorization', 'Bearer ' + accessToken
)
161 .expect(expectedStatus
)
162 .end(function (err
, res
) {
165 // Wait for the request between pods
166 setTimeout(callback
, 1000)
170 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
176 const path
= '/api/v1/videos'
179 .delete(path
+ '/' + id
)
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + token
)
182 .expect(expectedStatus
)
186 function flushAndRunMultipleServers (totalServers
, serversRun
) {
191 function anotherServerDone (number
, app
, url
) {
192 apps
[number
- 1] = app
193 urls
[number
- 1] = url
195 if (i
=== totalServers
) {
196 serversRun(apps
, urls
)
200 flushTests(function () {
201 for (let j
= 1; j
<= totalServers
; j
++) {
202 // For the virtual buffer
203 setTimeout(function () {
204 runServer(j
, function (app
, url
) {
205 anotherServerDone(j
, app
, url
)
212 function runServer (number
, callback
) {
215 url: `http://localhost:${9000 + number}`,
226 // These actions are async so we need to be sure that they have both been done
227 const serverRunString
= {
228 'Connected to mongodb': false,
229 'Server listening on port': false
233 client_id: 'Client id: ([a-f0-9]+)',
234 client_secret: 'Client secret: (.+)',
235 user_username: 'Username: (.+)',
236 user_password: 'User password: (.+)'
239 // Share the environment
240 const env
= Object
.create(process
.env
)
241 env
.NODE_ENV
= 'test'
242 env
.NODE_APP_INSTANCE
= number
249 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
250 server
.app
.stdout
.on('data', function onStdout (data
) {
251 let dontContinue
= false
253 // Capture things if we want to
254 for (const key
of Object
.keys(regexps
)) {
255 const regexp
= regexps
[key
]
256 const matches
= data
.toString().match(regexp
)
257 if (matches
!== null) {
258 if (key
=== 'client_id') server
.client
.id
= matches
[1]
259 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
260 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
261 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
265 // Check if all required sentences are here
266 for (const key
of Object
.keys(serverRunString
)) {
267 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
268 if (serverRunString
[key
] === false) dontContinue
= true
271 // If no, there is maybe one thing not already initialized (mongodb...)
272 if (dontContinue
=== true) return
274 server
.app
.stdout
.removeListener('data', onStdout
)
279 function searchVideo (url
, search
, end
) {
280 const path
= '/api/v1/videos'
283 .get(path
+ '/search/' + search
)
284 .set('Accept', 'application/json')
286 .expect('Content-Type', /json/)
290 function searchVideoWithPagination (url
, search
, start
, count
, end
) {
291 const path
= '/api/v1/videos'
294 .get(path
+ '/search/' + search
)
295 .query({ start: start
})
296 .query({ count: count
})
297 .set('Accept', 'application/json')
299 .expect('Content-Type', /json/)
303 function testImage (url
, videoName
, imagePath
, callback
) {
307 .end(function (err
, res
) {
308 if (err
) return callback(err
)
310 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
311 if (err
) return callback(err
)
313 callback(null, data
.equals(res
.body
))
318 function uploadVideo (url
, accessToken
, name
, description
, fixture
, specialStatus
, end
) {
324 const path
= '/api/v1/videos'
328 .set('Accept', 'application/json')
329 .set('Authorization', 'Bearer ' + accessToken
)
331 .field('description', description
)
332 .attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
333 .expect(specialStatus
)
337 // ---------------------------------------------------------------------------
339 module
.exports
= testUtils