4 var child_process = require('child_process')
5 var exec = child_process.exec
6 var fork = child_process.fork
7 var request = require('supertest')
10 flushTests: flushTests,
11 getFriendsList: getFriendsList,
12 getVideosList: getVideosList,
13 makeFriends: makeFriends,
14 quitFriends: quitFriends,
15 removeVideo: removeVideo,
16 flushAndRunMultipleServers: flushAndRunMultipleServers,
18 searchVideo: searchVideo,
19 uploadVideo: uploadVideo
22 // ---------------------- Export functions --------------------
24 function flushTests (callback) {
25 exec(__dirname + '/../../scripts/clean_test.sh', callback)
28 function getFriendsList (url, end) {
29 var path = '/api/v1/pods/'
33 .set('Accept', 'application/json')
35 .expect('Content-Type', /json/)
39 function getVideosList (url, end) {
40 var path = '/api/v1/videos'
44 .set('Accept', 'application/json')
46 .expect('Content-Type', /json/)
50 function makeFriends (url, expected_status, callback) {
52 callback = expected_status
56 var path = '/api/v1/pods/makefriends'
58 // The first pod make friend with the third
61 .set('Accept', 'application/json')
62 .expect(expected_status)
63 .end(function (err, res) {
66 // Wait for the request between pods
67 setTimeout(callback, 1000)
71 function quitFriends (url, callback) {
72 var path = '/api/v1/pods/quitfriends'
74 // The first pod make friend with the third
77 .set('Accept', 'application/json')
79 .end(function (err, res) {
82 // Wait for the request between pods
83 setTimeout(callback, 1000)
87 function removeVideo (url, id, end) {
88 var path = '/api/v1/videos'
91 .delete(path + '/' + id)
92 .set('Accept', 'application/json')
97 function flushAndRunMultipleServers (total_servers, serversRun) {
102 function anotherServerDone (number, app, url) {
103 apps[number - 1] = app
104 urls[number - 1] = url
106 if (i === total_servers) {
107 serversRun(apps, urls)
111 flushTests(function () {
112 for (var j = 1; j <= total_servers; j++) {
113 (function (k) { // TODO: ES6 with let
114 // For the virtual buffer
115 setTimeout(function () {
116 runServer(k, function (app, url) {
117 anotherServerDone(k, app, url)
125 function runServer (number, callback) {
126 var port = 9000 + number
127 var server_run_string = {
128 'Connected to mongodb': false,
129 'Server listening on port': false
132 // Share the environment
133 var env = Object.create(process.env)
134 env.NODE_ENV = 'test'
135 env.NODE_APP_INSTANCE = number
142 var app = fork(__dirname + '/../../server.js', [], options)
143 app.stdout.on('data', function onStdout (data) {
144 var dont_continue = false
145 // Check if all required sentences are here
146 for (var key of Object.keys(server_run_string)) {
147 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
148 if (server_run_string[key] === false) dont_continue = true
151 // If no, there is maybe one thing not already initialized (mongodb...)
152 if (dont_continue === true) return
154 app.stdout.removeListener('data', onStdout)
155 callback(app, 'http://localhost:' + port)
159 function searchVideo (url, search, end) {
160 var path = '/api/v1/videos'
163 .get(path + '/search/' + search)
164 .set('Accept', 'application/json')
166 .expect('Content-Type', /json/)
170 function uploadVideo (url, name, description, fixture, end) {
171 var path = '/api/v1/videos'
175 .set('Accept', 'application/json')
177 .field('description', description)
178 .attach('input_video', __dirname + '/fixtures/' + fixture)
183 // ---------------------------------------------------------------------------
185 module.exports = testUtils