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 '../../utils/index'
36 import { follow } from '../../utils/server/follows'
37 import { setAccessTokensToServers } from '../../utils/users/login'
38 import { getMyVideos } from '../../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 update my password', async function () {
328 accessToken: accessTokenUser,
329 currentPassword: 'super password',
330 newPassword: 'new password'
332 user.password = 'new password'
334 await userLogin(server, user, 200)
337 it('Should be able to change the NSFW display attribute', async function () {
340 accessToken: accessTokenUser,
341 nsfwPolicy: 'do_not_list'
344 const res = await getMyUserInformation(server.url, accessTokenUser)
345 const user = res.body
347 expect(user.username).to.equal('user_1')
348 expect(user.email).to.equal('user_1@example.com')
349 expect(user.nsfwPolicy).to.equal('do_not_list')
350 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
351 expect(user.id).to.be.a('number')
352 expect(user.account.displayName).to.equal('user_1')
353 expect(user.account.description).to.be.null
356 it('Should be able to change the autoPlayVideo attribute', async function () {
359 accessToken: accessTokenUser,
363 const res = await getMyUserInformation(server.url, accessTokenUser)
364 const user = res.body
366 expect(user.autoPlayVideo).to.be.false
369 it('Should be able to change the email display attribute', async function () {
372 accessToken: accessTokenUser,
373 email: 'updated@example.com'
376 const res = await getMyUserInformation(server.url, accessTokenUser)
377 const user = res.body
379 expect(user.username).to.equal('user_1')
380 expect(user.email).to.equal('updated@example.com')
381 expect(user.nsfwPolicy).to.equal('do_not_list')
382 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
383 expect(user.id).to.be.a('number')
384 expect(user.account.displayName).to.equal('user_1')
385 expect(user.account.description).to.be.null
388 it('Should be able to update my avatar', async function () {
389 const fixture = 'avatar.png'
391 await updateMyAvatar({
393 accessToken: accessTokenUser,
397 const res = await getMyUserInformation(server.url, accessTokenUser)
398 const user = res.body
400 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
403 it('Should be able to update my display name', async function () {
406 accessToken: accessTokenUser,
407 displayName: 'new display name'
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('new display name')
419 expect(user.account.description).to.be.null
422 it('Should be able to update my description', async function () {
425 accessToken: accessTokenUser,
426 description: 'my super description updated'
429 const res = await getMyUserInformation(server.url, accessTokenUser)
430 const user = res.body
432 expect(user.username).to.equal('user_1')
433 expect(user.email).to.equal('updated@example.com')
434 expect(user.nsfwPolicy).to.equal('do_not_list')
435 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
436 expect(user.id).to.be.a('number')
437 expect(user.account.displayName).to.equal('new display name')
438 expect(user.account.description).to.equal('my super description updated')
441 it('Should be able to update another user', async function () {
446 email: 'updated2@example.com',
448 role: UserRole.MODERATOR
451 const res = await getUserInformation(server.url, accessToken, userId)
452 const user = res.body
454 expect(user.username).to.equal('user_1')
455 expect(user.email).to.equal('updated2@example.com')
456 expect(user.nsfwPolicy).to.equal('do_not_list')
457 expect(user.videoQuota).to.equal(42)
458 expect(user.roleLabel).to.equal('Moderator')
459 expect(user.id).to.be.a('number')
462 it('Should have removed the user token', async function () {
463 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
465 accessTokenUser = await userLogin(server, user)
468 it('Should not be able to delete a user by a moderator', async function () {
469 await removeUser(server.url, 2, accessTokenUser, 403)
472 it('Should be able to list video blacklist by a moderator', async function () {
473 await getBlacklistedVideosList(server.url, accessTokenUser)
476 it('Should be able to remove this user', async function () {
477 await removeUser(server.url, userId, accessToken)
480 it('Should not be able to login with this user', async function () {
481 await userLogin(server, user, 400)
484 it('Should not have videos of this user', async function () {
485 const res = await getVideosList(server.url)
487 expect(res.body.total).to.equal(1)
489 const video = res.body.data[ 0 ]
490 expect(video.account.name).to.equal('root')
493 it('Should register a new user', async function () {
494 await registerUser(server.url, 'user_15', 'my super password')
497 it('Should be able to login with this registered user', async function () {
500 password: 'my super password'
503 accessToken = await userLogin(server, user15)
506 it('Should have the correct video quota', async function () {
507 const res = await getMyUserInformation(server.url, accessToken)
508 const user = res.body
510 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
513 it('Should remove me', async function () {
515 const res = await getUsersList(server.url, server.accessToken)
516 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
519 await deleteMe(server.url, accessToken)
522 const res = await getUsersList(server.url, server.accessToken)
523 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
527 it('Should block and unblock a user', async function () {
530 password: 'my super password'
532 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
533 const user16Id = resUser.body.user.id
535 accessToken = await userLogin(server, user16)
537 await getMyUserInformation(server.url, accessToken, 200)
538 await blockUser(server.url, user16Id, server.accessToken)
540 await getMyUserInformation(server.url, accessToken, 401)
541 await userLogin(server, user16, 400)
543 await unblockUser(server.url, user16Id, server.accessToken)
544 accessToken = await userLogin(server, user16)
545 await getMyUserInformation(server.url, accessToken, 200)
548 after(async function () {
549 killallServers([ server ])
551 // Keep the logs if the test failed