1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { User, UserRole } from '../../../../shared/index'
11 getBlacklistedVideosList,
13 getMyUserVideoQuotaUsed,
17 getUsersListPaginationAndSort,
35 } from '../../../../shared/utils/index'
36 import { follow } from '../../../../shared/utils/server/follows'
37 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
38 import { getMyVideos } from '../../../../shared/utils/videos/videos'
40 const expect = chai.expect
42 describe('Test users', function () {
43 let server: ServerInfo
44 let accessToken: string
45 let accessTokenUser: string
50 password: 'super password'
53 before(async function () {
57 server = await runServer(1)
59 await setAccessTokensToServers([ server ])
62 it('Should create a new client')
64 it('Should return the first client')
66 it('Should remove the last client')
68 it('Should not login with an invalid client id', async function () {
69 const client = { id: 'client', secret: server.client.secret }
70 const res = await login(server.url, client, server.user, 400)
72 expect(res.body.error).to.contain('client is invalid')
75 it('Should not login with an invalid client secret', async function () {
76 const client = { id: server.client.id, secret: 'coucou' }
77 const res = await login(server.url, client, server.user, 400)
79 expect(res.body.error).to.contain('client is invalid')
82 it('Should not login with an invalid username', async function () {
83 const user = { username: 'captain crochet', password: server.user.password }
84 const res = await login(server.url, server.client, user, 400)
86 expect(res.body.error).to.contain('credentials are invalid')
89 it('Should not login with an invalid password', async function () {
90 const user = { username: server.user.username, password: 'mew_three' }
91 const res = await login(server.url, server.client, user, 400)
93 expect(res.body.error).to.contain('credentials are invalid')
96 it('Should not be able to upload a video', async function () {
97 accessToken = 'my_super_token'
99 const videoAttributes = {}
100 await uploadVideo(server.url, accessToken, videoAttributes, 401)
103 it('Should not be able to follow', async function () {
104 accessToken = 'my_super_token'
105 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
108 it('Should not be able to unfollow')
110 it('Should be able to login', async function () {
111 const res = await login(server.url, server.client, server.user, 200)
113 accessToken = res.body.access_token
116 it('Should upload the video with the correct token', async function () {
117 const videoAttributes = {}
118 await uploadVideo(server.url, accessToken, videoAttributes)
119 const res = await getVideosList(server.url)
120 const video = res.body.data[ 0 ]
122 expect(video.account.name).to.equal('root')
126 it('Should upload the video again with the correct token', async function () {
127 const videoAttributes = {}
128 await uploadVideo(server.url, accessToken, videoAttributes)
131 it('Should retrieve a video rating', async function () {
132 await rateVideo(server.url, accessToken, videoId, 'like')
133 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
134 const rating = res.body
136 expect(rating.videoId).to.equal(videoId)
137 expect(rating.rating).to.equal('like')
140 it('Should not be able to remove the video with an incorrect token', async function () {
141 await removeVideo(server.url, 'bad_token', videoId, 401)
144 it('Should not be able to remove the video with the token of another account')
146 it('Should be able to remove the video with the correct token', async function () {
147 await removeVideo(server.url, accessToken, videoId)
150 it('Should logout (revoke token)')
152 it('Should not be able to get the user information')
154 it('Should not be able to upload a video')
156 it('Should not be able to remove a video')
158 it('Should not be able to rate a video', async function () {
159 const path = '/api/v1/videos/'
166 path: path + videoId,
167 token: 'wrong token',
169 statusCodeExpected: 401
171 await makePutBodyRequest(options)
174 it('Should be able to login again')
176 it('Should have an expired access token')
178 it('Should refresh the token')
180 it('Should be able to upload a video again')
182 it('Should be able to create a new user', async function () {
183 await createUser(server.url, accessToken, user.username, user.password, 2 * 1024 * 1024)
186 it('Should be able to login with this user', async function () {
187 accessTokenUser = await userLogin(server, user)
190 it('Should be able to get the user information', async function () {
191 const res = await getMyUserInformation(server.url, accessTokenUser)
192 const user = res.body
194 expect(user.username).to.equal('user_1')
195 expect(user.email).to.equal('user_1@example.com')
196 expect(user.nsfwPolicy).to.equal('display')
197 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
198 expect(user.roleLabel).to.equal('User')
199 expect(user.id).to.be.a('number')
200 expect(user.account.displayName).to.equal('user_1')
201 expect(user.account.description).to.be.null
204 it('Should be able to upload a video with this user', async function () {
207 const videoAttributes = {
208 name: 'super user video',
209 fixture: 'video_short.webm'
211 await uploadVideo(server.url, accessTokenUser, videoAttributes)
214 it('Should have video quota updated', async function () {
215 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
216 const data = res.body
218 expect(data.videoQuotaUsed).to.equal(218910)
220 const resUsers = await getUsersList(server.url, server.accessToken)
222 const users: User[] = resUsers.body.data
223 const tmpUser = users.find(u => u.username === user.username)
224 expect(tmpUser.videoQuotaUsed).to.equal(218910)
227 it('Should be able to list my videos', async function () {
228 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
229 expect(res.body.total).to.equal(1)
231 const videos = res.body.data
232 expect(videos).to.have.lengthOf(1)
234 expect(videos[ 0 ].name).to.equal('super user video')
237 it('Should list all the users', async function () {
238 const res = await getUsersList(server.url, server.accessToken)
239 const result = res.body
240 const total = result.total
241 const users = result.data
243 expect(total).to.equal(2)
244 expect(users).to.be.an('array')
245 expect(users.length).to.equal(2)
247 const user = users[ 0 ]
248 expect(user.username).to.equal('user_1')
249 expect(user.email).to.equal('user_1@example.com')
250 expect(user.nsfwPolicy).to.equal('display')
252 const rootUser = users[ 1 ]
253 expect(rootUser.username).to.equal('root')
254 expect(rootUser.email).to.equal('admin1@example.com')
255 expect(user.nsfwPolicy).to.equal('display')
260 it('Should list only the first user by username asc', async function () {
261 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
263 const result = res.body
264 const total = result.total
265 const users = result.data
267 expect(total).to.equal(2)
268 expect(users.length).to.equal(1)
270 const user = users[ 0 ]
271 expect(user.username).to.equal('root')
272 expect(user.email).to.equal('admin1@example.com')
273 expect(user.roleLabel).to.equal('Administrator')
274 expect(user.nsfwPolicy).to.equal('display')
277 it('Should list only the first user by username desc', async function () {
278 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
279 const result = res.body
280 const total = result.total
281 const users = result.data
283 expect(total).to.equal(2)
284 expect(users.length).to.equal(1)
286 const user = users[ 0 ]
287 expect(user.username).to.equal('user_1')
288 expect(user.email).to.equal('user_1@example.com')
289 expect(user.nsfwPolicy).to.equal('display')
292 it('Should list only the second user by createdAt desc', async function () {
293 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
294 const result = res.body
295 const total = result.total
296 const users = result.data
298 expect(total).to.equal(2)
299 expect(users.length).to.equal(1)
301 const user = users[ 0 ]
302 expect(user.username).to.equal('user_1')
303 expect(user.email).to.equal('user_1@example.com')
304 expect(user.nsfwPolicy).to.equal('display')
307 it('Should list all the users by createdAt asc', async function () {
308 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
309 const result = res.body
310 const total = result.total
311 const users = result.data
313 expect(total).to.equal(2)
314 expect(users.length).to.equal(2)
316 expect(users[ 0 ].username).to.equal('root')
317 expect(users[ 0 ].email).to.equal('admin1@example.com')
318 expect(users[ 0 ].nsfwPolicy).to.equal('display')
320 expect(users[ 1 ].username).to.equal('user_1')
321 expect(users[ 1 ].email).to.equal('user_1@example.com')
322 expect(users[ 1 ].nsfwPolicy).to.equal('display')
325 it('Should search user by username', async function () {
326 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
327 const users = res.body.data as User[]
329 expect(res.body.total).to.equal(1)
330 expect(users.length).to.equal(1)
332 expect(users[ 0 ].username).to.equal('root')
335 it('Should search user by email', async function () {
337 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
338 const users = res.body.data as User[]
340 expect(res.body.total).to.equal(1)
341 expect(users.length).to.equal(1)
343 expect(users[ 0 ].username).to.equal('user_1')
344 expect(users[ 0 ].email).to.equal('user_1@example.com')
348 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
349 const users = res.body.data as User[]
351 expect(res.body.total).to.equal(2)
352 expect(users.length).to.equal(2)
354 expect(users[ 0 ].username).to.equal('root')
355 expect(users[ 1 ].username).to.equal('user_1')
359 it('Should update my password', async function () {
362 accessToken: accessTokenUser,
363 currentPassword: 'super password',
364 newPassword: 'new password'
366 user.password = 'new password'
368 await userLogin(server, user, 200)
371 it('Should be able to change the NSFW display attribute', async function () {
374 accessToken: accessTokenUser,
375 nsfwPolicy: 'do_not_list'
378 const res = await getMyUserInformation(server.url, accessTokenUser)
379 const user = res.body
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('user_1@example.com')
383 expect(user.nsfwPolicy).to.equal('do_not_list')
384 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
385 expect(user.id).to.be.a('number')
386 expect(user.account.displayName).to.equal('user_1')
387 expect(user.account.description).to.be.null
390 it('Should be able to change the autoPlayVideo attribute', async function () {
393 accessToken: accessTokenUser,
397 const res = await getMyUserInformation(server.url, accessTokenUser)
398 const user = res.body
400 expect(user.autoPlayVideo).to.be.false
403 it('Should be able to change the email display attribute', async function () {
406 accessToken: accessTokenUser,
407 email: 'updated@example.com'
410 const res = await getMyUserInformation(server.url, accessTokenUser)
411 const user = res.body
413 expect(user.username).to.equal('user_1')
414 expect(user.email).to.equal('updated@example.com')
415 expect(user.nsfwPolicy).to.equal('do_not_list')
416 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
417 expect(user.id).to.be.a('number')
418 expect(user.account.displayName).to.equal('user_1')
419 expect(user.account.description).to.be.null
422 it('Should be able to update my avatar', async function () {
423 const fixture = 'avatar.png'
425 await updateMyAvatar({
427 accessToken: accessTokenUser,
431 const res = await getMyUserInformation(server.url, accessTokenUser)
432 const user = res.body
434 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
437 it('Should be able to update my display name', async function () {
440 accessToken: accessTokenUser,
441 displayName: 'new display name'
444 const res = await getMyUserInformation(server.url, accessTokenUser)
445 const user = res.body
447 expect(user.username).to.equal('user_1')
448 expect(user.email).to.equal('updated@example.com')
449 expect(user.nsfwPolicy).to.equal('do_not_list')
450 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
451 expect(user.id).to.be.a('number')
452 expect(user.account.displayName).to.equal('new display name')
453 expect(user.account.description).to.be.null
456 it('Should be able to update my description', async function () {
459 accessToken: accessTokenUser,
460 description: 'my super description updated'
463 const res = await getMyUserInformation(server.url, accessTokenUser)
464 const user = res.body
466 expect(user.username).to.equal('user_1')
467 expect(user.email).to.equal('updated@example.com')
468 expect(user.nsfwPolicy).to.equal('do_not_list')
469 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
470 expect(user.id).to.be.a('number')
471 expect(user.account.displayName).to.equal('new display name')
472 expect(user.account.description).to.equal('my super description updated')
475 it('Should be able to update another user', async function () {
480 email: 'updated2@example.com',
483 role: UserRole.MODERATOR
486 const res = await getUserInformation(server.url, accessToken, userId)
487 const user = res.body
489 expect(user.username).to.equal('user_1')
490 expect(user.email).to.equal('updated2@example.com')
491 expect(user.emailVerified).to.be.true
492 expect(user.nsfwPolicy).to.equal('do_not_list')
493 expect(user.videoQuota).to.equal(42)
494 expect(user.roleLabel).to.equal('Moderator')
495 expect(user.id).to.be.a('number')
498 it('Should have removed the user token', async function () {
499 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
501 accessTokenUser = await userLogin(server, user)
504 it('Should be able to update another user password', async function () {
509 password: 'password updated'
512 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
514 await userLogin(server, user, 400)
516 user.password = 'password updated'
517 accessTokenUser = await userLogin(server, user)
520 it('Should be able to list video blacklist by a moderator', async function () {
521 await getBlacklistedVideosList(server.url, accessTokenUser)
524 it('Should be able to remove this user', async function () {
525 await removeUser(server.url, userId, accessToken)
528 it('Should not be able to login with this user', async function () {
529 await userLogin(server, user, 400)
532 it('Should not have videos of this user', async function () {
533 const res = await getVideosList(server.url)
535 expect(res.body.total).to.equal(1)
537 const video = res.body.data[ 0 ]
538 expect(video.account.name).to.equal('root')
541 it('Should register a new user', async function () {
542 await registerUser(server.url, 'user_15', 'my super password')
545 it('Should be able to login with this registered user', async function () {
548 password: 'my super password'
551 accessToken = await userLogin(server, user15)
554 it('Should have the correct video quota', async function () {
555 const res = await getMyUserInformation(server.url, accessToken)
556 const user = res.body
558 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
561 it('Should remove me', async function () {
563 const res = await getUsersList(server.url, server.accessToken)
564 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
567 await deleteMe(server.url, accessToken)
570 const res = await getUsersList(server.url, server.accessToken)
571 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
575 it('Should block and unblock a user', async function () {
578 password: 'my super password'
580 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
581 const user16Id = resUser.body.user.id
583 accessToken = await userLogin(server, user16)
585 await getMyUserInformation(server.url, accessToken, 200)
586 await blockUser(server.url, user16Id, server.accessToken)
588 await getMyUserInformation(server.url, accessToken, 401)
589 await userLogin(server, user16, 400)
591 await unblockUser(server.url, user16Id, server.accessToken)
592 accessToken = await userLogin(server, user16)
593 await getMyUserInformation(server.url, accessToken, 200)
596 after(async function () {
597 killallServers([ server ])
599 // Keep the logs if the test failed