]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/utils.js
1b2f6105949b7f7710988e3e431c247157cc40e5
[github/Chocobozzz/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 getVideo: getVideo,
13 getVideosList: getVideosList,
14 makeFriends: makeFriends,
15 quitFriends: quitFriends,
16 removeVideo: removeVideo,
17 flushAndRunMultipleServers: flushAndRunMultipleServers,
18 runServer: runServer,
19 searchVideo: searchVideo,
20 uploadVideo: uploadVideo
21 }
22
23 // ---------------------- Export functions --------------------
24
25 function flushTests (callback) {
26 exec(pathUtils.join(__dirname, '../../../bin/clean_test.sh'), callback)
27 }
28
29 function getFriendsList (url, end) {
30 const path = '/api/v1/pods/'
31
32 request(url)
33 .get(path)
34 .set('Accept', 'application/json')
35 .expect(200)
36 .expect('Content-Type', /json/)
37 .end(end)
38 }
39
40 function getVideo (url, id, end) {
41 const path = '/api/v1/videos/' + id
42
43 request(url)
44 .get(path)
45 .set('Accept', 'application/json')
46 .expect(200)
47 .expect('Content-Type', /json/)
48 .end(end)
49 }
50
51 function getVideosList (url, end) {
52 const path = '/api/v1/videos'
53
54 request(url)
55 .get(path)
56 .set('Accept', 'application/json')
57 .expect(200)
58 .expect('Content-Type', /json/)
59 .end(end)
60 }
61
62 function makeFriends (url, expected_status, callback) {
63 if (!callback) {
64 callback = expected_status
65 expected_status = 204
66 }
67
68 const path = '/api/v1/pods/makefriends'
69
70 // The first pod make friend with the third
71 request(url)
72 .get(path)
73 .set('Accept', 'application/json')
74 .expect(expected_status)
75 .end(function (err, res) {
76 if (err) throw err
77
78 // Wait for the request between pods
79 setTimeout(callback, 1000)
80 })
81 }
82
83 function quitFriends (url, callback) {
84 const path = '/api/v1/pods/quitfriends'
85
86 // The first pod make friend with the third
87 request(url)
88 .get(path)
89 .set('Accept', 'application/json')
90 .expect(204)
91 .end(function (err, res) {
92 if (err) throw err
93
94 // Wait for the request between pods
95 setTimeout(callback, 1000)
96 })
97 }
98
99 function removeVideo (url, id, end) {
100 const path = '/api/v1/videos'
101
102 request(url)
103 .delete(path + '/' + id)
104 .set('Accept', 'application/json')
105 .expect(204)
106 .end(end)
107 }
108
109 function flushAndRunMultipleServers (total_servers, serversRun) {
110 let apps = []
111 let urls = []
112 let i = 0
113
114 function anotherServerDone (number, app, url) {
115 apps[number - 1] = app
116 urls[number - 1] = url
117 i++
118 if (i === total_servers) {
119 serversRun(apps, urls)
120 }
121 }
122
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)
129 })
130 }, 1000 * j)
131 }
132 })
133 }
134
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
140 }
141
142 // Share the environment
143 const env = Object.create(process.env)
144 env.NODE_ENV = 'test'
145 env.NODE_APP_INSTANCE = number
146 const options = {
147 silent: true,
148 env: env,
149 detached: true
150 }
151
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
159 }
160
161 // If no, there is maybe one thing not already initialized (mongodb...)
162 if (dont_continue === true) return
163
164 app.stdout.removeListener('data', onStdout)
165 callback(app, 'http://localhost:' + port)
166 })
167 }
168
169 function searchVideo (url, search, end) {
170 const path = '/api/v1/videos'
171
172 request(url)
173 .get(path + '/search/' + search)
174 .set('Accept', 'application/json')
175 .expect(200)
176 .expect('Content-Type', /json/)
177 .end(end)
178 }
179
180 function uploadVideo (url, name, description, fixture, end) {
181 const path = '/api/v1/videos'
182
183 request(url)
184 .post(path)
185 .set('Accept', 'application/json')
186 .field('name', name)
187 .field('description', description)
188 .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture))
189 .expect(204)
190 .end(end)
191 }
192
193 // ---------------------------------------------------------------------------
194
195 module.exports = testUtils