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