]>
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 | flushTests: flushTests, | |
12 | getFriendsList: getFriendsList, | |
13 | getVideo: getVideo, | |
14 | getVideosList: getVideosList, | |
15 | getVideosListPagination: getVideosListPagination, | |
16 | login: login, | |
17 | loginAndGetAccessToken: loginAndGetAccessToken, | |
18 | makeFriends: makeFriends, | |
19 | quitFriends: quitFriends, | |
20 | removeVideo: removeVideo, | |
21 | flushAndRunMultipleServers: flushAndRunMultipleServers, | |
22 | runServer: runServer, | |
23 | searchVideo: searchVideo, | |
24 | searchVideoWithPagination: searchVideoWithPagination, | |
25 | testImage: testImage, | |
26 | uploadVideo: uploadVideo | |
27 | } | |
28 | ||
29 | // ---------------------- Export functions -------------------- | |
30 | ||
31 | function flushTests (callback) { | |
32 | exec('npm run clean:server:test', callback) | |
33 | } | |
34 | ||
35 | function getFriendsList (url, end) { | |
36 | const path = '/api/v1/pods/' | |
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 | ||
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 | ||
57 | function getVideosList (url, end) { | |
58 | const path = '/api/v1/videos' | |
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 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 | ||
81 | function login (url, client, user, expectedStatus, end) { | |
82 | if (!end) { | |
83 | end = expectedStatus | |
84 | expectedStatus = 200 | |
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) | |
103 | .expect(expectedStatus) | |
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 | ||
115 | function makeFriends (url, accessToken, expectedStatus, callback) { | |
116 | if (!callback) { | |
117 | callback = expectedStatus | |
118 | expectedStatus = 204 | |
119 | } | |
120 | ||
121 | const path = '/api/v1/pods/makefriends' | |
122 | ||
123 | // The first pod make friend with the third | |
124 | request(url) | |
125 | .get(path) | |
126 | .set('Accept', 'application/json') | |
127 | .set('Authorization', 'Bearer ' + accessToken) | |
128 | .expect(expectedStatus) | |
129 | .end(function (err, res) { | |
130 | if (err) throw err | |
131 | ||
132 | // Wait for the request between pods | |
133 | setTimeout(callback, 1000) | |
134 | }) | |
135 | } | |
136 | ||
137 | function quitFriends (url, accessToken, expectedStatus, callback) { | |
138 | if (!callback) { | |
139 | callback = expectedStatus | |
140 | expectedStatus = 204 | |
141 | } | |
142 | ||
143 | const path = '/api/v1/pods/quitfriends' | |
144 | ||
145 | // The first pod make friend with the third | |
146 | request(url) | |
147 | .get(path) | |
148 | .set('Accept', 'application/json') | |
149 | .set('Authorization', 'Bearer ' + accessToken) | |
150 | .expect(expectedStatus) | |
151 | .end(function (err, res) { | |
152 | if (err) throw err | |
153 | ||
154 | // Wait for the request between pods | |
155 | setTimeout(callback, 1000) | |
156 | }) | |
157 | } | |
158 | ||
159 | function removeVideo (url, token, id, expectedStatus, end) { | |
160 | if (!end) { | |
161 | end = expectedStatus | |
162 | expectedStatus = 204 | |
163 | } | |
164 | ||
165 | const path = '/api/v1/videos' | |
166 | ||
167 | request(url) | |
168 | .delete(path + '/' + id) | |
169 | .set('Accept', 'application/json') | |
170 | .set('Authorization', 'Bearer ' + token) | |
171 | .expect(expectedStatus) | |
172 | .end(end) | |
173 | } | |
174 | ||
175 | function flushAndRunMultipleServers (totalServers, serversRun) { | |
176 | let apps = [] | |
177 | let urls = [] | |
178 | let i = 0 | |
179 | ||
180 | function anotherServerDone (number, app, url) { | |
181 | apps[number - 1] = app | |
182 | urls[number - 1] = url | |
183 | i++ | |
184 | if (i === totalServers) { | |
185 | serversRun(apps, urls) | |
186 | } | |
187 | } | |
188 | ||
189 | flushTests(function () { | |
190 | for (let j = 1; j <= totalServers; j++) { | |
191 | // For the virtual buffer | |
192 | setTimeout(function () { | |
193 | runServer(j, function (app, url) { | |
194 | anotherServerDone(j, app, url) | |
195 | }) | |
196 | }, 1000 * j) | |
197 | } | |
198 | }) | |
199 | } | |
200 | ||
201 | function runServer (number, callback) { | |
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 | |
216 | const serverRunString = { | |
217 | 'Connected to mongodb': false, | |
218 | 'Server listening on port': false | |
219 | } | |
220 | ||
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 | ||
228 | // Share the environment | |
229 | const env = Object.create(process.env) | |
230 | env.NODE_ENV = 'test' | |
231 | env.NODE_APP_INSTANCE = number | |
232 | const options = { | |
233 | silent: true, | |
234 | env: env, | |
235 | detached: true | |
236 | } | |
237 | ||
238 | server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) | |
239 | server.app.stdout.on('data', function onStdout (data) { | |
240 | let dontContinue = false | |
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 | ||
254 | // Check if all required sentences are here | |
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 | |
258 | } | |
259 | ||
260 | // If no, there is maybe one thing not already initialized (mongodb...) | |
261 | if (dontContinue === true) return | |
262 | ||
263 | server.app.stdout.removeListener('data', onStdout) | |
264 | callback(server) | |
265 | }) | |
266 | } | |
267 | ||
268 | function searchVideo (url, search, end) { | |
269 | const path = '/api/v1/videos' | |
270 | ||
271 | request(url) | |
272 | .get(path + '/search/' + search) | |
273 | .set('Accept', 'application/json') | |
274 | .expect(200) | |
275 | .expect('Content-Type', /json/) | |
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/) | |
289 | .end(end) | |
290 | } | |
291 | ||
292 | function testImage (url, videoName, imagePath, callback) { | |
293 | request(url) | |
294 | .get(imagePath) | |
295 | .expect(200) | |
296 | .end(function (err, res) { | |
297 | if (err) return callback(err) | |
298 | ||
299 | fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) { | |
300 | if (err) return callback(err) | |
301 | ||
302 | callback(null, data.equals(res.body)) | |
303 | }) | |
304 | }) | |
305 | } | |
306 | ||
307 | function uploadVideo (url, accessToken, name, description, fixture, specialStatus, end) { | |
308 | if (!end) { | |
309 | end = specialStatus | |
310 | specialStatus = 204 | |
311 | } | |
312 | ||
313 | const path = '/api/v1/videos' | |
314 | ||
315 | request(url) | |
316 | .post(path) | |
317 | .set('Accept', 'application/json') | |
318 | .set('Authorization', 'Bearer ' + accessToken) | |
319 | .field('name', name) | |
320 | .field('description', description) | |
321 | .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture)) | |
322 | .expect(specialStatus) | |
323 | .end(end) | |
324 | } | |
325 | ||
326 | // --------------------------------------------------------------------------- | |
327 | ||
328 | module.exports = testUtils |