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
,
16 loginAndGetAccessToken: loginAndGetAccessToken
,
17 makeFriends: makeFriends
,
18 quitFriends: quitFriends
,
19 removeVideo: removeVideo
,
20 flushAndRunMultipleServers: flushAndRunMultipleServers
,
22 searchVideo: searchVideo
,
24 uploadVideo: uploadVideo
27 // ---------------------- Export functions --------------------
29 function flushTests (callback
) {
30 exec('npm run clean:server:test', callback
)
33 function getFriendsList (url
, end
) {
34 const path
= '/api/v1/pods/'
38 .set('Accept', 'application/json')
40 .expect('Content-Type', /json/)
44 function getVideo (url
, id
, end
) {
45 const path
= '/api/v1/videos/' + id
49 .set('Accept', 'application/json')
51 .expect('Content-Type', /json/)
55 function getVideosList (url
, end
) {
56 const path
= '/api/v1/videos'
60 .set('Accept', 'application/json')
62 .expect('Content-Type', /json/)
66 function login (url
, client
, user
, expectedStatus
, end
) {
72 const path
= '/api/v1/users/token'
76 client_secret: client
.secret
,
77 username: user
.username
,
78 password: user
.password
,
79 response_type: 'code',
80 grant_type: 'password',
88 .expect(expectedStatus
)
92 function loginAndGetAccessToken (server
, callback
) {
93 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
94 if (err
) return callback(err
)
96 return callback(null, res
.body
.access_token
)
100 function makeFriends (url
, expectedStatus
, callback
) {
102 callback
= expectedStatus
106 const path
= '/api/v1/pods/makefriends'
108 // The first pod make friend with the third
111 .set('Accept', 'application/json')
112 .expect(expectedStatus
)
113 .end(function (err
, res
) {
116 // Wait for the request between pods
117 setTimeout(callback
, 1000)
121 function quitFriends (url
, callback
) {
122 const path
= '/api/v1/pods/quitfriends'
124 // The first pod make friend with the third
127 .set('Accept', 'application/json')
129 .end(function (err
, res
) {
132 // Wait for the request between pods
133 setTimeout(callback
, 1000)
137 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
143 const path
= '/api/v1/videos'
146 .delete(path
+ '/' + id
)
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + token
)
149 .expect(expectedStatus
)
153 function flushAndRunMultipleServers (totalServers
, serversRun
) {
158 function anotherServerDone (number
, app
, url
) {
159 apps
[number
- 1] = app
160 urls
[number
- 1] = url
162 if (i
=== totalServers
) {
163 serversRun(apps
, urls
)
167 flushTests(function () {
168 for (let j
= 1; j
<= totalServers
; j
++) {
169 // For the virtual buffer
170 setTimeout(function () {
171 runServer(j
, function (app
, url
) {
172 anotherServerDone(j
, app
, url
)
179 function runServer (number
, callback
) {
182 url: `http://localhost:${9000 + number}`,
193 // These actions are async so we need to be sure that they have both been done
194 const serverRunString
= {
195 'Connected to mongodb': false,
196 'Server listening on port': false
200 client_id: 'Client id: ([a-f0-9]+)',
201 client_secret: 'Client secret: (.+)',
202 user_username: 'Username: (.+)',
203 user_password: 'User password: (.+)'
206 // Share the environment
207 const env
= Object
.create(process
.env
)
208 env
.NODE_ENV
= 'test'
209 env
.NODE_APP_INSTANCE
= number
216 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
217 server
.app
.stdout
.on('data', function onStdout (data
) {
218 let dontContinue
= false
220 // Capture things if we want to
221 for (const key
of Object
.keys(regexps
)) {
222 const regexp
= regexps
[key
]
223 const matches
= data
.toString().match(regexp
)
224 if (matches
!== null) {
225 if (key
=== 'client_id') server
.client
.id
= matches
[1]
226 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
227 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
228 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
232 // Check if all required sentences are here
233 for (const key
of Object
.keys(serverRunString
)) {
234 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
235 if (serverRunString
[key
] === false) dontContinue
= true
238 // If no, there is maybe one thing not already initialized (mongodb...)
239 if (dontContinue
=== true) return
241 server
.app
.stdout
.removeListener('data', onStdout
)
246 function searchVideo (url
, search
, end
) {
247 const path
= '/api/v1/videos'
250 .get(path
+ '/search/' + search
)
251 .set('Accept', 'application/json')
253 .expect('Content-Type', /json/)
257 function testImage (url
, videoName
, imagePath
, callback
) {
261 .end(function (err
, res
) {
262 if (err
) return callback(err
)
264 fs
.readFile(pathUtils
.join(__dirname
, 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
265 if (err
) return callback(err
)
267 callback(null, data
.equals(res
.body
))
272 function uploadVideo (url
, accessToken
, name
, description
, fixture
, specialStatus
, end
) {
278 const path
= '/api/v1/videos'
282 .set('Accept', 'application/json')
283 .set('Authorization', 'Bearer ' + accessToken
)
285 .field('description', description
)
286 .attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
287 .expect(specialStatus
)
291 // ---------------------------------------------------------------------------
293 module
.exports
= testUtils