]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - test/api/utils.js
4 var child_process
= require('child_process')
5 var exec
= child_process
.exec
6 var fork
= child_process
.fork
7 var request
= require('supertest')
9 function flushTests (callback
) {
10 exec(__dirname
+ '/../../scripts/clean_test.sh', function () {
15 function getFriendsList (url
, end
) {
16 var path
= '/api/v1/pods/'
20 .set('Accept', 'application/json')
22 .expect('Content-Type', /json/)
26 function getVideosList (url
, end
) {
27 var path
= '/api/v1/videos'
31 .set('Accept', 'application/json')
33 .expect('Content-Type', /json/)
37 function makeFriends (url
, expected_status
, callback
) {
39 callback
= expected_status
43 var path
= '/api/v1/pods/makefriends'
45 // The first pod make friend with the third
48 .set('Accept', 'application/json')
49 .expect(expected_status
)
50 .end(function (err
, res
) {
53 // Wait for the request between pods
54 setTimeout(function () {
60 function quitFriends (url
, callback
) {
61 var path
= '/api/v1/pods/quitfriends'
63 // The first pod make friend with the third
66 .set('Accept', 'application/json')
68 .end(function (err
, res
) {
71 // Wait for the request between pods
72 setTimeout(function () {
78 function uploadVideo (url
, name
, description
, fixture
, end
) {
79 var path
= '/api/v1/videos'
83 .set('Accept', 'application/json')
85 .field('description', description
)
86 .attach('input_video', __dirname
+ '/fixtures/' + fixture
)
91 function removeVideo (url
, id
, end
) {
92 var path
= '/api/v1/videos'
95 .delete(path
+ '/' + id
)
96 .set('Accept', 'application/json')
101 function runMultipleServers (total_servers
, serversRun
) {
106 function anotherServerDone (number
, app
, url
) {
107 apps
[number
- 1] = app
108 urls
[number
- 1] = url
110 if (i
=== total_servers
) {
111 serversRun(apps
, urls
)
115 flushTests(function () {
116 for (var j
= 1; j
<= total_servers
; j
++) {
117 (function (k
) { // TODO: ES6 with let
118 // For the virtual buffer
119 setTimeout(function () {
120 runServer(k
, function (app
, url
) {
121 anotherServerDone(k
, app
, url
)
129 function runServer (number
, callback
) {
130 var port
= 9000 + number
131 var server_run_string
= {
132 'Connected to mongodb': false,
133 'Server listening on port': false
136 // Share the environment
137 var env
= Object
.create(process
.env
)
138 env
.NODE_ENV
= 'test'
139 env
.NODE_APP_INSTANCE
= number
146 var app
= fork(__dirname
+ '/../../server.js', [], options
)
147 app
.stdout
.on('data', function onStdout (data
) {
148 var dont_continue
= false
149 // Check if all required sentences are here
150 for (var key
of Object
.keys(server_run_string
)) {
151 if (data
.toString().indexOf(key
) !== -1) server_run_string
[key
] = true
152 if (server_run_string
[key
] === false) dont_continue
= true
155 // If no, there is maybe one thing not already initialized (mongodb...)
156 if (dont_continue
=== true) return
158 app
.stdout
.removeListener('data', onStdout
)
159 callback(app
, 'http://localhost:' + port
)
163 function searchVideo (url
, search
, end
) {
164 var path
= '/api/v1/videos'
167 .get(path
+ '/search/' + search
)
168 .set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
175 flushTests: flushTests
,
176 getFriendsList: getFriendsList
,
177 getVideosList: getVideosList
,
178 makeFriends: makeFriends
,
179 quitFriends: quitFriends
,
180 removeVideo: removeVideo
,
181 runMultipleServers: runMultipleServers
,
182 runServer: runServer
,
183 searchVideo: searchVideo
,
184 uploadVideo: uploadVideo