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