1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { User, UserRole } from '../../../../shared/index'
12 getBlacklistedVideosList,
14 getMyUserVideoQuotaUsed,
18 getUsersListPaginationAndSort,
36 } from '../../../../shared/utils'
37 import { follow } from '../../../../shared/utils/server/follows'
38 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
39 import { getMyVideos } from '../../../../shared/utils/videos/videos'
41 const expect = chai.expect
43 describe('Test users', function () {
44 let server: ServerInfo
45 let accessToken: string
46 let accessTokenUser: string
51 password: 'super password'
54 before(async function () {
58 server = await runServer(1)
60 await setAccessTokensToServers([ server ])
63 it('Should create a new client')
65 it('Should return the first client')
67 it('Should remove the last client')
69 it('Should not login with an invalid client id', async function () {
70 const client = { id: 'client', secret: server.client.secret }
71 const res = await login(server.url, client, server.user, 400)
73 expect(res.body.error).to.contain('client is invalid')
76 it('Should not login with an invalid client secret', async function () {
77 const client = { id: server.client.id, secret: 'coucou' }
78 const res = await login(server.url, client, server.user, 400)
80 expect(res.body.error).to.contain('client is invalid')
83 it('Should not login with an invalid username', async function () {
84 const user = { username: 'captain crochet', password: server.user.password }
85 const res = await login(server.url, server.client, user, 400)
87 expect(res.body.error).to.contain('credentials are invalid')
90 it('Should not login with an invalid password', async function () {
91 const user = { username: server.user.username, password: 'mew_three' }
92 const res = await login(server.url, server.client, user, 400)
94 expect(res.body.error).to.contain('credentials are invalid')
97 it('Should not be able to upload a video', async function () {
98 accessToken = 'my_super_token'
100 const videoAttributes = {}
101 await uploadVideo(server.url, accessToken, videoAttributes, 401)
104 it('Should not be able to follow', async function () {
105 accessToken = 'my_super_token'
106 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
109 it('Should not be able to unfollow')
111 it('Should be able to login', async function () {
112 const res = await login(server.url, server.client, server.user, 200)
114 accessToken = res.body.access_token
117 it('Should upload the video with the correct token', async function () {
118 const videoAttributes = {}
119 await uploadVideo(server.url, accessToken, videoAttributes)
120 const res = await getVideosList(server.url)
121 const video = res.body.data[ 0 ]
123 expect(video.account.name).to.equal('root')
127 it('Should upload the video again with the correct token', async function () {
128 const videoAttributes = {}
129 await uploadVideo(server.url, accessToken, videoAttributes)
132 it('Should retrieve a video rating', async function () {
133 await rateVideo(server.url, accessToken, videoId, 'like')
134 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
135 const rating = res.body
137 expect(rating.videoId).to.equal(videoId)
138 expect(rating.rating).to.equal('like')
141 it('Should retrieve ratings list', async function () {
142 await rateVideo(server.url, accessToken, videoId, 'like')
143 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 200)
144 const ratings = res.body
146 expect(ratings.data[0].video.id).to.equal(videoId)
147 expect(ratings.data[0].rating).to.equal('like')
150 it('Should retrieve ratings list by rating type', async function () {
151 await rateVideo(server.url, accessToken, videoId, 'like')
152 let res = await getAccountRatings(server.url, server.user.username, server.accessToken, 200, { rating: 'like' })
153 let ratings = res.body
154 expect(ratings.data.length).to.equal(1)
155 res = await getAccountRatings(server.url, server.user.username, server.accessToken, 200, { rating: 'dislike' })
157 expect(ratings.data.length).to.equal(0)
158 await getAccountRatings(server.url, server.user.username, server.accessToken, 400, { rating: 'invalid' })
161 it('Should not access ratings list if not logged with correct user', async function () {
162 const user = { username: 'anuragh', password: 'passbyme' }
163 const resUser = await createUser(server.url, server.accessToken, user.username, user.password)
164 const userId = resUser.body.user.id
165 const userAccessToken = await userLogin(server, user)
166 await getAccountRatings(server.url, server.user.username, userAccessToken, 403)
167 await removeUser(server.url, userId, server.accessToken)
170 it('Should not be able to remove the video with an incorrect token', async function () {
171 await removeVideo(server.url, 'bad_token', videoId, 401)
174 it('Should not be able to remove the video with the token of another account')
176 it('Should be able to remove the video with the correct token', async function () {
177 await removeVideo(server.url, accessToken, videoId)
180 it('Should logout (revoke token)')
182 it('Should not be able to get the user information')
184 it('Should not be able to upload a video')
186 it('Should not be able to remove a video')
188 it('Should not be able to rate a video', async function () {
189 const path = '/api/v1/videos/'
196 path: path + videoId,
197 token: 'wrong token',
199 statusCodeExpected: 401
201 await makePutBodyRequest(options)
204 it('Should be able to login again')
206 it('Should have an expired access token')
208 it('Should refresh the token')
210 it('Should be able to upload a video again')
212 it('Should be able to create a new user', async function () {
213 await createUser(server.url, accessToken, user.username, user.password, 2 * 1024 * 1024)
216 it('Should be able to login with this user', async function () {
217 accessTokenUser = await userLogin(server, user)
220 it('Should be able to get the user information', async function () {
221 const res = await getMyUserInformation(server.url, accessTokenUser)
222 const user = res.body
224 expect(user.username).to.equal('user_1')
225 expect(user.email).to.equal('user_1@example.com')
226 expect(user.nsfwPolicy).to.equal('display')
227 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
228 expect(user.roleLabel).to.equal('User')
229 expect(user.id).to.be.a('number')
230 expect(user.account.displayName).to.equal('user_1')
231 expect(user.account.description).to.be.null
234 it('Should be able to upload a video with this user', async function () {
237 const videoAttributes = {
238 name: 'super user video',
239 fixture: 'video_short.webm'
241 await uploadVideo(server.url, accessTokenUser, videoAttributes)
244 it('Should have video quota updated', async function () {
245 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
246 const data = res.body
248 expect(data.videoQuotaUsed).to.equal(218910)
250 const resUsers = await getUsersList(server.url, server.accessToken)
252 const users: User[] = resUsers.body.data
253 const tmpUser = users.find(u => u.username === user.username)
254 expect(tmpUser.videoQuotaUsed).to.equal(218910)
257 it('Should be able to list my videos', async function () {
258 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
259 expect(res.body.total).to.equal(1)
261 const videos = res.body.data
262 expect(videos).to.have.lengthOf(1)
264 expect(videos[ 0 ].name).to.equal('super user video')
267 it('Should list all the users', async function () {
268 const res = await getUsersList(server.url, server.accessToken)
269 const result = res.body
270 const total = result.total
271 const users = result.data
273 expect(total).to.equal(2)
274 expect(users).to.be.an('array')
275 expect(users.length).to.equal(2)
277 const user = users[ 0 ]
278 expect(user.username).to.equal('user_1')
279 expect(user.email).to.equal('user_1@example.com')
280 expect(user.nsfwPolicy).to.equal('display')
282 const rootUser = users[ 1 ]
283 expect(rootUser.username).to.equal('root')
284 expect(rootUser.email).to.equal('admin1@example.com')
285 expect(user.nsfwPolicy).to.equal('display')
290 it('Should list only the first user by username asc', async function () {
291 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
293 const result = res.body
294 const total = result.total
295 const users = result.data
297 expect(total).to.equal(2)
298 expect(users.length).to.equal(1)
300 const user = users[ 0 ]
301 expect(user.username).to.equal('root')
302 expect(user.email).to.equal('admin1@example.com')
303 expect(user.roleLabel).to.equal('Administrator')
304 expect(user.nsfwPolicy).to.equal('display')
307 it('Should list only the first user by username desc', async function () {
308 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
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(1)
316 const user = users[ 0 ]
317 expect(user.username).to.equal('user_1')
318 expect(user.email).to.equal('user_1@example.com')
319 expect(user.nsfwPolicy).to.equal('display')
322 it('Should list only the second user by createdAt desc', async function () {
323 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
324 const result = res.body
325 const total = result.total
326 const users = result.data
328 expect(total).to.equal(2)
329 expect(users.length).to.equal(1)
331 const user = users[ 0 ]
332 expect(user.username).to.equal('user_1')
333 expect(user.email).to.equal('user_1@example.com')
334 expect(user.nsfwPolicy).to.equal('display')
337 it('Should list all the users by createdAt asc', async function () {
338 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
339 const result = res.body
340 const total = result.total
341 const users = result.data
343 expect(total).to.equal(2)
344 expect(users.length).to.equal(2)
346 expect(users[ 0 ].username).to.equal('root')
347 expect(users[ 0 ].email).to.equal('admin1@example.com')
348 expect(users[ 0 ].nsfwPolicy).to.equal('display')
350 expect(users[ 1 ].username).to.equal('user_1')
351 expect(users[ 1 ].email).to.equal('user_1@example.com')
352 expect(users[ 1 ].nsfwPolicy).to.equal('display')
355 it('Should search user by username', async function () {
356 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
357 const users = res.body.data as User[]
359 expect(res.body.total).to.equal(1)
360 expect(users.length).to.equal(1)
362 expect(users[ 0 ].username).to.equal('root')
365 it('Should search user by email', async function () {
367 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
368 const users = res.body.data as User[]
370 expect(res.body.total).to.equal(1)
371 expect(users.length).to.equal(1)
373 expect(users[ 0 ].username).to.equal('user_1')
374 expect(users[ 0 ].email).to.equal('user_1@example.com')
378 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
379 const users = res.body.data as User[]
381 expect(res.body.total).to.equal(2)
382 expect(users.length).to.equal(2)
384 expect(users[ 0 ].username).to.equal('root')
385 expect(users[ 1 ].username).to.equal('user_1')
389 it('Should update my password', async function () {
392 accessToken: accessTokenUser,
393 currentPassword: 'super password',
394 newPassword: 'new password'
396 user.password = 'new password'
398 await userLogin(server, user, 200)
401 it('Should be able to change the NSFW display attribute', async function () {
404 accessToken: accessTokenUser,
405 nsfwPolicy: 'do_not_list'
408 const res = await getMyUserInformation(server.url, accessTokenUser)
409 const user = res.body
411 expect(user.username).to.equal('user_1')
412 expect(user.email).to.equal('user_1@example.com')
413 expect(user.nsfwPolicy).to.equal('do_not_list')
414 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
415 expect(user.id).to.be.a('number')
416 expect(user.account.displayName).to.equal('user_1')
417 expect(user.account.description).to.be.null
420 it('Should be able to change the autoPlayVideo attribute', async function () {
423 accessToken: accessTokenUser,
427 const res = await getMyUserInformation(server.url, accessTokenUser)
428 const user = res.body
430 expect(user.autoPlayVideo).to.be.false
433 it('Should be able to change the email display attribute', async function () {
436 accessToken: accessTokenUser,
437 email: 'updated@example.com'
440 const res = await getMyUserInformation(server.url, accessTokenUser)
441 const user = res.body
443 expect(user.username).to.equal('user_1')
444 expect(user.email).to.equal('updated@example.com')
445 expect(user.nsfwPolicy).to.equal('do_not_list')
446 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
447 expect(user.id).to.be.a('number')
448 expect(user.account.displayName).to.equal('user_1')
449 expect(user.account.description).to.be.null
452 it('Should be able to update my avatar', async function () {
453 const fixture = 'avatar.png'
455 await updateMyAvatar({
457 accessToken: accessTokenUser,
461 const res = await getMyUserInformation(server.url, accessTokenUser)
462 const user = res.body
464 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
467 it('Should be able to update my display name', async function () {
470 accessToken: accessTokenUser,
471 displayName: 'new display name'
474 const res = await getMyUserInformation(server.url, accessTokenUser)
475 const user = res.body
477 expect(user.username).to.equal('user_1')
478 expect(user.email).to.equal('updated@example.com')
479 expect(user.nsfwPolicy).to.equal('do_not_list')
480 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
481 expect(user.id).to.be.a('number')
482 expect(user.account.displayName).to.equal('new display name')
483 expect(user.account.description).to.be.null
486 it('Should be able to update my description', async function () {
489 accessToken: accessTokenUser,
490 description: 'my super description updated'
493 const res = await getMyUserInformation(server.url, accessTokenUser)
494 const user = res.body
496 expect(user.username).to.equal('user_1')
497 expect(user.email).to.equal('updated@example.com')
498 expect(user.nsfwPolicy).to.equal('do_not_list')
499 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
500 expect(user.id).to.be.a('number')
501 expect(user.account.displayName).to.equal('new display name')
502 expect(user.account.description).to.equal('my super description updated')
505 it('Should be able to update another user', async function () {
510 email: 'updated2@example.com',
513 role: UserRole.MODERATOR
516 const res = await getUserInformation(server.url, accessToken, userId)
517 const user = res.body
519 expect(user.username).to.equal('user_1')
520 expect(user.email).to.equal('updated2@example.com')
521 expect(user.emailVerified).to.be.true
522 expect(user.nsfwPolicy).to.equal('do_not_list')
523 expect(user.videoQuota).to.equal(42)
524 expect(user.roleLabel).to.equal('Moderator')
525 expect(user.id).to.be.a('number')
528 it('Should have removed the user token', async function () {
529 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
531 accessTokenUser = await userLogin(server, user)
534 it('Should be able to update another user password', async function () {
539 password: 'password updated'
542 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
544 await userLogin(server, user, 400)
546 user.password = 'password updated'
547 accessTokenUser = await userLogin(server, user)
550 it('Should be able to list video blacklist by a moderator', async function () {
551 await getBlacklistedVideosList(server.url, accessTokenUser)
554 it('Should be able to remove this user', async function () {
555 await removeUser(server.url, userId, accessToken)
558 it('Should not be able to login with this user', async function () {
559 await userLogin(server, user, 400)
562 it('Should not have videos of this user', async function () {
563 const res = await getVideosList(server.url)
565 expect(res.body.total).to.equal(1)
567 const video = res.body.data[ 0 ]
568 expect(video.account.name).to.equal('root')
571 it('Should register a new user', async function () {
572 await registerUser(server.url, 'user_15', 'my super password')
575 it('Should be able to login with this registered user', async function () {
578 password: 'my super password'
581 accessToken = await userLogin(server, user15)
584 it('Should have the correct video quota', async function () {
585 const res = await getMyUserInformation(server.url, accessToken)
586 const user = res.body
588 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
591 it('Should remove me', async function () {
593 const res = await getUsersList(server.url, server.accessToken)
594 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
597 await deleteMe(server.url, accessToken)
600 const res = await getUsersList(server.url, server.accessToken)
601 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
605 it('Should block and unblock a user', async function () {
608 password: 'my super password'
610 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
611 const user16Id = resUser.body.user.id
613 accessToken = await userLogin(server, user16)
615 await getMyUserInformation(server.url, accessToken, 200)
616 await blockUser(server.url, user16Id, server.accessToken)
618 await getMyUserInformation(server.url, accessToken, 401)
619 await userLogin(server, user16, 400)
621 await unblockUser(server.url, user16Id, server.accessToken)
622 accessToken = await userLogin(server, user16)
623 await getMyUserInformation(server.url, accessToken, 200)
626 after(async function () {
627 killallServers([ server ])
629 // Keep the logs if the test failed