]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const childProcess = require('child_process') | |
4 | const exec = childProcess.exec | |
5 | const fork = childProcess.fork | |
6 | const fs = require('fs') | |
7 | const pathUtils = require('path') | |
8 | const request = require('supertest') | |
9 | ||
10 | const testUtils = { | |
11 | dateIsValid: dateIsValid, | |
12 | flushTests: flushTests, | |
13 | getFriendsList: getFriendsList, | |
14 | getVideo: getVideo, | |
15 | getVideosList: getVideosList, | |
16 | getVideosListPagination: getVideosListPagination, | |
17 | login: login, | |
18 | loginAndGetAccessToken: loginAndGetAccessToken, | |
19 | makeFriends: makeFriends, | |
20 | quitFriends: quitFriends, | |
21 | removeVideo: removeVideo, | |
22 | flushAndRunMultipleServers: flushAndRunMultipleServers, | |
23 | runServer: runServer, | |
24 | searchVideo: searchVideo, | |
25 | searchVideoWithPagination: searchVideoWithPagination, | |
26 | testImage: testImage, | |
27 | uploadVideo: uploadVideo | |
28 | } | |
29 | ||
30 | // ---------------------- Export functions -------------------- | |
31 | ||
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 | ||
42 | function flushTests (callback) { | |
43 | exec('npm run clean:server:test', callback) | |
44 | } | |
45 | ||
46 | function getFriendsList (url, end) { | |
47 | const path = '/api/v1/pods/' | |
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 | ||
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 | ||
68 | function getVideosList (url, end) { | |
69 | const path = '/api/v1/videos' | |
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 | ||
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 | ||
92 | function login (url, client, user, expectedStatus, end) { | |
93 | if (!end) { | |
94 | end = expectedStatus | |
95 | expectedStatus = 200 | |
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) | |
114 | .expect(expectedStatus) | |
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 | ||
126 | function makeFriends (url, accessToken, expectedStatus, callback) { | |
127 | if (!callback) { | |
128 | callback = expectedStatus | |
129 | expectedStatus = 204 | |
130 | } | |
131 | ||
132 | const path = '/api/v1/pods/makefriends' | |
133 | ||
134 | // The first pod make friend with the third | |
135 | request(url) | |
136 | .get(path) | |
137 | .set('Accept', 'application/json') | |
138 | .set('Authorization', 'Bearer ' + accessToken) | |
139 | .expect(expectedStatus) | |
140 | .end(function (err, res) { | |
141 | if (err) throw err | |
142 | ||
143 | // Wait for the request between pods | |
144 | setTimeout(callback, 1000) | |
145 | }) | |
146 | } | |
147 | ||
148 | function quitFriends (url, accessToken, expectedStatus, callback) { | |
149 | if (!callback) { | |
150 | callback = expectedStatus | |
151 | expectedStatus = 204 | |
152 | } | |
153 | ||
154 | const path = '/api/v1/pods/quitfriends' | |
155 | ||
156 | // The first pod make friend with the third | |
157 | request(url) | |
158 | .get(path) | |
159 | .set('Accept', 'application/json') | |
160 | .set('Authorization', 'Bearer ' + accessToken) | |
161 | .expect(expectedStatus) | |
162 | .end(function (err, res) { | |
163 | if (err) throw err | |
164 | ||
165 | // Wait for the request between pods | |
166 | setTimeout(callback, 1000) | |
167 | }) | |
168 | } | |
169 | ||
170 | function removeVideo (url, token, id, expectedStatus, end) { | |
171 | if (!end) { | |
172 | end = expectedStatus | |
173 | expectedStatus = 204 | |
174 | } | |
175 | ||
176 | const path = '/api/v1/videos' | |
177 | ||
178 | request(url) | |
179 | .delete(path + '/' + id) | |
180 | .set('Accept', 'application/json') | |
181 | .set('Authorization', 'Bearer ' + token) | |
182 | .expect(expectedStatus) | |
183 | .end(end) | |
184 | } | |
185 | ||
186 | function flushAndRunMultipleServers (totalServers, serversRun) { | |
187 | let apps = [] | |
188 | let urls = [] | |
189 | let i = 0 | |
190 | ||
191 | function anotherServerDone (number, app, url) { | |
192 | apps[number - 1] = app | |
193 | urls[number - 1] = url | |
194 | i++ | |
195 | if (i === totalServers) { | |
196 | serversRun(apps, urls) | |
197 | } | |
198 | } | |
199 | ||
200 | flushTests(function () { | |
201 | for (let j = 1; j <= totalServers; j++) { | |
202 | // For the virtual buffer | |
203 | setTimeout(function () { | |
204 | runServer(j, function (app, url) { | |
205 | anotherServerDone(j, app, url) | |
206 | }) | |
207 | }, 1000 * j) | |
208 | } | |
209 | }) | |
210 | } | |
211 | ||
212 | function runServer (number, callback) { | |
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 | |
227 | const serverRunString = { | |
228 | 'Connected to mongodb': false, | |
229 | 'Server listening on port': false | |
230 | } | |
231 | ||
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 | ||
239 | // Share the environment | |
240 | const env = Object.create(process.env) | |
241 | env.NODE_ENV = 'test' | |
242 | env.NODE_APP_INSTANCE = number | |
243 | const options = { | |
244 | silent: true, | |
245 | env: env, | |
246 | detached: true | |
247 | } | |
248 | ||
249 | server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) | |
250 | server.app.stdout.on('data', function onStdout (data) { | |
251 | let dontContinue = false | |
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 | ||
265 | // Check if all required sentences are here | |
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 | |
269 | } | |
270 | ||
271 | // If no, there is maybe one thing not already initialized (mongodb...) | |
272 | if (dontContinue === true) return | |
273 | ||
274 | server.app.stdout.removeListener('data', onStdout) | |
275 | callback(server) | |
276 | }) | |
277 | } | |
278 | ||
279 | function searchVideo (url, search, end) { | |
280 | const path = '/api/v1/videos' | |
281 | ||
282 | request(url) | |
283 | .get(path + '/search/' + search) | |
284 | .set('Accept', 'application/json') | |
285 | .expect(200) | |
286 | .expect('Content-Type', /json/) | |
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/) | |
300 | .end(end) | |
301 | } | |
302 | ||
303 | function testImage (url, videoName, imagePath, callback) { | |
304 | request(url) | |
305 | .get(imagePath) | |
306 | .expect(200) | |
307 | .end(function (err, res) { | |
308 | if (err) return callback(err) | |
309 | ||
310 | fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) { | |
311 | if (err) return callback(err) | |
312 | ||
313 | callback(null, data.equals(res.body)) | |
314 | }) | |
315 | }) | |
316 | } | |
317 | ||
318 | function uploadVideo (url, accessToken, name, description, fixture, specialStatus, end) { | |
319 | if (!end) { | |
320 | end = specialStatus | |
321 | specialStatus = 204 | |
322 | } | |
323 | ||
324 | const path = '/api/v1/videos' | |
325 | ||
326 | request(url) | |
327 | .post(path) | |
328 | .set('Accept', 'application/json') | |
329 | .set('Authorization', 'Bearer ' + accessToken) | |
330 | .field('name', name) | |
331 | .field('description', description) | |
332 | .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture)) | |
333 | .expect(specialStatus) | |
334 | .end(end) | |
335 | } | |
336 | ||
337 | // --------------------------------------------------------------------------- | |
338 | ||
339 | module.exports = testUtils |