]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable no-unused-expressions */ | |
2 | ||
3 | 'use strict' | |
4 | ||
5 | const chai = require('chai') | |
6 | const expect = chai.expect | |
7 | const series = require('async/series') | |
8 | ||
9 | const loginUtils = require('../utils/login') | |
10 | const podsUtils = require('../utils/pods') | |
11 | const serversUtils = require('../utils/servers') | |
12 | const usersUtils = require('../utils/users') | |
13 | const requestsUtils = require('../utils/requests') | |
14 | const videosUtils = require('../utils/videos') | |
15 | ||
16 | describe('Test users', function () { | |
17 | let server = null | |
18 | let accessToken = null | |
19 | let accessTokenUser = null | |
20 | let videoId = null | |
21 | let userId = null | |
22 | ||
23 | before(function (done) { | |
24 | this.timeout(20000) | |
25 | ||
26 | series([ | |
27 | function (next) { | |
28 | serversUtils.flushTests(next) | |
29 | }, | |
30 | function (next) { | |
31 | serversUtils.runServer(1, function (server1) { | |
32 | server = server1 | |
33 | next() | |
34 | }) | |
35 | } | |
36 | ], done) | |
37 | }) | |
38 | ||
39 | it('Should create a new client') | |
40 | ||
41 | it('Should return the first client') | |
42 | ||
43 | it('Should remove the last client') | |
44 | ||
45 | it('Should not login with an invalid client id', function (done) { | |
46 | const client = { id: 'client', password: server.client.secret } | |
47 | loginUtils.login(server.url, client, server.user, 400, function (err, res) { | |
48 | if (err) throw err | |
49 | ||
50 | expect(res.body.error).to.equal('invalid_client') | |
51 | done() | |
52 | }) | |
53 | }) | |
54 | ||
55 | it('Should not login with an invalid client password', function (done) { | |
56 | const client = { id: server.client.id, password: 'coucou' } | |
57 | loginUtils.login(server.url, client, server.user, 400, function (err, res) { | |
58 | if (err) throw err | |
59 | ||
60 | expect(res.body.error).to.equal('invalid_client') | |
61 | done() | |
62 | }) | |
63 | }) | |
64 | ||
65 | it('Should not login with an invalid username', function (done) { | |
66 | const user = { username: 'captain crochet', password: server.user.password } | |
67 | loginUtils.login(server.url, server.client, user, 400, function (err, res) { | |
68 | if (err) throw err | |
69 | ||
70 | expect(res.body.error).to.equal('invalid_grant') | |
71 | done() | |
72 | }) | |
73 | }) | |
74 | ||
75 | it('Should not login with an invalid password', function (done) { | |
76 | const user = { username: server.user.username, password: 'mewthree' } | |
77 | loginUtils.login(server.url, server.client, user, 400, function (err, res) { | |
78 | if (err) throw err | |
79 | ||
80 | expect(res.body.error).to.equal('invalid_grant') | |
81 | done() | |
82 | }) | |
83 | }) | |
84 | ||
85 | it('Should not be able to upload a video', function (done) { | |
86 | accessToken = 'mysupertoken' | |
87 | ||
88 | const videoAttributes = {} | |
89 | videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 401, done) | |
90 | }) | |
91 | ||
92 | it('Should not be able to make friends', function (done) { | |
93 | accessToken = 'mysupertoken' | |
94 | podsUtils.makeFriends(server.url, accessToken, 401, done) | |
95 | }) | |
96 | ||
97 | it('Should not be able to quit friends', function (done) { | |
98 | accessToken = 'mysupertoken' | |
99 | podsUtils.quitFriends(server.url, accessToken, 401, done) | |
100 | }) | |
101 | ||
102 | it('Should be able to login', function (done) { | |
103 | loginUtils.login(server.url, server.client, server.user, 200, function (err, res) { | |
104 | if (err) throw err | |
105 | ||
106 | accessToken = res.body.access_token | |
107 | done() | |
108 | }) | |
109 | }) | |
110 | ||
111 | it('Should upload the video with the correct token', function (done) { | |
112 | const videoAttributes = {} | |
113 | videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, function (err, res) { | |
114 | if (err) throw err | |
115 | ||
116 | videosUtils.getVideosList(server.url, function (err, res) { | |
117 | if (err) throw err | |
118 | ||
119 | const video = res.body.data[0] | |
120 | expect(video.author).to.equal('root') | |
121 | ||
122 | videoId = video.id | |
123 | done() | |
124 | }) | |
125 | }) | |
126 | }) | |
127 | ||
128 | it('Should upload the video again with the correct token', function (done) { | |
129 | const videoAttributes = {} | |
130 | videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, done) | |
131 | }) | |
132 | ||
133 | it('Should retrieve a video rating', function (done) { | |
134 | videosUtils.rateVideo(server.url, accessToken, videoId, 'like', function (err) { | |
135 | if (err) throw err | |
136 | ||
137 | usersUtils.getUserVideoRating(server.url, accessToken, videoId, function (err, res) { | |
138 | if (err) throw err | |
139 | ||
140 | const rating = res.body | |
141 | ||
142 | expect(rating.videoId).to.equal(videoId) | |
143 | expect(rating.rating).to.equal('like') | |
144 | ||
145 | done() | |
146 | }) | |
147 | }) | |
148 | }) | |
149 | ||
150 | it('Should not be able to remove the video with an incorrect token', function (done) { | |
151 | videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done) | |
152 | }) | |
153 | ||
154 | it('Should not be able to remove the video with the token of another account') | |
155 | ||
156 | it('Should be able to remove the video with the correct token', function (done) { | |
157 | videosUtils.removeVideo(server.url, accessToken, videoId, done) | |
158 | }) | |
159 | ||
160 | it('Should logout (revoke token)') | |
161 | ||
162 | it('Should not be able to get the user informations') | |
163 | ||
164 | it('Should not be able to upload a video') | |
165 | ||
166 | it('Should not be able to remove a video') | |
167 | ||
168 | it('Should not be able to rate a video', function (done) { | |
169 | const path = '/api/v1/videos/' | |
170 | const data = { | |
171 | rating: 'likes' | |
172 | } | |
173 | ||
174 | requestsUtils.makePutBodyRequest(server.url, path + videoId, 'wrong token', data, done, 401) | |
175 | }) | |
176 | ||
177 | it('Should be able to login again') | |
178 | ||
179 | it('Should have an expired access token') | |
180 | ||
181 | it('Should refresh the token') | |
182 | ||
183 | it('Should be able to upload a video again') | |
184 | ||
185 | it('Should be able to create a new user', function (done) { | |
186 | usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done) | |
187 | }) | |
188 | ||
189 | it('Should be able to login with this user', function (done) { | |
190 | server.user = { | |
191 | username: 'user_1', | |
192 | password: 'super password' | |
193 | } | |
194 | ||
195 | loginUtils.loginAndGetAccessToken(server, function (err, token) { | |
196 | if (err) throw err | |
197 | ||
198 | accessTokenUser = token | |
199 | ||
200 | done() | |
201 | }) | |
202 | }) | |
203 | ||
204 | it('Should be able to get the user informations', function (done) { | |
205 | usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) { | |
206 | if (err) throw err | |
207 | ||
208 | const user = res.body | |
209 | ||
210 | expect(user.username).to.equal('user_1') | |
211 | expect(user.email).to.equal('user_1@example.com') | |
212 | expect(user.displayNSFW).to.be.falsy | |
213 | expect(user.id).to.exist | |
214 | ||
215 | done() | |
216 | }) | |
217 | }) | |
218 | ||
219 | it('Should be able to upload a video with this user', function (done) { | |
220 | this.timeout(5000) | |
221 | ||
222 | const videoAttributes = {} | |
223 | videosUtils.uploadVideo(server.url, accessTokenUser, videoAttributes, done) | |
224 | }) | |
225 | ||
226 | it('Should list all the users', function (done) { | |
227 | usersUtils.getUsersList(server.url, function (err, res) { | |
228 | if (err) throw err | |
229 | ||
230 | const result = res.body | |
231 | const total = result.total | |
232 | const users = result.data | |
233 | ||
234 | expect(total).to.equal(2) | |
235 | expect(users).to.be.an('array') | |
236 | expect(users.length).to.equal(2) | |
237 | ||
238 | const user = users[0] | |
239 | expect(user.username).to.equal('user_1') | |
240 | expect(user.email).to.equal('user_1@example.com') | |
241 | expect(user.displayNSFW).to.be.falsy | |
242 | ||
243 | const rootUser = users[1] | |
244 | expect(rootUser.username).to.equal('root') | |
245 | expect(rootUser.email).to.equal('admin1@example.com') | |
246 | expect(rootUser.displayNSFW).to.be.falsy | |
247 | ||
248 | userId = user.id | |
249 | ||
250 | done() | |
251 | }) | |
252 | }) | |
253 | ||
254 | it('Should list only the first user by username asc', function (done) { | |
255 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, 'username', function (err, res) { | |
256 | if (err) throw err | |
257 | ||
258 | const result = res.body | |
259 | const total = result.total | |
260 | const users = result.data | |
261 | ||
262 | expect(total).to.equal(2) | |
263 | expect(users.length).to.equal(1) | |
264 | ||
265 | const user = users[0] | |
266 | expect(user.username).to.equal('root') | |
267 | expect(user.email).to.equal('admin1@example.com') | |
268 | expect(user.displayNSFW).to.be.falsy | |
269 | ||
270 | done() | |
271 | }) | |
272 | }) | |
273 | ||
274 | it('Should list only the first user by username desc', function (done) { | |
275 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-username', function (err, res) { | |
276 | if (err) throw err | |
277 | ||
278 | const result = res.body | |
279 | const total = result.total | |
280 | const users = result.data | |
281 | ||
282 | expect(total).to.equal(2) | |
283 | expect(users.length).to.equal(1) | |
284 | ||
285 | const user = users[0] | |
286 | expect(user.username).to.equal('user_1') | |
287 | expect(user.email).to.equal('user_1@example.com') | |
288 | expect(user.displayNSFW).to.be.falsy | |
289 | ||
290 | done() | |
291 | }) | |
292 | }) | |
293 | ||
294 | it('Should list only the second user by createdAt desc', function (done) { | |
295 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt', function (err, res) { | |
296 | if (err) throw err | |
297 | ||
298 | const result = res.body | |
299 | const total = result.total | |
300 | const users = result.data | |
301 | ||
302 | expect(total).to.equal(2) | |
303 | expect(users.length).to.equal(1) | |
304 | ||
305 | const user = users[0] | |
306 | expect(user.username).to.equal('user_1') | |
307 | expect(user.email).to.equal('user_1@example.com') | |
308 | expect(user.displayNSFW).to.be.falsy | |
309 | ||
310 | done() | |
311 | }) | |
312 | }) | |
313 | ||
314 | it('Should list all the users by createdAt asc', function (done) { | |
315 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt', function (err, res) { | |
316 | if (err) throw err | |
317 | ||
318 | const result = res.body | |
319 | const total = result.total | |
320 | const users = result.data | |
321 | ||
322 | expect(total).to.equal(2) | |
323 | expect(users.length).to.equal(2) | |
324 | ||
325 | expect(users[0].username).to.equal('root') | |
326 | expect(users[0].email).to.equal('admin1@example.com') | |
327 | expect(users[0].displayNSFW).to.be.falsy | |
328 | ||
329 | expect(users[1].username).to.equal('user_1') | |
330 | expect(users[1].email).to.equal('user_1@example.com') | |
331 | expect(users[1].displayNSFW).to.be.falsy | |
332 | ||
333 | done() | |
334 | }) | |
335 | }) | |
336 | ||
337 | it('Should update the user password', function (done) { | |
338 | usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', null, function (err, res) { | |
339 | if (err) throw err | |
340 | ||
341 | server.user.password = 'new password' | |
342 | loginUtils.login(server.url, server.client, server.user, 200, done) | |
343 | }) | |
344 | }) | |
345 | ||
346 | it('Should be able to change the NSFW display attribute', function (done) { | |
347 | usersUtils.updateUser(server.url, userId, accessTokenUser, null, true, function (err, res) { | |
348 | if (err) throw err | |
349 | ||
350 | usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) { | |
351 | if (err) throw err | |
352 | ||
353 | const user = res.body | |
354 | ||
355 | expect(user.username).to.equal('user_1') | |
356 | expect(user.email).to.equal('user_1@example.com') | |
357 | expect(user.displayNSFW).to.be.truthy | |
358 | expect(user.id).to.exist | |
359 | ||
360 | done() | |
361 | }) | |
362 | }) | |
363 | }) | |
364 | ||
365 | it('Should be able to remove this user', function (done) { | |
366 | usersUtils.removeUser(server.url, userId, accessToken, done) | |
367 | }) | |
368 | ||
369 | it('Should not be able to login with this user', function (done) { | |
370 | // server.user is already set to user 1 | |
371 | loginUtils.login(server.url, server.client, server.user, 400, done) | |
372 | }) | |
373 | ||
374 | it('Should not have videos of this user', function (done) { | |
375 | videosUtils.getVideosList(server.url, function (err, res) { | |
376 | if (err) throw err | |
377 | ||
378 | expect(res.body.total).to.equal(1) | |
379 | const video = res.body.data[0] | |
380 | expect(video.author).to.equal('root') | |
381 | ||
382 | done() | |
383 | }) | |
384 | }) | |
385 | ||
386 | it('Should register a new user', function (done) { | |
387 | usersUtils.registerUser(server.url, 'user_15', 'my super password', done) | |
388 | }) | |
389 | ||
390 | it('Should be able to login with this registered user', function (done) { | |
391 | server.user = { | |
392 | username: 'user_15', | |
393 | password: 'my super password' | |
394 | } | |
395 | ||
396 | loginUtils.loginAndGetAccessToken(server, done) | |
397 | }) | |
398 | ||
399 | after(function (done) { | |
400 | process.kill(-server.app.pid) | |
401 | ||
402 | // Keep the logs if the test failed | |
403 | if (this.ok) { | |
404 | serversUtils.flushTests(done) | |
405 | } else { | |
406 | done() | |
407 | } | |
408 | }) | |
409 | }) |