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