Use const/let now we use node 4.2
[oweals/peertube.git] / server / tests / api / utils.js
1 'use strict'
2
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')
8
9 const testUtils = {
10   flushTests: flushTests,
11   getFriendsList: getFriendsList,
12   getVideosList: getVideosList,
13   makeFriends: makeFriends,
14   quitFriends: quitFriends,
15   removeVideo: removeVideo,
16   flushAndRunMultipleServers: flushAndRunMultipleServers,
17   runServer: runServer,
18   searchVideo: searchVideo,
19   uploadVideo: uploadVideo
20 }
21
22 // ---------------------- Export functions --------------------
23
24 function flushTests (callback) {
25   exec(pathUtils.join(__dirname, '../../../bin/clean_test.sh'), callback)
26 }
27
28 function getFriendsList (url, end) {
29   const path = '/api/v1/pods/'
30
31   request(url)
32     .get(path)
33     .set('Accept', 'application/json')
34     .expect(200)
35     .expect('Content-Type', /json/)
36     .end(end)
37 }
38
39 function getVideosList (url, end) {
40   const path = '/api/v1/videos'
41
42   request(url)
43     .get(path)
44     .set('Accept', 'application/json')
45     .expect(200)
46     .expect('Content-Type', /json/)
47     .end(end)
48 }
49
50 function makeFriends (url, expected_status, callback) {
51   if (!callback) {
52     callback = expected_status
53     expected_status = 204
54   }
55
56   const path = '/api/v1/pods/makefriends'
57
58   // The first pod make friend with the third
59   request(url)
60     .get(path)
61     .set('Accept', 'application/json')
62     .expect(expected_status)
63     .end(function (err, res) {
64       if (err) throw err
65
66       // Wait for the request between pods
67       setTimeout(callback, 1000)
68     })
69 }
70
71 function quitFriends (url, callback) {
72   const path = '/api/v1/pods/quitfriends'
73
74   // The first pod make friend with the third
75   request(url)
76     .get(path)
77     .set('Accept', 'application/json')
78     .expect(204)
79     .end(function (err, res) {
80       if (err) throw err
81
82       // Wait for the request between pods
83       setTimeout(callback, 1000)
84     })
85 }
86
87 function removeVideo (url, id, end) {
88   const path = '/api/v1/videos'
89
90   request(url)
91     .delete(path + '/' + id)
92     .set('Accept', 'application/json')
93     .expect(204)
94     .end(end)
95 }
96
97 function flushAndRunMultipleServers (total_servers, serversRun) {
98   let apps = []
99   let urls = []
100   let i = 0
101
102   function anotherServerDone (number, app, url) {
103     apps[number - 1] = app
104     urls[number - 1] = url
105     i++
106     if (i === total_servers) {
107       serversRun(apps, urls)
108     }
109   }
110
111   flushTests(function () {
112     for (let j = 1; j <= total_servers; j++) {
113       // For the virtual buffer
114       setTimeout(function () {
115         runServer(j, function (app, url) {
116           anotherServerDone(j, app, url)
117         })
118       }, 1000 * j)
119     }
120   })
121 }
122
123 function runServer (number, callback) {
124   const port = 9000 + number
125   const server_run_string = {
126     'Connected to mongodb': false,
127     'Server listening on port': false
128   }
129
130   // Share the environment
131   const env = Object.create(process.env)
132   env.NODE_ENV = 'test'
133   env.NODE_APP_INSTANCE = number
134   const options = {
135     silent: true,
136     env: env,
137     detached: true
138   }
139
140   const app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
141   app.stdout.on('data', function onStdout (data) {
142     let dont_continue = false
143     // Check if all required sentences are here
144     for (const key of Object.keys(server_run_string)) {
145       if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
146       if (server_run_string[key] === false) dont_continue = true
147     }
148
149     // If no, there is maybe one thing not already initialized (mongodb...)
150     if (dont_continue === true) return
151
152     app.stdout.removeListener('data', onStdout)
153     callback(app, 'http://localhost:' + port)
154   })
155 }
156
157 function searchVideo (url, search, end) {
158   const path = '/api/v1/videos'
159
160   request(url)
161     .get(path + '/search/' + search)
162     .set('Accept', 'application/json')
163     .expect(200)
164     .expect('Content-Type', /json/)
165     .end(end)
166 }
167
168 function uploadVideo (url, name, description, fixture, end) {
169   const path = '/api/v1/videos'
170
171   request(url)
172     .post(path)
173     .set('Accept', 'application/json')
174     .field('name', name)
175     .field('description', description)
176     .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
177     .expect(204)
178     .end(end)
179 }
180
181 // ---------------------------------------------------------------------------
182
183 module.exports = testUtils