]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
a13807b5c07a8eec32e1c69901c1f63c74b679e6
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { UserRole } from '../../../../shared/index'
6 import {
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList,
8 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
9 runServer, ServerInfo, serverLogin, updateMyUser, updateUser, uploadVideo
10 } from '../../utils/index'
11 import { follow } from '../../utils/server/follows'
12 import { setAccessTokensToServers } from '../../utils/users/login'
13 import { getMyVideos } from '../../utils/videos/videos'
14
15 const expect = chai.expect
16
17 describe('Test users', function () {
18 let server: ServerInfo
19 let accessToken: string
20 let accessTokenUser: string
21 let videoId: number
22 let userId: number
23
24 before(async function () {
25 this.timeout(10000)
26
27 await flushTests()
28 server = await runServer(1)
29
30 await setAccessTokensToServers([ server ])
31 })
32
33 it('Should create a new client')
34
35 it('Should return the first client')
36
37 it('Should remove the last client')
38
39 it('Should not login with an invalid client id', async function () {
40 const client = { id: 'client', secret: server.client.secret }
41 const res = await login(server.url, client, server.user, 400)
42
43 expect(res.body.error)
44 .to
45 .equal('Authentication failed.')
46 })
47
48 it('Should not login with an invalid client secret', async function () {
49 const client = { id: server.client.id, secret: 'coucou' }
50 const res = await login(server.url, client, server.user, 400)
51
52 expect(res.body.error)
53 .to
54 .equal('Authentication failed.')
55 })
56
57 it('Should not login with an invalid username', async function () {
58 const user = { username: 'captain crochet', password: server.user.password }
59 const res = await login(server.url, server.client, user, 400)
60
61 expect(res.body.error)
62 .to
63 .equal('Authentication failed.')
64 })
65
66 it('Should not login with an invalid password', async function () {
67 const user = { username: server.user.username, password: 'mew_three' }
68 const res = await login(server.url, server.client, user, 400)
69
70 expect(res.body.error)
71 .to
72 .equal('Authentication failed.')
73 })
74
75 it('Should not be able to upload a video', async function () {
76 accessToken = 'my_super_token'
77
78 const videoAttributes = {}
79 await uploadVideo(server.url, accessToken, videoAttributes, 401)
80 })
81
82 it('Should not be able to follow', async function () {
83 accessToken = 'my_super_token'
84 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
85 })
86
87 it('Should not be able to unfollow')
88
89 it('Should be able to login', async function () {
90 const res = await login(server.url, server.client, server.user, 200)
91
92 accessToken = res.body.access_token
93 })
94
95 it('Should upload the video with the correct token', async function () {
96 const videoAttributes = {}
97 await uploadVideo(server.url, accessToken, videoAttributes)
98 const res = await getVideosList(server.url)
99 const video = res.body.data[ 0 ]
100
101 expect(video.accountName)
102 .to
103 .equal('root')
104 videoId = video.id
105 })
106
107 it('Should upload the video again with the correct token', async function () {
108 const videoAttributes = {}
109 await uploadVideo(server.url, accessToken, videoAttributes)
110 })
111
112 it('Should retrieve a video rating', async function () {
113 await rateVideo(server.url, accessToken, videoId, 'like')
114 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
115 const rating = res.body
116
117 expect(rating.videoId)
118 .to
119 .equal(videoId)
120 expect(rating.rating)
121 .to
122 .equal('like')
123 })
124
125 it('Should not be able to remove the video with an incorrect token', async function () {
126 await removeVideo(server.url, 'bad_token', videoId, 401)
127 })
128
129 it('Should not be able to remove the video with the token of another account')
130
131 it('Should be able to remove the video with the correct token', async function () {
132 await removeVideo(server.url, accessToken, videoId)
133 })
134
135 it('Should logout (revoke token)')
136
137 it('Should not be able to get the user information')
138
139 it('Should not be able to upload a video')
140
141 it('Should not be able to remove a video')
142
143 it('Should not be able to rate a video', async function () {
144 const path = '/api/v1/videos/'
145 const data = {
146 rating: 'likes'
147 }
148
149 const options = {
150 url: server.url,
151 path: path + videoId,
152 token: 'wrong token',
153 fields: data,
154 statusCodeExpected: 401
155 }
156 await makePutBodyRequest(options)
157 })
158
159 it('Should be able to login again')
160
161 it('Should have an expired access token')
162
163 it('Should refresh the token')
164
165 it('Should be able to upload a video again')
166
167 it('Should be able to create a new user', async function () {
168 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
169 })
170
171 it('Should be able to login with this user', async function () {
172 server.user = {
173 username: 'user_1',
174 password: 'super password'
175 }
176
177 accessTokenUser = await serverLogin(server)
178 })
179
180 it('Should be able to get the user information', async function () {
181 const res = await getMyUserInformation(server.url, accessTokenUser)
182 const user = res.body
183
184 expect(user.username)
185 .to
186 .equal('user_1')
187 expect(user.email)
188 .to
189 .equal('user_1@example.com')
190 expect(user.displayNSFW).to.be.false
191 expect(user.videoQuota)
192 .to
193 .equal(2 * 1024 * 1024)
194 expect(user.roleLabel)
195 .to
196 .equal('User')
197 expect(user.id)
198 .to
199 .be
200 .a('number')
201 })
202
203 it('Should be able to upload a video with this user', async function () {
204 this.timeout(5000)
205
206 const videoAttributes = {
207 name: 'super user video'
208 }
209 await uploadVideo(server.url, accessTokenUser, videoAttributes)
210 })
211
212 it('Should be able to list my videos', async function () {
213 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
214 expect(res.body.total)
215 .to
216 .equal(1)
217
218 const videos = res.body.data
219 expect(videos)
220 .to
221 .have
222 .lengthOf(1)
223
224 expect(videos[ 0 ].name)
225 .to
226 .equal('super user video')
227 })
228
229 it('Should list all the users', async function () {
230 const res = await getUsersList(server.url, server.accessToken)
231 const result = res.body
232 const total = result.total
233 const users = result.data
234
235 expect(total)
236 .to
237 .equal(2)
238 expect(users)
239 .to
240 .be
241 .an('array')
242 expect(users.length)
243 .to
244 .equal(2)
245
246 const user = users[ 0 ]
247 expect(user.username)
248 .to
249 .equal('user_1')
250 expect(user.email)
251 .to
252 .equal('user_1@example.com')
253 expect(user.displayNSFW).to.be.false
254
255 const rootUser = users[ 1 ]
256 expect(rootUser.username)
257 .to
258 .equal('root')
259 expect(rootUser.email)
260 .to
261 .equal('admin1@example.com')
262 expect(rootUser.displayNSFW).to.be.false
263
264 userId = user.id
265 })
266
267 it('Should list only the first user by username asc', async function () {
268 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
269
270 const result = res.body
271 const total = result.total
272 const users = result.data
273
274 expect(total)
275 .to
276 .equal(2)
277 expect(users.length)
278 .to
279 .equal(1)
280
281 const user = users[ 0 ]
282 expect(user.username)
283 .to
284 .equal('root')
285 expect(user.email)
286 .to
287 .equal('admin1@example.com')
288 expect(user.roleLabel)
289 .to
290 .equal('Administrator')
291 expect(user.displayNSFW).to.be.false
292 })
293
294 it('Should list only the first user by username desc', async function () {
295 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
296 const result = res.body
297 const total = result.total
298 const users = result.data
299
300 expect(total)
301 .to
302 .equal(2)
303 expect(users.length)
304 .to
305 .equal(1)
306
307 const user = users[ 0 ]
308 expect(user.username)
309 .to
310 .equal('user_1')
311 expect(user.email)
312 .to
313 .equal('user_1@example.com')
314 expect(user.displayNSFW).to.be.false
315 })
316
317 it('Should list only the second user by createdAt desc', async function () {
318 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
319 const result = res.body
320 const total = result.total
321 const users = result.data
322
323 expect(total)
324 .to
325 .equal(2)
326 expect(users.length)
327 .to
328 .equal(1)
329
330 const user = users[ 0 ]
331 expect(user.username)
332 .to
333 .equal('user_1')
334 expect(user.email)
335 .to
336 .equal('user_1@example.com')
337 expect(user.displayNSFW).to.be.false
338 })
339
340 it('Should list all the users by createdAt asc', async function () {
341 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
342 const result = res.body
343 const total = result.total
344 const users = result.data
345
346 expect(total)
347 .to
348 .equal(2)
349 expect(users.length)
350 .to
351 .equal(2)
352
353 expect(users[ 0 ].username)
354 .to
355 .equal('root')
356 expect(users[ 0 ].email)
357 .to
358 .equal('admin1@example.com')
359 expect(users[ 0 ].displayNSFW).to.be.false
360
361 expect(users[ 1 ].username)
362 .to
363 .equal('user_1')
364 expect(users[ 1 ].email)
365 .to
366 .equal('user_1@example.com')
367 expect(users[ 1 ].displayNSFW).to.be.false
368 })
369
370 it('Should update my password', async function () {
371 await updateMyUser({
372 url: server.url,
373 accessToken: accessTokenUser,
374 newPassword: 'new password'
375 })
376 server.user.password = 'new password'
377
378 await login(server.url, server.client, server.user, 200)
379 })
380
381 it('Should be able to change the NSFW display attribute', async function () {
382 await updateMyUser({
383 url: server.url,
384 accessToken: accessTokenUser,
385 displayNSFW: true
386 })
387
388 const res = await getMyUserInformation(server.url, accessTokenUser)
389 const user = res.body
390
391 expect(user.username)
392 .to
393 .equal('user_1')
394 expect(user.email)
395 .to
396 .equal('user_1@example.com')
397 expect(user.displayNSFW).to.be.ok
398 expect(user.videoQuota)
399 .to
400 .equal(2 * 1024 * 1024)
401 expect(user.id)
402 .to
403 .be
404 .a('number')
405 })
406
407 it('Should be able to change the autoPlayVideo attribute', async function () {
408 await updateMyUser({
409 url: server.url,
410 accessToken: accessTokenUser,
411 autoPlayVideo: false
412 })
413
414 const res = await getMyUserInformation(server.url, accessTokenUser)
415 const user = res.body
416
417 expect(user.autoPlayVideo).to.be.false
418 })
419
420 it('Should be able to change the email display attribute', async function () {
421 await updateMyUser({
422 url: server.url,
423 accessToken: accessTokenUser,
424 email: 'updated@example.com'
425 })
426
427 const res = await getMyUserInformation(server.url, accessTokenUser)
428 const user = res.body
429
430 expect(user.username)
431 .to
432 .equal('user_1')
433 expect(user.email)
434 .to
435 .equal('updated@example.com')
436 expect(user.displayNSFW).to.be.ok
437 expect(user.videoQuota)
438 .to
439 .equal(2 * 1024 * 1024)
440 expect(user.id)
441 .to
442 .be
443 .a('number')
444 })
445
446 it('Should be able to update another user', async function () {
447 await updateUser({
448 url: server.url,
449 userId,
450 accessToken,
451 email: 'updated2@example.com',
452 videoQuota: 42,
453 role: UserRole.MODERATOR
454 })
455
456 const res = await getUserInformation(server.url, accessToken, userId)
457 const user = res.body
458
459 expect(user.username)
460 .to
461 .equal('user_1')
462 expect(user.email)
463 .to
464 .equal('updated2@example.com')
465 expect(user.displayNSFW).to.be.ok
466 expect(user.videoQuota)
467 .to
468 .equal(42)
469 expect(user.roleLabel)
470 .to
471 .equal('Moderator')
472 expect(user.id)
473 .to
474 .be
475 .a('number')
476 })
477
478 it('Should not be able to delete a user by a moderator', async function () {
479 await removeUser(server.url, 2, accessTokenUser, 403)
480 })
481
482 it('Should be able to list video blacklist by a moderator', async function () {
483 await getBlacklistedVideosList(server.url, accessTokenUser)
484 })
485
486 it('Should be able to remove this user', async function () {
487 await removeUser(server.url, userId, accessToken)
488 })
489
490 it('Should not be able to login with this user', async function () {
491 // server.user is already set to user 1
492 await login(server.url, server.client, server.user, 400)
493 })
494
495 it('Should not have videos of this user', async function () {
496 const res = await getVideosList(server.url)
497
498 expect(res.body.total)
499 .to
500 .equal(1)
501
502 const video = res.body.data[ 0 ]
503 expect(video.accountName)
504 .to
505 .equal('root')
506 })
507
508 it('Should register a new user', async function () {
509 await registerUser(server.url, 'user_15', 'my super password')
510 })
511
512 it('Should be able to login with this registered user', async function () {
513 server.user = {
514 username: 'user_15',
515 password: 'my super password'
516 }
517
518 accessToken = await serverLogin(server)
519 })
520
521 it('Should have the correct video quota', async function () {
522 const res = await getMyUserInformation(server.url, accessToken)
523 const user = res.body
524
525 expect(user.videoQuota)
526 .to
527 .equal(5 * 1024 * 1024)
528 })
529
530 after(async function () {
531 killallServers([ server ])
532
533 // Keep the logs if the test failed
534 if (this[ 'ok' ]) {
535 await flushTests()
536 }
537 })
538 })