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 flushTests: flushTests
,
12 getFriendsList: getFriendsList
,
14 getVideosList: getVideosList
,
15 getVideosListPagination: getVideosListPagination
,
17 loginAndGetAccessToken: loginAndGetAccessToken
,
18 makeFriends: makeFriends
,
19 quitFriends: quitFriends
,
20 removeVideo: removeVideo
,
21 flushAndRunMultipleServers: flushAndRunMultipleServers
,
23 searchVideo: searchVideo
,
24 searchVideoWithPagination: searchVideoWithPagination
,
26 uploadVideo: uploadVideo
29 // ---------------------- Export functions --------------------
31 function flushTests (callback
) {
32 exec('npm run clean:server:test', callback
)
35 function getFriendsList (url
, end
) {
36 const path
= '/api/v1/pods/'
40 .set('Accept', 'application/json')
42 .expect('Content-Type', /json/)
46 function getVideo (url
, id
, end
) {
47 const path
= '/api/v1/videos/' + id
51 .set('Accept', 'application/json')
53 .expect('Content-Type', /json/)
57 function getVideosList (url
, end
) {
58 const path
= '/api/v1/videos'
62 .set('Accept', 'application/json')
64 .expect('Content-Type', /json/)
68 function getVideosListPagination (url
, start
, count
, end
) {
69 const path
= '/api/v1/videos'
73 .query({ start: start
})
74 .query({ count: count
})
75 .set('Accept', 'application/json')
77 .expect('Content-Type', /json/)
81 function login (url
, client
, user
, expectedStatus
, end
) {
87 const path
= '/api/v1/users/token'
91 client_secret: client
.secret
,
92 username: user
.username
,
93 password: user
.password
,
94 response_type: 'code',
95 grant_type: 'password',
103 .expect(expectedStatus
)
107 function loginAndGetAccessToken (server
, callback
) {
108 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
109 if (err
) return callback(err
)
111 return callback(null, res
.body
.access_token
)
115 function makeFriends (url
, accessToken
, expectedStatus
, callback
) {
117 callback
= expectedStatus
121 const path
= '/api/v1/pods/makefriends'
123 // The first pod make friend with the third
126 .set('Accept', 'application/json')
127 .set('Authorization', 'Bearer ' + accessToken
)
128 .expect(expectedStatus
)
129 .end(function (err
, res
) {
132 // Wait for the request between pods
133 setTimeout(callback
, 1000)
137 function quitFriends (url
, accessToken
, expectedStatus
, callback
) {
139 callback
= expectedStatus
143 const path
= '/api/v1/pods/quitfriends'
145 // The first pod make friend with the third
148 .set('Accept', 'application/json')
149 .set('Authorization', 'Bearer ' + accessToken
)
150 .expect(expectedStatus
)
151 .end(function (err
, res
) {
154 // Wait for the request between pods
155 setTimeout(callback
, 1000)
159 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
165 const path
= '/api/v1/videos'
168 .delete(path
+ '/' + id
)
169 .set('Accept', 'application/json')
170 .set('Authorization', 'Bearer ' + token
)
171 .expect(expectedStatus
)
175 function flushAndRunMultipleServers (totalServers
, serversRun
) {
180 function anotherServerDone (number
, app
, url
) {
181 apps
[number
- 1] = app
182 urls
[number
- 1] = url
184 if (i
=== totalServers
) {
185 serversRun(apps
, urls
)
189 flushTests(function () {
190 for (let j
= 1; j
<= totalServers
; j
++) {
191 // For the virtual buffer
192 setTimeout(function () {
193 runServer(j
, function (app
, url
) {
194 anotherServerDone(j
, app
, url
)
201 function runServer (number
, callback
) {
204 url: `http://localhost:${9000 + number}`,
215 // These actions are async so we need to be sure that they have both been done
216 const serverRunString
= {
217 'Connected to mongodb': false,
218 'Server listening on port': false
222 client_id: 'Client id: ([a-f0-9]+)',
223 client_secret: 'Client secret: (.+)',
224 user_username: 'Username: (.+)',
225 user_password: 'User password: (.+)'
228 // Share the environment
229 const env
= Object
.create(process
.env
)
230 env
.NODE_ENV
= 'test'
231 env
.NODE_APP_INSTANCE
= number
238 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
239 server
.app
.stdout
.on('data', function onStdout (data
) {
240 let dontContinue
= false
242 // Capture things if we want to
243 for (const key
of Object
.keys(regexps
)) {
244 const regexp
= regexps
[key
]
245 const matches
= data
.toString().match(regexp
)
246 if (matches
!== null) {
247 if (key
=== 'client_id') server
.client
.id
= matches
[1]
248 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
249 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
250 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
254 // Check if all required sentences are here
255 for (const key
of Object
.keys(serverRunString
)) {
256 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
257 if (serverRunString
[key
] === false) dontContinue
= true
260 // If no, there is maybe one thing not already initialized (mongodb...)
261 if (dontContinue
=== true) return
263 server
.app
.stdout
.removeListener('data', onStdout
)
268 function searchVideo (url
, search
, end
) {
269 const path
= '/api/v1/videos'
272 .get(path
+ '/search/' + search
)
273 .set('Accept', 'application/json')
275 .expect('Content-Type', /json/)
279 function searchVideoWithPagination (url
, search
, start
, count
, end
) {
280 const path
= '/api/v1/videos'
283 .get(path
+ '/search/' + search
)
284 .query({ start: start
})
285 .query({ count: count
})
286 .set('Accept', 'application/json')
288 .expect('Content-Type', /json/)
292 function testImage (url
, videoName
, imagePath
, callback
) {
296 .end(function (err
, res
) {
297 if (err
) return callback(err
)
299 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
300 if (err
) return callback(err
)
302 callback(null, data
.equals(res
.body
))
307 function uploadVideo (url
, accessToken
, name
, description
, fixture
, specialStatus
, end
) {
313 const path
= '/api/v1/videos'
317 .set('Accept', 'application/json')
318 .set('Authorization', 'Bearer ' + accessToken
)
320 .field('description', description
)
321 .attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
322 .expect(specialStatus
)
326 // ---------------------------------------------------------------------------
328 module
.exports
= testUtils