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