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