]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/utils.js
Use scripty instead of writing shell commands in package.json
[github/Chocobozzz/PeerTube.git] / server / tests / api / utils.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const child_process = require('child_process')
4const exec = child_process.exec
5const fork = child_process.fork
6const pathUtils = require('path')
7const request = require('supertest')
9f10b292 8
f0f5567b 9const testUtils = {
9f10b292
C
10 flushTests: flushTests,
11 getFriendsList: getFriendsList,
2df82d42 12 getVideo: getVideo,
9f10b292 13 getVideosList: getVideosList,
0c1cbbfe
C
14 login: login,
15 loginAndGetAccessToken: loginAndGetAccessToken,
9f10b292
C
16 makeFriends: makeFriends,
17 quitFriends: quitFriends,
18 removeVideo: removeVideo,
19 flushAndRunMultipleServers: flushAndRunMultipleServers,
20 runServer: runServer,
21 searchVideo: searchVideo,
22 uploadVideo: uploadVideo
23}
24
25// ---------------------- Export functions --------------------
26
27function flushTests (callback) {
93534495 28 exec('npm run clean:server:test', callback)
9f10b292
C
29}
30
31function getFriendsList (url, end) {
f0f5567b 32 const path = '/api/v1/pods/'
9f10b292
C
33
34 request(url)
35 .get(path)
36 .set('Accept', 'application/json')
37 .expect(200)
38 .expect('Content-Type', /json/)
39 .end(end)
40}
41
2df82d42
C
42function getVideo (url, id, end) {
43 const path = '/api/v1/videos/' + id
44
45 request(url)
46 .get(path)
47 .set('Accept', 'application/json')
48 .expect(200)
49 .expect('Content-Type', /json/)
50 .end(end)
51}
52
9f10b292 53function getVideosList (url, end) {
f0f5567b 54 const path = '/api/v1/videos'
9f10b292
C
55
56 request(url)
57 .get(path)
58 .set('Accept', 'application/json')
59 .expect(200)
60 .expect('Content-Type', /json/)
61 .end(end)
62}
63
0c1cbbfe
C
64function login (url, client, user, expected_status, end) {
65 if (!end) {
66 end = expected_status
67 expected_status = 200
68 }
69
70 const path = '/api/v1/users/token'
71
72 const body = {
73 client_id: client.id,
74 client_secret: client.secret,
75 username: user.username,
76 password: user.password,
77 response_type: 'code',
78 grant_type: 'password',
79 scope: 'upload'
80 }
81
82 request(url)
83 .post(path)
84 .type('form')
85 .send(body)
86 .expect(expected_status)
87 .end(end)
88}
89
90function loginAndGetAccessToken (server, callback) {
91 login(server.url, server.client, server.user, 200, function (err, res) {
92 if (err) return callback(err)
93
94 return callback(null, res.body.access_token)
95 })
96}
97
9f10b292
C
98function makeFriends (url, expected_status, callback) {
99 if (!callback) {
100 callback = expected_status
101 expected_status = 204
ee66c593
C
102 }
103
f0f5567b 104 const path = '/api/v1/pods/makefriends'
ee66c593 105
9f10b292
C
106 // The first pod make friend with the third
107 request(url)
108 .get(path)
109 .set('Accept', 'application/json')
110 .expect(expected_status)
111 .end(function (err, res) {
112 if (err) throw err
45239549 113
9f10b292
C
114 // Wait for the request between pods
115 setTimeout(callback, 1000)
876d1bcf 116 })
9f10b292 117}
876d1bcf 118
9f10b292 119function quitFriends (url, callback) {
f0f5567b 120 const path = '/api/v1/pods/quitfriends'
876d1bcf 121
9f10b292
C
122 // The first pod make friend with the third
123 request(url)
124 .get(path)
125 .set('Accept', 'application/json')
126 .expect(204)
127 .end(function (err, res) {
128 if (err) throw err
876d1bcf 129
9f10b292
C
130 // Wait for the request between pods
131 setTimeout(callback, 1000)
876d1bcf 132 })
9f10b292
C
133}
134
0c1cbbfe
C
135function removeVideo (url, token, id, expected_status, end) {
136 if (!end) {
137 end = expected_status
138 expected_status = 204
139 }
140
f0f5567b 141 const path = '/api/v1/videos'
9f10b292
C
142
143 request(url)
144 .delete(path + '/' + id)
145 .set('Accept', 'application/json')
0c1cbbfe
C
146 .set('Authorization', 'Bearer ' + token)
147 .expect(expected_status)
9f10b292
C
148 .end(end)
149}
150
151function flushAndRunMultipleServers (total_servers, serversRun) {
f0f5567b
C
152 let apps = []
153 let urls = []
154 let i = 0
9f10b292
C
155
156 function anotherServerDone (number, app, url) {
157 apps[number - 1] = app
158 urls[number - 1] = url
159 i++
160 if (i === total_servers) {
161 serversRun(apps, urls)
162 }
876d1bcf
C
163 }
164
9f10b292 165 flushTests(function () {
f0f5567b
C
166 for (let j = 1; j <= total_servers; j++) {
167 // For the virtual buffer
168 setTimeout(function () {
169 runServer(j, function (app, url) {
170 anotherServerDone(j, app, url)
171 })
172 }, 1000 * j)
9f10b292
C
173 }
174 })
175}
176
177function runServer (number, callback) {
0c1cbbfe
C
178 const server = {
179 app: null,
180 url: `http://localhost:${9000 + number}`,
181 client: {
182 id: null,
183 secret: null
184 },
185 user: {
186 username: null,
187 password: null
188 }
189 }
190
191 // These actions are async so we need to be sure that they have both been done
f0f5567b 192 const server_run_string = {
9f10b292
C
193 'Connected to mongodb': false,
194 'Server listening on port': false
876d1bcf
C
195 }
196
0c1cbbfe
C
197 const regexps = {
198 client_id: 'Client id: ([a-f0-9]+)',
199 client_secret: 'Client secret: (.+)',
200 user_username: 'Username: (.+)',
201 user_password: 'User password: (.+)'
202 }
203
9f10b292 204 // Share the environment
f0f5567b 205 const env = Object.create(process.env)
9f10b292
C
206 env.NODE_ENV = 'test'
207 env.NODE_APP_INSTANCE = number
f0f5567b 208 const options = {
9f10b292
C
209 silent: true,
210 env: env,
211 detached: true
876d1bcf 212 }
c45f7f84 213
0c1cbbfe
C
214 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
215 server.app.stdout.on('data', function onStdout (data) {
f0f5567b 216 let dont_continue = false
0c1cbbfe
C
217
218 // Capture things if we want to
219 for (const key of Object.keys(regexps)) {
220 const regexp = regexps[key]
221 const matches = data.toString().match(regexp)
222 if (matches !== null) {
223 if (key === 'client_id') server.client.id = matches[1]
224 else if (key === 'client_secret') server.client.secret = matches[1]
225 else if (key === 'user_username') server.user.username = matches[1]
226 else if (key === 'user_password') server.user.password = matches[1]
227 }
228 }
229
9f10b292 230 // Check if all required sentences are here
f0f5567b 231 for (const key of Object.keys(server_run_string)) {
9f10b292
C
232 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
233 if (server_run_string[key] === false) dont_continue = true
234 }
c45f7f84 235
9f10b292
C
236 // If no, there is maybe one thing not already initialized (mongodb...)
237 if (dont_continue === true) return
238
0c1cbbfe
C
239 server.app.stdout.removeListener('data', onStdout)
240 callback(server)
9f10b292
C
241 })
242}
243
244function searchVideo (url, search, end) {
f0f5567b 245 const path = '/api/v1/videos'
9f10b292
C
246
247 request(url)
248 .get(path + '/search/' + search)
249 .set('Accept', 'application/json')
250 .expect(200)
251 .expect('Content-Type', /json/)
252 .end(end)
253}
254
0c1cbbfe
C
255function uploadVideo (url, access_token, name, description, fixture, special_status, end) {
256 if (!end) {
257 end = special_status
258 special_status = 204
259 }
260
f0f5567b 261 const path = '/api/v1/videos'
9f10b292
C
262
263 request(url)
264 .post(path)
265 .set('Accept', 'application/json')
0c1cbbfe 266 .set('Authorization', 'Bearer ' + access_token)
9f10b292
C
267 .field('name', name)
268 .field('description', description)
8c9c1942 269 .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture))
0c1cbbfe 270 .expect(special_status)
9f10b292
C
271 .end(end)
272}
273
274// ---------------------------------------------------------------------------
275
276module.exports = testUtils