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