]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
ca06942e73c0caa6ecd407f4bae8b7993e58bd53
[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 { User, UserRole, Video } from '../../../../shared/index'
6 import {
7 blockUser,
8 cleanupTests,
9 createUser,
10 deleteMe,
11 flushAndRunServer,
12 getAccountRatings,
13 getBlacklistedVideosList,
14 getMyUserInformation,
15 getMyUserVideoQuotaUsed,
16 getMyUserVideoRating,
17 getUserInformation,
18 getUsersList,
19 getUsersListPaginationAndSort,
20 getVideoChannel,
21 getVideosList, installPlugin,
22 login,
23 makePutBodyRequest,
24 rateVideo,
25 registerUserWithChannel,
26 removeUser,
27 removeVideo,
28 ServerInfo,
29 testImage,
30 unblockUser,
31 updateMyAvatar,
32 updateMyUser,
33 updateUser,
34 uploadVideo,
35 userLogin
36 } from '../../../../shared/extra-utils'
37 import { follow } from '../../../../shared/extra-utils/server/follows'
38 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
39 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
40 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
41
42 const expect = chai.expect
43
44 describe('Test users', function () {
45 let server: ServerInfo
46 let accessToken: string
47 let accessTokenUser: string
48 let videoId: number
49 let userId: number
50 const user = {
51 username: 'user_1',
52 password: 'super password'
53 }
54
55 before(async function () {
56 this.timeout(30000)
57 server = await flushAndRunServer(1)
58
59 await setAccessTokensToServers([ server ])
60
61 await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' })
62 })
63
64 describe('OAuth client', function () {
65 it('Should create a new client')
66
67 it('Should return the first client')
68
69 it('Should remove the last client')
70
71 it('Should not login with an invalid client id', async function () {
72 const client = { id: 'client', secret: server.client.secret }
73 const res = await login(server.url, client, server.user, 400)
74
75 expect(res.body.error).to.contain('client is invalid')
76 })
77
78 it('Should not login with an invalid client secret', async function () {
79 const client = { id: server.client.id, secret: 'coucou' }
80 const res = await login(server.url, client, server.user, 400)
81
82 expect(res.body.error).to.contain('client is invalid')
83 })
84 })
85
86 describe('Login', function () {
87
88 it('Should not login with an invalid username', async function () {
89 const user = { username: 'captain crochet', password: server.user.password }
90 const res = await login(server.url, server.client, user, 400)
91
92 expect(res.body.error).to.contain('credentials are invalid')
93 })
94
95 it('Should not login with an invalid password', async function () {
96 const user = { username: server.user.username, password: 'mew_three' }
97 const res = await login(server.url, server.client, user, 400)
98
99 expect(res.body.error).to.contain('credentials are invalid')
100 })
101
102 it('Should not be able to upload a video', async function () {
103 accessToken = 'my_super_token'
104
105 const videoAttributes = {}
106 await uploadVideo(server.url, accessToken, videoAttributes, 401)
107 })
108
109 it('Should not be able to follow', async function () {
110 accessToken = 'my_super_token'
111 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
112 })
113
114 it('Should not be able to unfollow')
115
116 it('Should be able to login', async function () {
117 const res = await login(server.url, server.client, server.user, 200)
118
119 accessToken = res.body.access_token
120 })
121
122 it('Should be able to login with an insensitive username', async function () {
123 const user = { username: 'RoOt', password: server.user.password }
124 const res = await login(server.url, server.client, user, 200)
125
126 const user2 = { username: 'rOoT', password: server.user.password }
127 const res2 = await login(server.url, server.client, user2, 200)
128
129 const user3 = { username: 'ROOt', password: server.user.password }
130 const res3 = await login(server.url, server.client, user3, 200)
131 })
132 })
133
134 describe('Upload', function () {
135
136 it('Should upload the video with the correct token', async function () {
137 const videoAttributes = {}
138 await uploadVideo(server.url, accessToken, videoAttributes)
139 const res = await getVideosList(server.url)
140 const video = res.body.data[ 0 ]
141
142 expect(video.account.name).to.equal('root')
143 videoId = video.id
144 })
145
146 it('Should upload the video again with the correct token', async function () {
147 const videoAttributes = {}
148 await uploadVideo(server.url, accessToken, videoAttributes)
149 })
150 })
151
152 describe('Ratings', function () {
153
154 it('Should retrieve a video rating', async function () {
155 await rateVideo(server.url, accessToken, videoId, 'like')
156 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
157 const rating = res.body
158
159 expect(rating.videoId).to.equal(videoId)
160 expect(rating.rating).to.equal('like')
161 })
162
163 it('Should retrieve ratings list', async function () {
164 await rateVideo(server.url, accessToken, videoId, 'like')
165
166 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
167 const ratings = res.body
168
169 expect(ratings.total).to.equal(1)
170 expect(ratings.data[ 0 ].video.id).to.equal(videoId)
171 expect(ratings.data[ 0 ].rating).to.equal('like')
172 })
173
174 it('Should retrieve ratings list by rating type', async function () {
175 {
176 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
177 const ratings = res.body
178 expect(ratings.data.length).to.equal(1)
179 }
180
181 {
182 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
183 const ratings = res.body
184 expect(ratings.data.length).to.equal(0)
185 }
186 })
187 })
188
189 describe('Remove video', function () {
190 it('Should not be able to remove the video with an incorrect token', async function () {
191 await removeVideo(server.url, 'bad_token', videoId, 401)
192 })
193
194 it('Should not be able to remove the video with the token of another account')
195
196 it('Should be able to remove the video with the correct token', async function () {
197 await removeVideo(server.url, accessToken, videoId)
198 })
199 })
200
201 describe('Logout', function () {
202 it('Should logout (revoke token)')
203
204 it('Should not be able to get the user information')
205
206 it('Should not be able to upload a video')
207
208 it('Should not be able to remove a video')
209
210 it('Should not be able to rate a video', async function () {
211 const path = '/api/v1/videos/'
212 const data = {
213 rating: 'likes'
214 }
215
216 const options = {
217 url: server.url,
218 path: path + videoId,
219 token: 'wrong token',
220 fields: data,
221 statusCodeExpected: 401
222 }
223 await makePutBodyRequest(options)
224 })
225
226 it('Should be able to login again')
227
228 it('Should have an expired access token')
229
230 it('Should refresh the token')
231
232 it('Should be able to upload a video again')
233 })
234
235 describe('Creating a user', function () {
236
237 it('Should be able to create a new user', async function () {
238 await createUser({
239 url: server.url,
240 accessToken: accessToken,
241 username: user.username,
242 password: user.password,
243 videoQuota: 2 * 1024 * 1024,
244 adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
245 })
246 })
247
248 it('Should be able to login with this user', async function () {
249 accessTokenUser = await userLogin(server, user)
250 })
251
252 it('Should be able to get user information', async function () {
253 const res1 = await getMyUserInformation(server.url, accessTokenUser)
254 const userMe: User = res1.body
255
256 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id)
257 const userGet: User = res2.body
258
259 for (const user of [ userMe, userGet ]) {
260 expect(user.username).to.equal('user_1')
261 expect(user.email).to.equal('user_1@example.com')
262 expect(user.nsfwPolicy).to.equal('display')
263 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
264 expect(user.roleLabel).to.equal('User')
265 expect(user.id).to.be.a('number')
266 expect(user.account.displayName).to.equal('user_1')
267 expect(user.account.description).to.be.null
268 }
269
270 expect(userMe.adminFlags).to.be.undefined
271 expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)
272 })
273 })
274
275 describe('My videos & quotas', function () {
276
277 it('Should be able to upload a video with this user', async function () {
278 this.timeout(5000)
279
280 const videoAttributes = {
281 name: 'super user video',
282 fixture: 'video_short.webm'
283 }
284 await uploadVideo(server.url, accessTokenUser, videoAttributes)
285 })
286
287 it('Should have video quota updated', async function () {
288 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
289 const data = res.body
290
291 expect(data.videoQuotaUsed).to.equal(218910)
292
293 const resUsers = await getUsersList(server.url, server.accessToken)
294
295 const users: User[] = resUsers.body.data
296 const tmpUser = users.find(u => u.username === user.username)
297 expect(tmpUser.videoQuotaUsed).to.equal(218910)
298 })
299
300 it('Should be able to list my videos', async function () {
301 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
302 expect(res.body.total).to.equal(1)
303
304 const videos = res.body.data
305 expect(videos).to.have.lengthOf(1)
306
307 const video: Video = videos[ 0 ]
308 expect(video.name).to.equal('super user video')
309 expect(video.thumbnailPath).to.not.be.null
310 expect(video.previewPath).to.not.be.null
311 })
312 })
313
314 describe('Users listing', function () {
315
316 it('Should list all the users', async function () {
317 const res = await getUsersList(server.url, server.accessToken)
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).to.be.an('array')
324 expect(users.length).to.equal(2)
325
326 const user = users[ 0 ]
327 expect(user.username).to.equal('user_1')
328 expect(user.email).to.equal('user_1@example.com')
329 expect(user.nsfwPolicy).to.equal('display')
330
331 const rootUser = users[ 1 ]
332 expect(rootUser.username).to.equal('root')
333 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
334 expect(user.nsfwPolicy).to.equal('display')
335
336 userId = user.id
337 })
338
339 it('Should list only the first user by username asc', async function () {
340 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
341
342 const result = res.body
343 const total = result.total
344 const users = result.data
345
346 expect(total).to.equal(2)
347 expect(users.length).to.equal(1)
348
349 const user = users[ 0 ]
350 expect(user.username).to.equal('root')
351 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
352 expect(user.roleLabel).to.equal('Administrator')
353 expect(user.nsfwPolicy).to.equal('display')
354 })
355
356 it('Should list only the first user by username desc', async function () {
357 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
358 const result = res.body
359 const total = result.total
360 const users = result.data
361
362 expect(total).to.equal(2)
363 expect(users.length).to.equal(1)
364
365 const user = users[ 0 ]
366 expect(user.username).to.equal('user_1')
367 expect(user.email).to.equal('user_1@example.com')
368 expect(user.nsfwPolicy).to.equal('display')
369 })
370
371 it('Should list only the second user by createdAt desc', async function () {
372 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
373 const result = res.body
374 const total = result.total
375 const users = result.data
376
377 expect(total).to.equal(2)
378 expect(users.length).to.equal(1)
379
380 const user = users[ 0 ]
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('user_1@example.com')
383 expect(user.nsfwPolicy).to.equal('display')
384 })
385
386 it('Should list all the users by createdAt asc', async function () {
387 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
388 const result = res.body
389 const total = result.total
390 const users = result.data
391
392 expect(total).to.equal(2)
393 expect(users.length).to.equal(2)
394
395 expect(users[ 0 ].username).to.equal('root')
396 expect(users[ 0 ].email).to.equal('admin' + server.internalServerNumber + '@example.com')
397 expect(users[ 0 ].nsfwPolicy).to.equal('display')
398
399 expect(users[ 1 ].username).to.equal('user_1')
400 expect(users[ 1 ].email).to.equal('user_1@example.com')
401 expect(users[ 1 ].nsfwPolicy).to.equal('display')
402 })
403
404 it('Should search user by username', async function () {
405 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
406 const users = res.body.data as User[]
407
408 expect(res.body.total).to.equal(1)
409 expect(users.length).to.equal(1)
410
411 expect(users[ 0 ].username).to.equal('root')
412 })
413
414 it('Should search user by email', async function () {
415 {
416 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
417 const users = res.body.data as User[]
418
419 expect(res.body.total).to.equal(1)
420 expect(users.length).to.equal(1)
421
422 expect(users[ 0 ].username).to.equal('user_1')
423 expect(users[ 0 ].email).to.equal('user_1@example.com')
424 }
425
426 {
427 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
428 const users = res.body.data as User[]
429
430 expect(res.body.total).to.equal(2)
431 expect(users.length).to.equal(2)
432
433 expect(users[ 0 ].username).to.equal('root')
434 expect(users[ 1 ].username).to.equal('user_1')
435 }
436 })
437 })
438
439 describe('Update my account', function () {
440 it('Should update my password', async function () {
441 await updateMyUser({
442 url: server.url,
443 accessToken: accessTokenUser,
444 currentPassword: 'super password',
445 password: 'new password'
446 })
447 user.password = 'new password'
448
449 await userLogin(server, user, 200)
450 })
451
452 it('Should be able to change the NSFW display attribute', async function () {
453 await updateMyUser({
454 url: server.url,
455 accessToken: accessTokenUser,
456 nsfwPolicy: 'do_not_list'
457 })
458
459 const res = await getMyUserInformation(server.url, accessTokenUser)
460 const user = res.body
461
462 expect(user.username).to.equal('user_1')
463 expect(user.email).to.equal('user_1@example.com')
464 expect(user.nsfwPolicy).to.equal('do_not_list')
465 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
466 expect(user.id).to.be.a('number')
467 expect(user.account.displayName).to.equal('user_1')
468 expect(user.account.description).to.be.null
469 })
470
471 it('Should be able to change the autoPlayVideo attribute', async function () {
472 await updateMyUser({
473 url: server.url,
474 accessToken: accessTokenUser,
475 autoPlayVideo: false
476 })
477
478 const res = await getMyUserInformation(server.url, accessTokenUser)
479 const user = res.body
480
481 expect(user.autoPlayVideo).to.be.false
482 })
483
484 it('Should be able to change the autoPlayNextVideo attribute', async function () {
485 await updateMyUser({
486 url: server.url,
487 accessToken: accessTokenUser,
488 autoPlayNextVideo: true
489 })
490
491 const res = await getMyUserInformation(server.url, accessTokenUser)
492 const user = res.body
493
494 expect(user.autoPlayNextVideo).to.be.true
495 })
496
497 it('Should be able to change the email attribute', async function () {
498 await updateMyUser({
499 url: server.url,
500 accessToken: accessTokenUser,
501 currentPassword: 'new password',
502 email: 'updated@example.com'
503 })
504
505 const res = await getMyUserInformation(server.url, accessTokenUser)
506 const user = res.body
507
508 expect(user.username).to.equal('user_1')
509 expect(user.email).to.equal('updated@example.com')
510 expect(user.nsfwPolicy).to.equal('do_not_list')
511 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
512 expect(user.id).to.be.a('number')
513 expect(user.account.displayName).to.equal('user_1')
514 expect(user.account.description).to.be.null
515 })
516
517 it('Should be able to update my avatar', async function () {
518 const fixture = 'avatar.png'
519
520 await updateMyAvatar({
521 url: server.url,
522 accessToken: accessTokenUser,
523 fixture
524 })
525
526 const res = await getMyUserInformation(server.url, accessTokenUser)
527 const user = res.body
528
529 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
530 })
531
532 it('Should be able to update my display name', async function () {
533 await updateMyUser({
534 url: server.url,
535 accessToken: accessTokenUser,
536 displayName: 'new display name'
537 })
538
539 const res = await getMyUserInformation(server.url, accessTokenUser)
540 const user = res.body
541
542 expect(user.username).to.equal('user_1')
543 expect(user.email).to.equal('updated@example.com')
544 expect(user.nsfwPolicy).to.equal('do_not_list')
545 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
546 expect(user.id).to.be.a('number')
547 expect(user.account.displayName).to.equal('new display name')
548 expect(user.account.description).to.be.null
549 })
550
551 it('Should be able to update my description', async function () {
552 await updateMyUser({
553 url: server.url,
554 accessToken: accessTokenUser,
555 description: 'my super description updated'
556 })
557
558 const res = await getMyUserInformation(server.url, accessTokenUser)
559 const user: User = res.body
560
561 expect(user.username).to.equal('user_1')
562 expect(user.email).to.equal('updated@example.com')
563 expect(user.nsfwPolicy).to.equal('do_not_list')
564 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
565 expect(user.id).to.be.a('number')
566 expect(user.account.displayName).to.equal('new display name')
567 expect(user.account.description).to.equal('my super description updated')
568 expect(user.noWelcomeModal).to.be.false
569 expect(user.noInstanceConfigWarningModal).to.be.false
570 })
571
572 it('Should be able to update my theme', async function () {
573 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
574 await updateMyUser({
575 url: server.url,
576 accessToken: accessTokenUser,
577 theme
578 })
579
580 const res = await getMyUserInformation(server.url, accessTokenUser)
581 const body: User = res.body
582
583 expect(body.theme).to.equal(theme)
584 }
585 })
586
587 it('Should be able to update my modal preferences', async function () {
588 await updateMyUser({
589 url: server.url,
590 accessToken: accessTokenUser,
591 noInstanceConfigWarningModal: true,
592 noWelcomeModal: true
593 })
594
595 const res = await getMyUserInformation(server.url, accessTokenUser)
596 const user: User = res.body
597
598 expect(user.noWelcomeModal).to.be.true
599 expect(user.noInstanceConfigWarningModal).to.be.true
600 })
601 })
602
603 describe('Updating another user', function () {
604
605 it('Should be able to update another user', async function () {
606 await updateUser({
607 url: server.url,
608 userId,
609 accessToken,
610 email: 'updated2@example.com',
611 emailVerified: true,
612 videoQuota: 42,
613 role: UserRole.MODERATOR,
614 adminFlags: UserAdminFlag.NONE
615 })
616
617 const res = await getUserInformation(server.url, accessToken, userId)
618 const user = res.body
619
620 expect(user.username).to.equal('user_1')
621 expect(user.email).to.equal('updated2@example.com')
622 expect(user.emailVerified).to.be.true
623 expect(user.nsfwPolicy).to.equal('do_not_list')
624 expect(user.videoQuota).to.equal(42)
625 expect(user.roleLabel).to.equal('Moderator')
626 expect(user.id).to.be.a('number')
627 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
628 })
629
630 it('Should have removed the user token', async function () {
631 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
632
633 accessTokenUser = await userLogin(server, user)
634 })
635
636 it('Should be able to update another user password', async function () {
637 await updateUser({
638 url: server.url,
639 userId,
640 accessToken,
641 password: 'password updated'
642 })
643
644 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
645
646 await userLogin(server, user, 400)
647
648 user.password = 'password updated'
649 accessTokenUser = await userLogin(server, user)
650 })
651 })
652
653 describe('Video blacklists', function () {
654 it('Should be able to list video blacklist by a moderator', async function () {
655 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
656 })
657 })
658
659 describe('Remove a user', function () {
660 it('Should be able to remove this user', async function () {
661 await removeUser(server.url, userId, accessToken)
662 })
663
664 it('Should not be able to login with this user', async function () {
665 await userLogin(server, user, 400)
666 })
667
668 it('Should not have videos of this user', async function () {
669 const res = await getVideosList(server.url)
670
671 expect(res.body.total).to.equal(1)
672
673 const video = res.body.data[ 0 ]
674 expect(video.account.name).to.equal('root')
675 })
676 })
677
678 describe('Registering a new user', function () {
679 it('Should register a new user', async function () {
680 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
681 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
682
683 await registerUserWithChannel({ url: server.url, user, channel })
684 })
685
686 it('Should be able to login with this registered user', async function () {
687 const user15 = {
688 username: 'user_15',
689 password: 'my super password'
690 }
691
692 accessToken = await userLogin(server, user15)
693 })
694
695 it('Should have the correct display name', async function () {
696 const res = await getMyUserInformation(server.url, accessToken)
697 const user: User = res.body
698
699 expect(user.account.displayName).to.equal('super user 15')
700 })
701
702 it('Should have the correct video quota', async function () {
703 const res = await getMyUserInformation(server.url, accessToken)
704 const user = res.body
705
706 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
707 })
708
709 it('Should have created the channel', async function () {
710 const res = await getVideoChannel(server.url, 'my_user_15_channel')
711
712 expect(res.body.displayName).to.equal('my channel rocks')
713 })
714
715 it('Should remove me', async function () {
716 {
717 const res = await getUsersList(server.url, server.accessToken)
718 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
719 }
720
721 await deleteMe(server.url, accessToken)
722
723 {
724 const res = await getUsersList(server.url, server.accessToken)
725 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
726 }
727 })
728 })
729
730 describe('User blocking', function () {
731 it('Should block and unblock a user', async function () {
732 const user16 = {
733 username: 'user_16',
734 password: 'my super password'
735 }
736 const resUser = await createUser({
737 url: server.url,
738 accessToken: server.accessToken,
739 username: user16.username,
740 password: user16.password
741 })
742 const user16Id = resUser.body.user.id
743
744 accessToken = await userLogin(server, user16)
745
746 await getMyUserInformation(server.url, accessToken, 200)
747 await blockUser(server.url, user16Id, server.accessToken)
748
749 await getMyUserInformation(server.url, accessToken, 401)
750 await userLogin(server, user16, 400)
751
752 await unblockUser(server.url, user16Id, server.accessToken)
753 accessToken = await userLogin(server, user16)
754 await getMyUserInformation(server.url, accessToken, 200)
755 })
756 })
757
758 after(async function () {
759 await cleanupTests([ server ])
760 })
761 })