]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users.ts
Avoir some circular dependencies
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
86d13ec2 2
0e1dc3e7 3import * as chai from 'chai'
afffe988 4import 'mocha'
76314386 5import { MyUser, User, UserRole, Video, VideoPlaylistType, VideoAbuseState, VideoAbuseUpdate } from '../../../../shared/index'
0e1dc3e7 6import {
a890d1e0 7 blockUser,
7c3b7976 8 cleanupTests,
a890d1e0
C
9 createUser,
10 deleteMe,
7c3b7976 11 flushAndRunServer,
c100a614 12 getAccountRatings,
a890d1e0
C
13 getBlacklistedVideosList,
14 getMyUserInformation,
15 getMyUserVideoQuotaUsed,
16 getMyUserVideoRating,
17 getUserInformation,
18 getUsersList,
19 getUsersListPaginationAndSort,
1f20622f 20 getVideoChannel,
a1587156
C
21 getVideosList,
22 installPlugin,
a890d1e0
C
23 login,
24 makePutBodyRequest,
25 rateVideo,
1f20622f 26 registerUserWithChannel,
a890d1e0
C
27 removeUser,
28 removeVideo,
a890d1e0
C
29 ServerInfo,
30 testImage,
31 unblockUser,
32 updateMyAvatar,
33 updateMyUser,
34 updateUser,
35 uploadVideo,
76314386
RK
36 userLogin,
37 reportVideoAbuse,
38 addVideoCommentThread,
39 updateVideoAbuse,
5600def4 40 getVideoAbusesList, updateCustomSubConfig, getCustomConfig, waitJobs
94565d52
C
41} from '../../../../shared/extra-utils'
42import { follow } from '../../../../shared/extra-utils/server/follows'
7fed6375 43import { setAccessTokensToServers, logout } from '../../../../shared/extra-utils/users/login'
94565d52 44import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
1eddc9a7 45import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
5600def4 46import { CustomConfig } from '@shared/models/server'
0e1dc3e7 47
afffe988
C
48const expect = chai.expect
49
0e1dc3e7
C
50describe('Test users', function () {
51 let server: ServerInfo
52 let accessToken: string
53 let accessTokenUser: string
54 let videoId: number
55 let userId: number
f8b8c36b
C
56 const user = {
57 username: 'user_1',
58 password: 'super password'
59 }
0e1dc3e7
C
60
61 before(async function () {
e212f887 62 this.timeout(30000)
210feb6c 63 server = await flushAndRunServer(1)
86d13ec2
C
64
65 await setAccessTokensToServers([ server ])
9b474844
C
66
67 await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' })
0e1dc3e7
C
68 })
69
1eddc9a7
C
70 describe('OAuth client', function () {
71 it('Should create a new client')
0e1dc3e7 72
1eddc9a7 73 it('Should return the first client')
0e1dc3e7 74
1eddc9a7 75 it('Should remove the last client')
0e1dc3e7 76
1eddc9a7
C
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)
0e1dc3e7 80
1eddc9a7
C
81 expect(res.body.error).to.contain('client is invalid')
82 })
0e1dc3e7 83
1eddc9a7
C
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)
0e1dc3e7 87
1eddc9a7
C
88 expect(res.body.error).to.contain('client is invalid')
89 })
0e1dc3e7
C
90 })
91
1eddc9a7 92 describe('Login', function () {
0e1dc3e7 93
1eddc9a7
C
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)
0e1dc3e7 97
1eddc9a7
C
98 expect(res.body.error).to.contain('credentials are invalid')
99 })
0e1dc3e7 100
1eddc9a7
C
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)
0e1dc3e7 104
1eddc9a7
C
105 expect(res.body.error).to.contain('credentials are invalid')
106 })
0e1dc3e7 107
1eddc9a7
C
108 it('Should not be able to upload a video', async function () {
109 accessToken = 'my_super_token'
0e1dc3e7 110
1eddc9a7
C
111 const videoAttributes = {}
112 await uploadVideo(server.url, accessToken, videoAttributes, 401)
113 })
0e1dc3e7 114
1eddc9a7
C
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 })
0e1dc3e7 119
1eddc9a7 120 it('Should not be able to unfollow')
0e1dc3e7 121
1eddc9a7
C
122 it('Should be able to login', async function () {
123 const res = await login(server.url, server.client, server.user, 200)
0e1dc3e7 124
1eddc9a7
C
125 accessToken = res.body.access_token
126 })
50b4dcce
NB
127
128 it('Should be able to login with an insensitive username', async function () {
129 const user = { username: 'RoOt', password: server.user.password }
a1587156 130 await login(server.url, server.client, user, 200)
50b4dcce
NB
131
132 const user2 = { username: 'rOoT', password: server.user.password }
a1587156 133 await login(server.url, server.client, user2, 200)
50b4dcce
NB
134
135 const user3 = { username: 'ROOt', password: server.user.password }
a1587156 136 await login(server.url, server.client, user3, 200)
50b4dcce 137 })
0e1dc3e7
C
138 })
139
1eddc9a7 140 describe('Upload', function () {
0e1dc3e7 141
1eddc9a7
C
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)
a1587156 146 const video = res.body.data[0]
0e1dc3e7 147
1eddc9a7
C
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 })
0e1dc3e7
C
156 })
157
1eddc9a7 158 describe('Ratings', function () {
22834691 159
1eddc9a7
C
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
c100a614 164
1eddc9a7
C
165 expect(rating.videoId).to.equal(videoId)
166 expect(rating.rating).to.equal('like')
167 })
c100a614 168
1eddc9a7
C
169 it('Should retrieve ratings list', async function () {
170 await rateVideo(server.url, accessToken, videoId, 'like')
22834691 171
1eddc9a7 172 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
22834691 173 const ratings = res.body
0e1dc3e7 174
1eddc9a7 175 expect(ratings.total).to.equal(1)
a1587156
C
176 expect(ratings.data[0].video.id).to.equal(videoId)
177 expect(ratings.data[0].rating).to.equal('like')
1eddc9a7 178 })
0e1dc3e7 179
1eddc9a7
C
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 })
0e1dc3e7
C
193 })
194
1eddc9a7
C
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 })
0e1dc3e7 199
1eddc9a7 200 it('Should not be able to remove the video with the token of another account')
0e1dc3e7 201
1eddc9a7
C
202 it('Should be able to remove the video with the correct token', async function () {
203 await removeVideo(server.url, accessToken, videoId)
204 })
0e1dc3e7
C
205 })
206
1eddc9a7 207 describe('Logout', function () {
7fed6375
C
208 it('Should logout (revoke token)', async function () {
209 await logout(server.url, server.accessToken)
210 })
0e1dc3e7 211
7fed6375
C
212 it('Should not be able to get the user information', async function () {
213 await getMyUserInformation(server.url, server.accessToken, 401)
214 })
0e1dc3e7 215
7fed6375
C
216 it('Should not be able to upload a video', async function () {
217 await uploadVideo(server.url, server.accessToken, { name: 'video' }, 401)
218 })
0e1dc3e7 219
1eddc9a7 220 it('Should not be able to remove a video')
0e1dc3e7 221
1eddc9a7
C
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 }
0e1dc3e7 227
1eddc9a7
C
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 })
0e1dc3e7 237
1eddc9a7 238 it('Should be able to login again')
0e1dc3e7 239
1eddc9a7 240 it('Should have an expired access token')
0e1dc3e7 241
1eddc9a7
C
242 it('Should refresh the token')
243
244 it('Should be able to upload a video again')
0e1dc3e7
C
245 })
246
1eddc9a7 247 describe('Creating a user', function () {
ce5496d6 248
1eddc9a7
C
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 })
a76138ff 259
1eddc9a7
C
260 it('Should be able to login with this user', async function () {
261 accessTokenUser = await userLogin(server, user)
262 })
a76138ff 263
1eddc9a7
C
264 it('Should be able to get user information', async function () {
265 const res1 = await getMyUserInformation(server.url, accessTokenUser)
ac0868bc 266 const userMe: MyUser = res1.body
1eddc9a7 267
76314386 268 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id, true)
1eddc9a7
C
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)
29128b2f
RK
284
285 expect(userMe.specialPlaylists).to.have.lengthOf(1)
ac0868bc 286 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
76314386
RK
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)
1eddc9a7 297 })
ce5496d6
C
298 })
299
1eddc9a7 300 describe('My videos & quotas', function () {
11474c3c 301
1eddc9a7 302 it('Should be able to upload a video with this user', async function () {
5600def4 303 this.timeout(10000)
0e1dc3e7 304
1eddc9a7
C
305 const videoAttributes = {
306 name: 'super user video',
307 fixture: 'video_short.webm'
308 }
309 await uploadVideo(server.url, accessTokenUser, videoAttributes)
310 })
afffe988 311
1eddc9a7
C
312 it('Should have video quota updated', async function () {
313 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
314 const data = res.body
0e1dc3e7 315
1eddc9a7 316 expect(data.videoQuotaUsed).to.equal(218910)
0e1dc3e7 317
1eddc9a7 318 const resUsers = await getUsersList(server.url, server.accessToken)
0e1dc3e7 319
1eddc9a7
C
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 })
0e1dc3e7 324
1eddc9a7
C
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)
0e1dc3e7 328
1eddc9a7
C
329 const videos = res.body.data
330 expect(videos).to.have.lengthOf(1)
afffe988 331
a1587156 332 const video: Video = videos[0]
a18f275d
C
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
1eddc9a7 336 })
cca1e13b
C
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 })
5600def4
C
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 })
0e1dc3e7
C
385 })
386
1eddc9a7 387 describe('Users listing', function () {
0e1dc3e7 388
1eddc9a7
C
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
afffe988 394
1eddc9a7
C
395 expect(total).to.equal(2)
396 expect(users).to.be.an('array')
397 expect(users.length).to.equal(2)
0e1dc3e7 398
a1587156 399 const user = users[0]
1eddc9a7
C
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')
0e1dc3e7 403
a1587156 404 const rootUser = users[1]
1eddc9a7 405 expect(rootUser.username).to.equal('root')
48f07b4a 406 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
1eddc9a7 407 expect(user.nsfwPolicy).to.equal('display')
afffe988 408
1eddc9a7
C
409 userId = user.id
410 })
0e1dc3e7 411
1eddc9a7
C
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')
0e1dc3e7 414
1eddc9a7
C
415 const result = res.body
416 const total = result.total
417 const users = result.data
a7ba16b6 418
1eddc9a7
C
419 expect(total).to.equal(2)
420 expect(users.length).to.equal(1)
afffe988 421
a1587156 422 const user = users[0]
1eddc9a7 423 expect(user.username).to.equal('root')
48f07b4a 424 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
1eddc9a7
C
425 expect(user.roleLabel).to.equal('Administrator')
426 expect(user.nsfwPolicy).to.equal('display')
427 })
0e1dc3e7 428
1eddc9a7
C
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
24b9417c 434
1eddc9a7
C
435 expect(total).to.equal(2)
436 expect(users.length).to.equal(1)
24b9417c 437
a1587156 438 const user = users[0]
1eddc9a7
C
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 })
24b9417c 443
1eddc9a7
C
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
24b9417c 449
1eddc9a7 450 expect(total).to.equal(2)
24b9417c
C
451 expect(users.length).to.equal(1)
452
a1587156 453 const user = users[0]
1eddc9a7
C
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 })
24b9417c 458
1eddc9a7
C
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
24b9417c 464
1eddc9a7 465 expect(total).to.equal(2)
24b9417c
C
466 expect(users.length).to.equal(2)
467
a1587156
C
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')
24b9417c 471
a1587156
C
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')
11ba2ab3 475 })
0e1dc3e7 476
1eddc9a7
C
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)
0e1dc3e7 483
a1587156 484 expect(users[0].username).to.equal('root')
11ba2ab3 485 })
0e1dc3e7 486
1eddc9a7
C
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[]
0e1dc3e7 491
1eddc9a7
C
492 expect(res.body.total).to.equal(1)
493 expect(users.length).to.equal(1)
5c98d3bf 494
a1587156
C
495 expect(users[0].username).to.equal('user_1')
496 expect(users[0].email).to.equal('user_1@example.com')
1eddc9a7 497 }
7efe153b 498
1eddc9a7
C
499 {
500 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
501 const users = res.body.data as User[]
7efe153b 502
1eddc9a7
C
503 expect(res.body.total).to.equal(2)
504 expect(users.length).to.equal(2)
7efe153b 505
a1587156
C
506 expect(users[0].username).to.equal('root')
507 expect(users[1].username).to.equal('user_1')
1eddc9a7 508 }
11ba2ab3 509 })
5c98d3bf
C
510 })
511
1eddc9a7
C
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',
43d0ea7f 518 password: 'new password'
1eddc9a7
C
519 })
520 user.password = 'new password'
c5911fd3 521
1eddc9a7 522 await userLogin(server, user, 200)
c5911fd3
C
523 })
524
1eddc9a7
C
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 })
c5911fd3 543
1eddc9a7
C
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 })
c5911fd3 550
1eddc9a7
C
551 const res = await getMyUserInformation(server.url, accessTokenUser)
552 const user = res.body
553
554 expect(user.autoPlayVideo).to.be.false
ed56ad11
C
555 })
556
6aa54148
L
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
675a8fc7 570 it('Should be able to change the email attribute', async function () {
1eddc9a7
C
571 await updateMyUser({
572 url: server.url,
573 accessToken: accessTokenUser,
675a8fc7 574 currentPassword: 'new password',
1eddc9a7
C
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 })
ed56ad11 589
1eddc9a7
C
590 it('Should be able to update my avatar', async function () {
591 const fixture = 'avatar.png'
ed56ad11 592
1eddc9a7
C
593 await updateMyAvatar({
594 url: server.url,
595 accessToken: accessTokenUser,
596 fixture
597 })
2422c46b 598
1eddc9a7
C
599 const res = await getMyUserInformation(server.url, accessTokenUser)
600 const user = res.body
2422c46b 601
1eddc9a7
C
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 })
2422c46b 623
1eddc9a7
C
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)
43d0ea7f 632 const user: User = res.body
1eddc9a7
C
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')
43d0ea7f
C
641 expect(user.noWelcomeModal).to.be.false
642 expect(user.noInstanceConfigWarningModal).to.be.false
1eddc9a7 643 })
9b474844
C
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 })
43d0ea7f
C
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 })
0e1dc3e7
C
674 })
675
1eddc9a7 676 describe('Updating another user', function () {
1eddc9a7
C
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 })
f8b8c36b 701
1eddc9a7
C
702 it('Should have removed the user token', async function () {
703 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
f8b8c36b 704
1eddc9a7 705 accessTokenUser = await userLogin(server, user)
b426edd4
C
706 })
707
1eddc9a7
C
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 })
b426edd4 715
1eddc9a7 716 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
b426edd4 717
1eddc9a7 718 await userLogin(server, user, 400)
b426edd4 719
1eddc9a7
C
720 user.password = 'password updated'
721 accessTokenUser = await userLogin(server, user)
722 })
757f0da3
C
723 })
724
1eddc9a7
C
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 })
0e1dc3e7
C
729 })
730
1eddc9a7
C
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 })
0e1dc3e7 735
1eddc9a7
C
736 it('Should not be able to login with this user', async function () {
737 await userLogin(server, user, 400)
738 })
0e1dc3e7 739
1eddc9a7
C
740 it('Should not have videos of this user', async function () {
741 const res = await getVideosList(server.url)
0e1dc3e7 742
1eddc9a7 743 expect(res.body.total).to.equal(1)
0e1dc3e7 744
a1587156 745 const video = res.body.data[0]
1eddc9a7
C
746 expect(video.account.name).to.equal('root')
747 })
0e1dc3e7
C
748 })
749
1eddc9a7 750 describe('Registering a new user', function () {
76314386
RK
751 let user15AccessToken
752
1eddc9a7 753 it('Should register a new user', async function () {
1f20622f 754 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
e590b4a5
C
755 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
756
757 await registerUserWithChannel({ url: server.url, user, channel })
1eddc9a7 758 })
0e1dc3e7 759
1eddc9a7
C
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 }
5c98d3bf 765
76314386 766 user15AccessToken = await userLogin(server, user15)
1eddc9a7 767 })
5c98d3bf 768
1f20622f 769 it('Should have the correct display name', async function () {
76314386 770 const res = await getMyUserInformation(server.url, user15AccessToken)
1f20622f
C
771 const user: User = res.body
772
773 expect(user.account.displayName).to.equal('super user 15')
774 })
775
1eddc9a7 776 it('Should have the correct video quota', async function () {
76314386 777 const res = await getMyUserInformation(server.url, user15AccessToken)
1eddc9a7 778 const user = res.body
0e1dc3e7 779
1eddc9a7
C
780 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
781 })
92b9d60c 782
e590b4a5
C
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
1eddc9a7
C
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 }
92b9d60c 794
76314386 795 await deleteMe(server.url, user15AccessToken)
1eddc9a7
C
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 })
92b9d60c
C
802 })
803
1eddc9a7 804 describe('User blocking', function () {
76314386
RK
805 let user16Id
806 let user16AccessToken
807
1eddc9a7
C
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 })
76314386 819 user16Id = resUser.body.user.id
e6921918 820
76314386 821 user16AccessToken = await userLogin(server, user16)
e6921918 822
76314386 823 await getMyUserInformation(server.url, user16AccessToken, 200)
1eddc9a7 824 await blockUser(server.url, user16Id, server.accessToken)
e6921918 825
76314386 826 await getMyUserInformation(server.url, user16AccessToken, 401)
1eddc9a7 827 await userLogin(server, user16, 400)
e6921918 828
1eddc9a7 829 await unblockUser(server.url, user16Id, server.accessToken)
76314386
RK
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
1eddc9a7 908 })
e6921918
C
909 })
910
7c3b7976
C
911 after(async function () {
912 await cleanupTests([ server ])
0e1dc3e7
C
913 })
914})