]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
replace numbers with typed http status codes (#3409)
[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 })
721
722 const res = await getUserInformation(server.url, accessToken, userId)
723 const user = res.body
724
725 expect(user.username).to.equal('user_1')
726 expect(user.email).to.equal('updated2@example.com')
727 expect(user.emailVerified).to.be.true
728 expect(user.nsfwPolicy).to.equal('do_not_list')
729 expect(user.videoQuota).to.equal(42)
730 expect(user.roleLabel).to.equal('Moderator')
731 expect(user.id).to.be.a('number')
732 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
733 })
734
735 it('Should have removed the user token', async function () {
736 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401)
737
738 accessTokenUser = await userLogin(server, user)
739 })
740
741 it('Should be able to update another user password', async function () {
742 await updateUser({
743 url: server.url,
744 userId,
745 accessToken,
746 password: 'password updated'
747 })
748
749 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401)
750
751 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
752
753 user.password = 'password updated'
754 accessTokenUser = await userLogin(server, user)
755 })
756 })
757
758 describe('Video blacklists', function () {
759 it('Should be able to list video blacklist by a moderator', async function () {
760 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
761 })
762 })
763
764 describe('Remove a user', function () {
765 it('Should be able to remove this user', async function () {
766 await removeUser(server.url, userId, accessToken)
767 })
768
769 it('Should not be able to login with this user', async function () {
770 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
771 })
772
773 it('Should not have videos of this user', async function () {
774 const res = await getVideosList(server.url)
775
776 expect(res.body.total).to.equal(1)
777
778 const video = res.body.data[0]
779 expect(video.account.name).to.equal('root')
780 })
781 })
782
783 describe('Registering a new user', function () {
784 let user15AccessToken
785
786 it('Should register a new user', async function () {
787 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
788 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
789
790 await registerUserWithChannel({ url: server.url, user, channel })
791 })
792
793 it('Should be able to login with this registered user', async function () {
794 const user15 = {
795 username: 'user_15',
796 password: 'my super password'
797 }
798
799 user15AccessToken = await userLogin(server, user15)
800 })
801
802 it('Should have the correct display name', async function () {
803 const res = await getMyUserInformation(server.url, user15AccessToken)
804 const user: User = res.body
805
806 expect(user.account.displayName).to.equal('super user 15')
807 })
808
809 it('Should have the correct video quota', async function () {
810 const res = await getMyUserInformation(server.url, user15AccessToken)
811 const user = res.body
812
813 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
814 })
815
816 it('Should have created the channel', async function () {
817 const res = await getVideoChannel(server.url, 'my_user_15_channel')
818
819 expect(res.body.displayName).to.equal('my channel rocks')
820 })
821
822 it('Should remove me', async function () {
823 {
824 const res = await getUsersList(server.url, server.accessToken)
825 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
826 }
827
828 await deleteMe(server.url, user15AccessToken)
829
830 {
831 const res = await getUsersList(server.url, server.accessToken)
832 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
833 }
834 })
835 })
836
837 describe('User blocking', function () {
838 let user16Id
839 let user16AccessToken
840 const user16 = {
841 username: 'user_16',
842 password: 'my super password'
843 }
844
845 it('Should block a user', async function () {
846 const resUser = await createUser({
847 url: server.url,
848 accessToken: server.accessToken,
849 username: user16.username,
850 password: user16.password
851 })
852 user16Id = resUser.body.user.id
853
854 user16AccessToken = await userLogin(server, user16)
855
856 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200)
857 await blockUser(server.url, user16Id, server.accessToken)
858
859 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.UNAUTHORIZED_401)
860 await userLogin(server, user16, HttpStatusCode.BAD_REQUEST_400)
861 })
862
863 it('Should search user by banned status', async function () {
864 {
865 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, true)
866 const users = res.body.data as User[]
867
868 expect(res.body.total).to.equal(1)
869 expect(users.length).to.equal(1)
870
871 expect(users[0].username).to.equal(user16.username)
872 }
873
874 {
875 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, false)
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.not.equal(user16.username)
882 }
883 })
884
885 it('Should unblock a user', async function () {
886 await unblockUser(server.url, user16Id, server.accessToken)
887 user16AccessToken = await userLogin(server, user16)
888 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200)
889 })
890 })
891
892 describe('User stats', function () {
893 let user17Id
894 let user17AccessToken
895
896 it('Should report correct initial statistics about a user', async function () {
897 const user17 = {
898 username: 'user_17',
899 password: 'my super password'
900 }
901 const resUser = await createUser({
902 url: server.url,
903 accessToken: server.accessToken,
904 username: user17.username,
905 password: user17.password
906 })
907
908 user17Id = resUser.body.user.id
909 user17AccessToken = await userLogin(server, user17)
910
911 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
912 const user: User = res.body
913
914 expect(user.videosCount).to.equal(0)
915 expect(user.videoCommentsCount).to.equal(0)
916 expect(user.abusesCount).to.equal(0)
917 expect(user.abusesCreatedCount).to.equal(0)
918 expect(user.abusesAcceptedCount).to.equal(0)
919 })
920
921 it('Should report correct videos count', async function () {
922 const videoAttributes = {
923 name: 'video to test user stats'
924 }
925 await uploadVideo(server.url, user17AccessToken, videoAttributes)
926 const res1 = await getVideosList(server.url)
927 videoId = res1.body.data.find(video => video.name === videoAttributes.name).id
928
929 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true)
930 const user: User = res2.body
931
932 expect(user.videosCount).to.equal(1)
933 })
934
935 it('Should report correct video comments for user', async function () {
936 const text = 'super comment'
937 await addVideoCommentThread(server.url, user17AccessToken, videoId, text)
938
939 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
940 const user: User = res.body
941
942 expect(user.videoCommentsCount).to.equal(1)
943 })
944
945 it('Should report correct abuses counts', async function () {
946 const reason = 'my super bad reason'
947 await reportAbuse({ url: server.url, token: user17AccessToken, videoId, reason })
948
949 const res1 = await getAdminAbusesList({ url: server.url, token: server.accessToken })
950 const abuseId = res1.body.data[0].id
951
952 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true)
953 const user2: User = res2.body
954
955 expect(user2.abusesCount).to.equal(1) // number of incriminations
956 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
957
958 const body: AbuseUpdate = { state: AbuseState.ACCEPTED }
959 await updateAbuse(server.url, server.accessToken, abuseId, body)
960
961 const res3 = await getUserInformation(server.url, server.accessToken, user17Id, true)
962 const user3: User = res3.body
963
964 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
965 })
966 })
967
968 after(async function () {
969 await cleanupTests([ server ])
970 })
971 })