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