4 var child_process
= require('child_process')
5 var exec
= child_process
.exec
6 var fork
= child_process
.fork
7 var pathUtils
= require('path')
8 var request
= require('supertest')
11 flushTests: flushTests
,
12 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
, '../../scripts/clean_test.sh'), callback
)
29 function getFriendsList (url
, end
) {
30 var path
= '/api/v1/pods/'
34 .set('Accept', 'application/json')
36 .expect('Content-Type', /json/)
40 function getVideosList (url
, end
) {
41 var path
= '/api/v1/videos'
45 .set('Accept', 'application/json')
47 .expect('Content-Type', /json/)
51 function makeFriends (url
, expected_status
, callback
) {
53 callback
= expected_status
57 var path
= '/api/v1/pods/makefriends'
59 // The first pod make friend with the third
62 .set('Accept', 'application/json')
63 .expect(expected_status
)
64 .end(function (err
, res
) {
67 // Wait for the request between pods
68 setTimeout(callback
, 1000)
72 function quitFriends (url
, callback
) {
73 var path
= '/api/v1/pods/quitfriends'
75 // The first pod make friend with the third
78 .set('Accept', 'application/json')
80 .end(function (err
, res
) {
83 // Wait for the request between pods
84 setTimeout(callback
, 1000)
88 function removeVideo (url
, id
, end
) {
89 var path
= '/api/v1/videos'
92 .delete(path
+ '/' + id
)
93 .set('Accept', 'application/json')
98 function flushAndRunMultipleServers (total_servers
, serversRun
) {
103 function anotherServerDone (number
, app
, url
) {
104 apps
[number
- 1] = app
105 urls
[number
- 1] = url
107 if (i
=== total_servers
) {
108 serversRun(apps
, urls
)
112 flushTests(function () {
113 for (var j
= 1; j
<= total_servers
; j
++) {
114 (function (k
) { // TODO: ES6 with let
115 // For the virtual buffer
116 setTimeout(function () {
117 runServer(k
, function (app
, url
) {
118 anotherServerDone(k
, app
, url
)
126 function runServer (number
, callback
) {
127 var port
= 9000 + number
128 var server_run_string
= {
129 'Connected to mongodb': false,
130 'Server listening on port': false
133 // Share the environment
134 var env
= Object
.create(process
.env
)
135 env
.NODE_ENV
= 'test'
136 env
.NODE_APP_INSTANCE
= number
143 var app
= fork(pathUtils
.join(__dirname
, '../../server.js'), [], options
)
144 app
.stdout
.on('data', function onStdout (data
) {
145 var dont_continue
= false
146 // Check if all required sentences are here
147 for (var key
of Object
.keys(server_run_string
)) {
148 if (data
.toString().indexOf(key
) !== -1) server_run_string
[key
] = true
149 if (server_run_string
[key
] === false) dont_continue
= true
152 // If no, there is maybe one thing not already initialized (mongodb...)
153 if (dont_continue
=== true) return
155 app
.stdout
.removeListener('data', onStdout
)
156 callback(app
, 'http://localhost:' + port
)
160 function searchVideo (url
, search
, end
) {
161 var path
= '/api/v1/videos'
164 .get(path
+ '/search/' + search
)
165 .set('Accept', 'application/json')
167 .expect('Content-Type', /json/)
171 function uploadVideo (url
, name
, description
, fixture
, end
) {
172 var path
= '/api/v1/videos'
176 .set('Accept', 'application/json')
178 .field('description', description
)
179 .attach('input_video', pathUtils
.join(__dirname
, 'fixtures', fixture
))
184 // ---------------------------------------------------------------------------
186 module
.exports
= testUtils