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