]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
fe83ca041028c98eeb6ea9153becc11a2e90def1
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { AbuseState, AbuseUpdate, MyUser, User, UserRole, Video, VideoPlaylistType } from '@shared/models'
6 import { CustomConfig } from '@shared/models/server'
7 import {
8 addVideoCommentThread,
9 blockUser,
10 cleanupTests,
11 createUser,
12 deleteMe,
13 flushAndRunServer,
14 getAccountRatings,
15 getAdminAbusesList,
16 getBlacklistedVideosList,
17 getCustomConfig,
18 getMyUserInformation,
19 getMyUserVideoQuotaUsed,
20 getMyUserVideoRating,
21 getUserInformation,
22 getUsersList,
23 getUsersListPaginationAndSort,
24 getVideoChannel,
25 getVideosList,
26 installPlugin,
27 login,
28 makePutBodyRequest,
29 rateVideo,
30 registerUserWithChannel,
31 removeUser,
32 removeVideo,
33 reportAbuse,
34 ServerInfo,
35 testImage,
36 unblockUser,
37 updateAbuse,
38 updateCustomSubConfig,
39 updateMyAvatar,
40 updateMyUser,
41 updateUser,
42 uploadVideo,
43 userLogin,
44 waitJobs
45 } from '../../../../shared/extra-utils'
46 import { follow } from '../../../../shared/extra-utils/server/follows'
47 import { logout, serverLogin, setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
48 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
49 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
50
51 const expect = chai.expect
52
53 describe('Test users', function () {
54 let server: ServerInfo
55 let accessToken: string
56 let accessTokenUser: string
57 let videoId: number
58 let userId: number
59 const user = {
60 username: 'user_1',
61 password: 'super password'
62 }
63
64 before(async function () {
65 this.timeout(30000)
66
67 server = await flushAndRunServer(1, {
68 rates_limit: {
69 login: {
70 max: 30
71 }
72 }
73 })
74
75 await setAccessTokensToServers([ server ])
76
77 await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' })
78 })
79
80 describe('OAuth client', function () {
81 it('Should create a new client')
82
83 it('Should return the first client')
84
85 it('Should remove the last client')
86
87 it('Should not login with an invalid client id', async function () {
88 const client = { id: 'client', secret: server.client.secret }
89 const res = await login(server.url, client, server.user, 400)
90
91 expect(res.body.error).to.contain('client is invalid')
92 })
93
94 it('Should not login with an invalid client secret', async function () {
95 const client = { id: server.client.id, secret: 'coucou' }
96 const res = await login(server.url, client, server.user, 400)
97
98 expect(res.body.error).to.contain('client is invalid')
99 })
100 })
101
102 describe('Login', function () {
103
104 it('Should not login with an invalid username', async function () {
105 const user = { username: 'captain crochet', password: server.user.password }
106 const res = await login(server.url, server.client, user, 400)
107
108 expect(res.body.error).to.contain('credentials are invalid')
109 })
110
111 it('Should not login with an invalid password', async function () {
112 const user = { username: server.user.username, password: 'mew_three' }
113 const res = await login(server.url, server.client, user, 400)
114
115 expect(res.body.error).to.contain('credentials are invalid')
116 })
117
118 it('Should not be able to upload a video', async function () {
119 accessToken = 'my_super_token'
120
121 const videoAttributes = {}
122 await uploadVideo(server.url, accessToken, videoAttributes, 401)
123 })
124
125 it('Should not be able to follow', async function () {
126 accessToken = 'my_super_token'
127 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
128 })
129
130 it('Should not be able to unfollow')
131
132 it('Should be able to login', async function () {
133 const res = await login(server.url, server.client, server.user, 200)
134
135 accessToken = res.body.access_token
136 })
137
138 it('Should be able to login with an insensitive username', async function () {
139 const user = { username: 'RoOt', password: server.user.password }
140 await login(server.url, server.client, user, 200)
141
142 const user2 = { username: 'rOoT', password: server.user.password }
143 await login(server.url, server.client, user2, 200)
144
145 const user3 = { username: 'ROOt', password: server.user.password }
146 await login(server.url, server.client, user3, 200)
147 })
148 })
149
150 describe('Upload', function () {
151
152 it('Should upload the video with the correct token', async function () {
153 const videoAttributes = {}
154 await uploadVideo(server.url, accessToken, videoAttributes)
155 const res = await getVideosList(server.url)
156 const video = res.body.data[0]
157
158 expect(video.account.name).to.equal('root')
159 videoId = video.id
160 })
161
162 it('Should upload the video again with the correct token', async function () {
163 const videoAttributes = {}
164 await uploadVideo(server.url, accessToken, videoAttributes)
165 })
166 })
167
168 describe('Ratings', function () {
169
170 it('Should retrieve a video rating', async function () {
171 await rateVideo(server.url, accessToken, videoId, 'like')
172 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
173 const rating = res.body
174
175 expect(rating.videoId).to.equal(videoId)
176 expect(rating.rating).to.equal('like')
177 })
178
179 it('Should retrieve ratings list', async function () {
180 await rateVideo(server.url, accessToken, videoId, 'like')
181
182 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
183 const ratings = res.body
184
185 expect(ratings.total).to.equal(1)
186 expect(ratings.data[0].video.id).to.equal(videoId)
187 expect(ratings.data[0].rating).to.equal('like')
188 })
189
190 it('Should retrieve ratings list by rating type', async function () {
191 {
192 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
193 const ratings = res.body
194 expect(ratings.data.length).to.equal(1)
195 }
196
197 {
198 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
199 const ratings = res.body
200 expect(ratings.data.length).to.equal(0)
201 }
202 })
203 })
204
205 describe('Remove video', function () {
206 it('Should not be able to remove the video with an incorrect token', async function () {
207 await removeVideo(server.url, 'bad_token', videoId, 401)
208 })
209
210 it('Should not be able to remove the video with the token of another account')
211
212 it('Should be able to remove the video with the correct token', async function () {
213 await removeVideo(server.url, accessToken, videoId)
214 })
215 })
216
217 describe('Logout', function () {
218 it('Should logout (revoke token)', async function () {
219 await logout(server.url, server.accessToken)
220 })
221
222 it('Should not be able to get the user information', async function () {
223 await getMyUserInformation(server.url, server.accessToken, 401)
224 })
225
226 it('Should not be able to upload a video', async function () {
227 await uploadVideo(server.url, server.accessToken, { name: 'video' }, 401)
228 })
229
230 it('Should not be able to rate a video', async function () {
231 const path = '/api/v1/videos/'
232 const data = {
233 rating: 'likes'
234 }
235
236 const options = {
237 url: server.url,
238 path: path + videoId,
239 token: 'wrong token',
240 fields: data,
241 statusCodeExpected: 401
242 }
243 await makePutBodyRequest(options)
244 })
245
246 it('Should be able to login again', async function () {
247 server.accessToken = await serverLogin(server)
248 })
249
250 it('Should have an expired access token')
251
252 it('Should refresh the token')
253
254 it('Should be able to get my user information again', async function () {
255 await getMyUserInformation(server.url, server.accessToken)
256 })
257 })
258
259 describe('Creating a user', function () {
260
261 it('Should be able to create a new user', async function () {
262 await createUser({
263 url: server.url,
264 accessToken: accessToken,
265 username: user.username,
266 password: user.password,
267 videoQuota: 2 * 1024 * 1024,
268 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
269 })
270 })
271
272 it('Should be able to login with this user', async function () {
273 accessTokenUser = await userLogin(server, user)
274 })
275
276 it('Should be able to get user information', async function () {
277 const res1 = await getMyUserInformation(server.url, accessTokenUser)
278 const userMe: MyUser = res1.body
279
280 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id, true)
281 const userGet: User = res2.body
282
283 for (const user of [ userMe, userGet ]) {
284 expect(user.username).to.equal('user_1')
285 expect(user.email).to.equal('user_1@example.com')
286 expect(user.nsfwPolicy).to.equal('display')
287 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
288 expect(user.roleLabel).to.equal('User')
289 expect(user.id).to.be.a('number')
290 expect(user.account.displayName).to.equal('user_1')
291 expect(user.account.description).to.be.null
292 }
293
294 expect(userMe.adminFlags).to.be.undefined
295 expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
296
297 expect(userMe.specialPlaylists).to.have.lengthOf(1)
298 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
299
300 // Check stats are included with withStats
301 expect(userGet.videosCount).to.be.a('number')
302 expect(userGet.videosCount).to.equal(0)
303 expect(userGet.videoCommentsCount).to.be.a('number')
304 expect(userGet.videoCommentsCount).to.equal(0)
305 expect(userGet.abusesCount).to.be.a('number')
306 expect(userGet.abusesCount).to.equal(0)
307 expect(userGet.abusesAcceptedCount).to.be.a('number')
308 expect(userGet.abusesAcceptedCount).to.equal(0)
309 })
310 })
311
312 describe('My videos & quotas', function () {
313
314 it('Should be able to upload a video with this user', async function () {
315 this.timeout(10000)
316
317 const videoAttributes = {
318 name: 'super user video',
319 fixture: 'video_short.webm'
320 }
321 await uploadVideo(server.url, accessTokenUser, videoAttributes)
322 })
323
324 it('Should have video quota updated', async function () {
325 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
326 const data = res.body
327
328 expect(data.videoQuotaUsed).to.equal(218910)
329
330 const resUsers = await getUsersList(server.url, server.accessToken)
331
332 const users: User[] = resUsers.body.data
333 const tmpUser = users.find(u => u.username === user.username)
334 expect(tmpUser.videoQuotaUsed).to.equal(218910)
335 })
336
337 it('Should be able to list my videos', async function () {
338 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
339 expect(res.body.total).to.equal(1)
340
341 const videos = res.body.data
342 expect(videos).to.have.lengthOf(1)
343
344 const video: Video = videos[0]
345 expect(video.name).to.equal('super user video')
346 expect(video.thumbnailPath).to.not.be.null
347 expect(video.previewPath).to.not.be.null
348 })
349
350 it('Should be able to search in my videos', async function () {
351 {
352 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'user video')
353 expect(res.body.total).to.equal(1)
354
355 const videos = res.body.data
356 expect(videos).to.have.lengthOf(1)
357 }
358
359 {
360 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'toto')
361 expect(res.body.total).to.equal(0)
362
363 const videos = res.body.data
364 expect(videos).to.have.lengthOf(0)
365 }
366 })
367
368 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
369 this.timeout(60000)
370
371 {
372 const res = await getCustomConfig(server.url, server.accessToken)
373 const config = res.body as CustomConfig
374 config.transcoding.webtorrent.enabled = false
375 config.transcoding.hls.enabled = true
376 config.transcoding.enabled = true
377 await updateCustomSubConfig(server.url, server.accessToken, config)
378 }
379
380 {
381 const videoAttributes = {
382 name: 'super user video 2',
383 fixture: 'video_short.webm'
384 }
385 await uploadVideo(server.url, accessTokenUser, videoAttributes)
386
387 await waitJobs([ server ])
388 }
389
390 {
391 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
392 const data = res.body
393
394 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
395 }
396 })
397 })
398
399 describe('Users listing', function () {
400
401 it('Should list all the users', async function () {
402 const res = await getUsersList(server.url, server.accessToken)
403 const result = res.body
404 const total = result.total
405 const users = result.data
406
407 expect(total).to.equal(2)
408 expect(users).to.be.an('array')
409 expect(users.length).to.equal(2)
410
411 const user = users[0]
412 expect(user.username).to.equal('user_1')
413 expect(user.email).to.equal('user_1@example.com')
414 expect(user.nsfwPolicy).to.equal('display')
415
416 const rootUser = users[1]
417 expect(rootUser.username).to.equal('root')
418 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
419 expect(user.nsfwPolicy).to.equal('display')
420
421 expect(rootUser.lastLoginDate).to.exist
422 expect(user.lastLoginDate).to.exist
423
424 userId = user.id
425 })
426
427 it('Should list only the first user by username asc', async function () {
428 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
429
430 const result = res.body
431 const total = result.total
432 const users = result.data
433
434 expect(total).to.equal(2)
435 expect(users.length).to.equal(1)
436
437 const user = users[0]
438 expect(user.username).to.equal('root')
439 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
440 expect(user.roleLabel).to.equal('Administrator')
441 expect(user.nsfwPolicy).to.equal('display')
442 })
443
444 it('Should list only the first user by username desc', async function () {
445 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
446 const result = res.body
447 const total = result.total
448 const users = result.data
449
450 expect(total).to.equal(2)
451 expect(users.length).to.equal(1)
452
453 const user = users[0]
454 expect(user.username).to.equal('user_1')
455 expect(user.email).to.equal('user_1@example.com')
456 expect(user.nsfwPolicy).to.equal('display')
457 })
458
459 it('Should list only the second user by createdAt desc', async function () {
460 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
461 const result = res.body
462 const total = result.total
463 const users = result.data
464
465 expect(total).to.equal(2)
466 expect(users.length).to.equal(1)
467
468 const user = users[0]
469 expect(user.username).to.equal('user_1')
470 expect(user.email).to.equal('user_1@example.com')
471 expect(user.nsfwPolicy).to.equal('display')
472 })
473
474 it('Should list all the users by createdAt asc', async function () {
475 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
476 const result = res.body
477 const total = result.total
478 const users = result.data
479
480 expect(total).to.equal(2)
481 expect(users.length).to.equal(2)
482
483 expect(users[0].username).to.equal('root')
484 expect(users[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
485 expect(users[0].nsfwPolicy).to.equal('display')
486
487 expect(users[1].username).to.equal('user_1')
488 expect(users[1].email).to.equal('user_1@example.com')
489 expect(users[1].nsfwPolicy).to.equal('display')
490 })
491
492 it('Should search user by username', async function () {
493 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
494 const users = res.body.data as User[]
495
496 expect(res.body.total).to.equal(1)
497 expect(users.length).to.equal(1)
498
499 expect(users[0].username).to.equal('root')
500 })
501
502 it('Should search user by email', async function () {
503 {
504 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
505 const users = res.body.data as User[]
506
507 expect(res.body.total).to.equal(1)
508 expect(users.length).to.equal(1)
509
510 expect(users[0].username).to.equal('user_1')
511 expect(users[0].email).to.equal('user_1@example.com')
512 }
513
514 {
515 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
516 const users = res.body.data as User[]
517
518 expect(res.body.total).to.equal(2)
519 expect(users.length).to.equal(2)
520
521 expect(users[0].username).to.equal('root')
522 expect(users[1].username).to.equal('user_1')
523 }
524 })
525 })
526
527 describe('Update my account', function () {
528 it('Should update my password', async function () {
529 await updateMyUser({
530 url: server.url,
531 accessToken: accessTokenUser,
532 currentPassword: 'super password',
533 password: 'new password'
534 })
535 user.password = 'new password'
536
537 await userLogin(server, user, 200)
538 })
539
540 it('Should be able to change the NSFW display attribute', async function () {
541 await updateMyUser({
542 url: server.url,
543 accessToken: accessTokenUser,
544 nsfwPolicy: 'do_not_list'
545 })
546
547 const res = await getMyUserInformation(server.url, accessTokenUser)
548 const user = res.body
549
550 expect(user.username).to.equal('user_1')
551 expect(user.email).to.equal('user_1@example.com')
552 expect(user.nsfwPolicy).to.equal('do_not_list')
553 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
554 expect(user.id).to.be.a('number')
555 expect(user.account.displayName).to.equal('user_1')
556 expect(user.account.description).to.be.null
557 })
558
559 it('Should be able to change the autoPlayVideo attribute', async function () {
560 await updateMyUser({
561 url: server.url,
562 accessToken: accessTokenUser,
563 autoPlayVideo: false
564 })
565
566 const res = await getMyUserInformation(server.url, accessTokenUser)
567 const user = res.body
568
569 expect(user.autoPlayVideo).to.be.false
570 })
571
572 it('Should be able to change the autoPlayNextVideo attribute', async function () {
573 await updateMyUser({
574 url: server.url,
575 accessToken: accessTokenUser,
576 autoPlayNextVideo: true
577 })
578
579 const res = await getMyUserInformation(server.url, accessTokenUser)
580 const user = res.body
581
582 expect(user.autoPlayNextVideo).to.be.true
583 })
584
585 it('Should be able to change the email attribute', async function () {
586 await updateMyUser({
587 url: server.url,
588 accessToken: accessTokenUser,
589 currentPassword: 'new password',
590 email: 'updated@example.com'
591 })
592
593 const res = await getMyUserInformation(server.url, accessTokenUser)
594 const user = res.body
595
596 expect(user.username).to.equal('user_1')
597 expect(user.email).to.equal('updated@example.com')
598 expect(user.nsfwPolicy).to.equal('do_not_list')
599 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
600 expect(user.id).to.be.a('number')
601 expect(user.account.displayName).to.equal('user_1')
602 expect(user.account.description).to.be.null
603 })
604
605 it('Should be able to update my avatar with a gif', async function () {
606 const fixture = 'avatar.gif'
607
608 await updateMyAvatar({
609 url: server.url,
610 accessToken: accessTokenUser,
611 fixture
612 })
613
614 const res = await getMyUserInformation(server.url, accessTokenUser)
615 const user = res.body
616
617 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
618 })
619
620 it('Should be able to update my avatar with a gif, and then a png', async function () {
621 for (const extension of [ '.png', '.gif' ]) {
622 const fixture = 'avatar' + extension
623
624 await updateMyAvatar({
625 url: server.url,
626 accessToken: accessTokenUser,
627 fixture
628 })
629
630 const res = await getMyUserInformation(server.url, accessTokenUser)
631 const user = res.body
632
633 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
634 }
635 })
636
637 it('Should be able to update my display name', async function () {
638 await updateMyUser({
639 url: server.url,
640 accessToken: accessTokenUser,
641 displayName: 'new display name'
642 })
643
644 const res = await getMyUserInformation(server.url, accessTokenUser)
645 const user = res.body
646
647 expect(user.username).to.equal('user_1')
648 expect(user.email).to.equal('updated@example.com')
649 expect(user.nsfwPolicy).to.equal('do_not_list')
650 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
651 expect(user.id).to.be.a('number')
652 expect(user.account.displayName).to.equal('new display name')
653 expect(user.account.description).to.be.null
654 })
655
656 it('Should be able to update my description', async function () {
657 await updateMyUser({
658 url: server.url,
659 accessToken: accessTokenUser,
660 description: 'my super description updated'
661 })
662
663 const res = await getMyUserInformation(server.url, accessTokenUser)
664 const user: User = res.body
665
666 expect(user.username).to.equal('user_1')
667 expect(user.email).to.equal('updated@example.com')
668 expect(user.nsfwPolicy).to.equal('do_not_list')
669 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
670 expect(user.id).to.be.a('number')
671 expect(user.account.displayName).to.equal('new display name')
672 expect(user.account.description).to.equal('my super description updated')
673 expect(user.noWelcomeModal).to.be.false
674 expect(user.noInstanceConfigWarningModal).to.be.false
675 })
676
677 it('Should be able to update my theme', async function () {
678 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
679 await updateMyUser({
680 url: server.url,
681 accessToken: accessTokenUser,
682 theme
683 })
684
685 const res = await getMyUserInformation(server.url, accessTokenUser)
686 const body: User = res.body
687
688 expect(body.theme).to.equal(theme)
689 }
690 })
691
692 it('Should be able to update my modal preferences', async function () {
693 await updateMyUser({
694 url: server.url,
695 accessToken: accessTokenUser,
696 noInstanceConfigWarningModal: true,
697 noWelcomeModal: true
698 })
699
700 const res = await getMyUserInformation(server.url, accessTokenUser)
701 const user: User = res.body
702
703 expect(user.noWelcomeModal).to.be.true
704 expect(user.noInstanceConfigWarningModal).to.be.true
705 })
706 })
707
708 describe('Updating another user', function () {
709 it('Should be able to update another user', async function () {
710 await updateUser({
711 url: server.url,
712 userId,
713 accessToken,
714 email: 'updated2@example.com',
715 emailVerified: true,
716 videoQuota: 42,
717 role: UserRole.MODERATOR,
718 adminFlags: UserAdminFlag.NONE
719 })
720
721 const res = await getUserInformation(server.url, accessToken, userId)
722 const user = res.body
723
724 expect(user.username).to.equal('user_1')
725 expect(user.email).to.equal('updated2@example.com')
726 expect(user.emailVerified).to.be.true
727 expect(user.nsfwPolicy).to.equal('do_not_list')
728 expect(user.videoQuota).to.equal(42)
729 expect(user.roleLabel).to.equal('Moderator')
730 expect(user.id).to.be.a('number')
731 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
732 })
733
734 it('Should have removed the user token', async function () {
735 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
736
737 accessTokenUser = await userLogin(server, user)
738 })
739
740 it('Should be able to update another user password', async function () {
741 await updateUser({
742 url: server.url,
743 userId,
744 accessToken,
745 password: 'password updated'
746 })
747
748 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
749
750 await userLogin(server, user, 400)
751
752 user.password = 'password updated'
753 accessTokenUser = await userLogin(server, user)
754 })
755 })
756
757 describe('Video blacklists', function () {
758 it('Should be able to list video blacklist by a moderator', async function () {
759 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
760 })
761 })
762
763 describe('Remove a user', function () {
764 it('Should be able to remove this user', async function () {
765 await removeUser(server.url, userId, accessToken)
766 })
767
768 it('Should not be able to login with this user', async function () {
769 await userLogin(server, user, 400)
770 })
771
772 it('Should not have videos of this user', async function () {
773 const res = await getVideosList(server.url)
774
775 expect(res.body.total).to.equal(1)
776
777 const video = res.body.data[0]
778 expect(video.account.name).to.equal('root')
779 })
780 })
781
782 describe('Registering a new user', function () {
783 let user15AccessToken
784
785 it('Should register a new user', async function () {
786 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
787 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
788
789 await registerUserWithChannel({ url: server.url, user, channel })
790 })
791
792 it('Should be able to login with this registered user', async function () {
793 const user15 = {
794 username: 'user_15',
795 password: 'my super password'
796 }
797
798 user15AccessToken = await userLogin(server, user15)
799 })
800
801 it('Should have the correct display name', async function () {
802 const res = await getMyUserInformation(server.url, user15AccessToken)
803 const user: User = res.body
804
805 expect(user.account.displayName).to.equal('super user 15')
806 })
807
808 it('Should have the correct video quota', async function () {
809 const res = await getMyUserInformation(server.url, user15AccessToken)
810 const user = res.body
811
812 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
813 })
814
815 it('Should have created the channel', async function () {
816 const res = await getVideoChannel(server.url, 'my_user_15_channel')
817
818 expect(res.body.displayName).to.equal('my channel rocks')
819 })
820
821 it('Should remove me', async function () {
822 {
823 const res = await getUsersList(server.url, server.accessToken)
824 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
825 }
826
827 await deleteMe(server.url, user15AccessToken)
828
829 {
830 const res = await getUsersList(server.url, server.accessToken)
831 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
832 }
833 })
834 })
835
836 describe('User blocking', function () {
837 let user16Id
838 let user16AccessToken
839 const user16 = {
840 username: 'user_16',
841 password: 'my super password'
842 }
843
844 it('Should block a user', async function () {
845 const resUser = await createUser({
846 url: server.url,
847 accessToken: server.accessToken,
848 username: user16.username,
849 password: user16.password
850 })
851 user16Id = resUser.body.user.id
852
853 user16AccessToken = await userLogin(server, user16)
854
855 await getMyUserInformation(server.url, user16AccessToken, 200)
856 await blockUser(server.url, user16Id, server.accessToken)
857
858 await getMyUserInformation(server.url, user16AccessToken, 401)
859 await userLogin(server, user16, 400)
860 })
861
862 it('Should search user by banned status', async function () {
863 {
864 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, true)
865 const users = res.body.data as User[]
866
867 expect(res.body.total).to.equal(1)
868 expect(users.length).to.equal(1)
869
870 expect(users[0].username).to.equal(user16.username)
871 }
872
873 {
874 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, false)
875 const users = res.body.data as User[]
876
877 expect(res.body.total).to.equal(1)
878 expect(users.length).to.equal(1)
879
880 expect(users[0].username).to.not.equal(user16.username)
881 }
882 })
883
884 it('Should unblock a user', async function () {
885 await unblockUser(server.url, user16Id, server.accessToken)
886 user16AccessToken = await userLogin(server, user16)
887 await getMyUserInformation(server.url, user16AccessToken, 200)
888 })
889 })
890
891 describe('User stats', function () {
892 let user17Id
893 let user17AccessToken
894
895 it('Should report correct initial statistics about a user', async function () {
896 const user17 = {
897 username: 'user_17',
898 password: 'my super password'
899 }
900 const resUser = await createUser({
901 url: server.url,
902 accessToken: server.accessToken,
903 username: user17.username,
904 password: user17.password
905 })
906
907 user17Id = resUser.body.user.id
908 user17AccessToken = await userLogin(server, user17)
909
910 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
911 const user: User = res.body
912
913 expect(user.videosCount).to.equal(0)
914 expect(user.videoCommentsCount).to.equal(0)
915 expect(user.abusesCount).to.equal(0)
916 expect(user.abusesCreatedCount).to.equal(0)
917 expect(user.abusesAcceptedCount).to.equal(0)
918 })
919
920 it('Should report correct videos count', async function () {
921 const videoAttributes = {
922 name: 'video to test user stats'
923 }
924 await uploadVideo(server.url, user17AccessToken, videoAttributes)
925 const res1 = await getVideosList(server.url)
926 videoId = res1.body.data.find(video => video.name === videoAttributes.name).id
927
928 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true)
929 const user: User = res2.body
930
931 expect(user.videosCount).to.equal(1)
932 })
933
934 it('Should report correct video comments for user', async function () {
935 const text = 'super comment'
936 await addVideoCommentThread(server.url, user17AccessToken, videoId, text)
937
938 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
939 const user: User = res.body
940
941 expect(user.videoCommentsCount).to.equal(1)
942 })
943
944 it('Should report correct abuses counts', async function () {
945 const reason = 'my super bad reason'
946 await reportAbuse({ url: server.url, token: user17AccessToken, videoId, reason })
947
948 const res1 = await getAdminAbusesList({ url: server.url, token: server.accessToken })
949 const abuseId = res1.body.data[0].id
950
951 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true)
952 const user2: User = res2.body
953
954 expect(user2.abusesCount).to.equal(1) // number of incriminations
955 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
956
957 const body: AbuseUpdate = { state: AbuseState.ACCEPTED }
958 await updateAbuse(server.url, server.accessToken, abuseId, body)
959
960 const res3 = await getUserInformation(server.url, server.accessToken, user17Id, true)
961 const user3: User = res3.body
962
963 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
964 })
965 })
966
967 after(async function () {
968 await cleanupTests([ server ])
969 })
970 })