3 const child_process
= require('child_process')
4 const exec
= child_process
.exec
5 const fork
= child_process
.fork
6 const pathUtils
= require('path')
7 const request
= require('supertest')
10 flushTests: flushTests
,
11 getFriendsList: getFriendsList
,
13 getVideosList: getVideosList
,
15 loginAndGetAccessToken: loginAndGetAccessToken
,
16 makeFriends: makeFriends
,
17 quitFriends: quitFriends
,
18 removeVideo: removeVideo
,
19 flushAndRunMultipleServers: flushAndRunMultipleServers
,
21 searchVideo: searchVideo
,
22 uploadVideo: uploadVideo
25 // ---------------------- Export functions --------------------
27 function flushTests (callback
) {
28 exec(pathUtils
.join(__dirname
, '../../../bin/clean_test.sh'), callback
)
31 function getFriendsList (url
, end
) {
32 const path
= '/api/v1/pods/'
36 .set('Accept', 'application/json')
38 .expect('Content-Type', /json/)
42 function getVideo (url
, id
, end
) {
43 const path
= '/api/v1/videos/' + id
47 .set('Accept', 'application/json')
49 .expect('Content-Type', /json/)
53 function getVideosList (url
, end
) {
54 const path
= '/api/v1/videos'
58 .set('Accept', 'application/json')
60 .expect('Content-Type', /json/)
64 function login (url
, client
, user
, expected_status
, end
) {
70 const path
= '/api/v1/users/token'
74 client_secret: client
.secret
,
75 username: user
.username
,
76 password: user
.password
,
77 response_type: 'code',
78 grant_type: 'password',
86 .expect(expected_status
)
90 function loginAndGetAccessToken (server
, callback
) {
91 login(server
.url
, server
.client
, server
.user
, 200, function (err
, res
) {
92 if (err
) return callback(err
)
94 return callback(null, res
.body
.access_token
)
98 function makeFriends (url
, expected_status
, callback
) {
100 callback
= expected_status
101 expected_status
= 204
104 const path
= '/api/v1/pods/makefriends'
106 // The first pod make friend with the third
109 .set('Accept', 'application/json')
110 .expect(expected_status
)
111 .end(function (err
, res
) {
114 // Wait for the request between pods
115 setTimeout(callback
, 1000)
119 function quitFriends (url
, callback
) {
120 const path
= '/api/v1/pods/quitfriends'
122 // The first pod make friend with the third
125 .set('Accept', 'application/json')
127 .end(function (err
, res
) {
130 // Wait for the request between pods
131 setTimeout(callback
, 1000)
135 function removeVideo (url
, token
, id
, expected_status
, end
) {
137 end
= expected_status
138 expected_status
= 204
141 const path
= '/api/v1/videos'
144 .delete(path
+ '/' + id
)
145 .set('Accept', 'application/json')
146 .set('Authorization', 'Bearer ' + token
)
147 .expect(expected_status
)
151 function flushAndRunMultipleServers (total_servers
, serversRun
) {
156 function anotherServerDone (number
, app
, url
) {
157 apps
[number
- 1] = app
158 urls
[number
- 1] = url
160 if (i
=== total_servers
) {
161 serversRun(apps
, urls
)
165 flushTests(function () {
166 for (let j
= 1; j
<= total_servers
; j
++) {
167 // For the virtual buffer
168 setTimeout(function () {
169 runServer(j
, function (app
, url
) {
170 anotherServerDone(j
, app
, url
)
177 function runServer (number
, callback
) {
180 url: `http://localhost:${9000 + number}`,
191 // These actions are async so we need to be sure that they have both been done
192 const server_run_string
= {
193 'Connected to mongodb': false,
194 'Server listening on port': false
198 client_id: 'Client id: ([a-f0-9]+)',
199 client_secret: 'Client secret: (.+)',
200 user_username: 'Username: (.+)',
201 user_password: 'User password: (.+)'
204 // Share the environment
205 const env
= Object
.create(process
.env
)
206 env
.NODE_ENV
= 'test'
207 env
.NODE_APP_INSTANCE
= number
214 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
215 server
.app
.stdout
.on('data', function onStdout (data
) {
216 let dont_continue
= false
218 // Capture things if we want to
219 for (const key
of Object
.keys(regexps
)) {
220 const regexp
= regexps
[key
]
221 const matches
= data
.toString().match(regexp
)
222 if (matches
!== null) {
223 if (key
=== 'client_id') server
.client
.id
= matches
[1]
224 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
225 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
226 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
230 // Check if all required sentences are here
231 for (const key
of Object
.keys(server_run_string
)) {
232 if (data
.toString().indexOf(key
) !== -1) server_run_string
[key
] = true
233 if (server_run_string
[key
] === false) dont_continue
= true
236 // If no, there is maybe one thing not already initialized (mongodb...)
237 if (dont_continue
=== true) return
239 server
.app
.stdout
.removeListener('data', onStdout
)
244 function searchVideo (url
, search
, end
) {
245 const path
= '/api/v1/videos'
248 .get(path
+ '/search/' + search
)
249 .set('Accept', 'application/json')
251 .expect('Content-Type', /json/)
255 function uploadVideo (url
, access_token
, name
, description
, fixture
, special_status
, end
) {
261 const path
= '/api/v1/videos'
265 .set('Accept', 'application/json')
266 .set('Authorization', 'Bearer ' + access_token
)
268 .field('description', description
)
269 .attach('videofile', pathUtils
.join(__dirname
, 'fixtures', fixture
))
270 .expect(special_status
)
274 // ---------------------------------------------------------------------------
276 module
.exports
= testUtils