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
,
14 makeFriends: makeFriends
,
15 quitFriends: quitFriends
,
16 removeVideo: removeVideo
,
17 flushAndRunMultipleServers: flushAndRunMultipleServers
,
19 searchVideo: searchVideo
,
20 uploadVideo: uploadVideo
23 // ---------------------- Export functions --------------------
25 function flushTests (callback
) {
26 exec(pathUtils
.join(__dirname
, '../../../bin/clean_test.sh'), callback
)
29 function getFriendsList (url
, end
) {
30 const path
= '/api/v1/pods/'
34 .set('Accept', 'application/json')
36 .expect('Content-Type', /json/)
40 function getVideo (url
, id
, end
) {
41 const path
= '/api/v1/videos/' + id
45 .set('Accept', 'application/json')
47 .expect('Content-Type', /json/)
51 function getVideosList (url
, end
) {
52 const path
= '/api/v1/videos'
56 .set('Accept', 'application/json')
58 .expect('Content-Type', /json/)
62 function makeFriends (url
, expected_status
, callback
) {
64 callback
= expected_status
68 const path
= '/api/v1/pods/makefriends'
70 // The first pod make friend with the third
73 .set('Accept', 'application/json')
74 .expect(expected_status
)
75 .end(function (err
, res
) {
78 // Wait for the request between pods
79 setTimeout(callback
, 1000)
83 function quitFriends (url
, callback
) {
84 const path
= '/api/v1/pods/quitfriends'
86 // The first pod make friend with the third
89 .set('Accept', 'application/json')
91 .end(function (err
, res
) {
94 // Wait for the request between pods
95 setTimeout(callback
, 1000)
99 function removeVideo (url
, id
, end
) {
100 const path
= '/api/v1/videos'
103 .delete(path
+ '/' + id
)
104 .set('Accept', 'application/json')
109 function flushAndRunMultipleServers (total_servers
, serversRun
) {
114 function anotherServerDone (number
, app
, url
) {
115 apps
[number
- 1] = app
116 urls
[number
- 1] = url
118 if (i
=== total_servers
) {
119 serversRun(apps
, urls
)
123 flushTests(function () {
124 for (let j
= 1; j
<= total_servers
; j
++) {
125 // For the virtual buffer
126 setTimeout(function () {
127 runServer(j
, function (app
, url
) {
128 anotherServerDone(j
, app
, url
)
135 function runServer (number
, callback
) {
136 const port
= 9000 + number
137 const server_run_string
= {
138 'Connected to mongodb': false,
139 'Server listening on port': false
142 // Share the environment
143 const env
= Object
.create(process
.env
)
144 env
.NODE_ENV
= 'test'
145 env
.NODE_APP_INSTANCE
= number
152 const app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
153 app
.stdout
.on('data', function onStdout (data
) {
154 let dont_continue
= false
155 // Check if all required sentences are here
156 for (const key
of Object
.keys(server_run_string
)) {
157 if (data
.toString().indexOf(key
) !== -1) server_run_string
[key
] = true
158 if (server_run_string
[key
] === false) dont_continue
= true
161 // If no, there is maybe one thing not already initialized (mongodb...)
162 if (dont_continue
=== true) return
164 app
.stdout
.removeListener('data', onStdout
)
165 callback(app
, 'http://localhost:' + port
)
169 function searchVideo (url
, search
, end
) {
170 const path
= '/api/v1/videos'
173 .get(path
+ '/search/' + search
)
174 .set('Accept', 'application/json')
176 .expect('Content-Type', /json/)
180 function uploadVideo (url
, name
, description
, fixture
, end
) {
181 const path
= '/api/v1/videos'
185 .set('Accept', 'application/json')
187 .field('description', description
)
188 .attach('input_video', pathUtils
.join(__dirname
, 'fixtures', fixture
))
193 // ---------------------------------------------------------------------------
195 module
.exports
= testUtils