1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { UserRole } from '../../../shared'
9 getBlacklistedVideosList,
13 getUsersListPaginationAndSort,
18 loginAndGetAccessToken,
30 import { follow } from '../utils/follows'
31 import { getMyVideos } from '../utils/videos'
32 import { setAccessTokensToServers } from '../utils/login'
34 const expect = chai.expect
36 describe('Test users', function () {
37 let server: ServerInfo
38 let accessToken: string
39 let accessTokenUser: string
43 before(async function () {
47 server = await runServer(1)
49 await setAccessTokensToServers([ server ])
52 it('Should create a new client')
54 it('Should return the first client')
56 it('Should remove the last client')
58 it('Should not login with an invalid client id', async function () {
59 const client = { id: 'client', secret: server.client.secret }
60 const res = await login(server.url, client, server.user, 400)
62 expect(res.body.error)
64 .equal('invalid_client')
67 it('Should not login with an invalid client secret', async function () {
68 const client = { id: server.client.id, secret: 'coucou' }
69 const res = await login(server.url, client, server.user, 400)
71 expect(res.body.error)
73 .equal('invalid_client')
76 it('Should not login with an invalid username', async function () {
77 const user = { username: 'captain crochet', password: server.user.password }
78 const res = await login(server.url, server.client, user, 400)
80 expect(res.body.error)
82 .equal('invalid_grant')
85 it('Should not login with an invalid password', async function () {
86 const user = { username: server.user.username, password: 'mew_three' }
87 const res = await login(server.url, server.client, user, 400)
89 expect(res.body.error)
91 .equal('invalid_grant')
94 it('Should not be able to upload a video', async function () {
95 accessToken = 'my_super_token'
97 const videoAttributes = {}
98 await uploadVideo(server.url, accessToken, videoAttributes, 401)
101 it('Should not be able to follow', async function () {
102 accessToken = 'my_super_token'
103 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
106 it('Should not be able to unfollow')
108 it('Should be able to login', async function () {
109 const res = await login(server.url, server.client, server.user, 200)
111 accessToken = res.body.access_token
114 it('Should upload the video with the correct token', async function () {
115 const videoAttributes = {}
116 await uploadVideo(server.url, accessToken, videoAttributes, 204)
117 const res = await getVideosList(server.url)
118 const video = res.body.data[ 0 ]
120 expect(video.accountName)
126 it('Should upload the video again with the correct token', async function () {
127 const videoAttributes = {}
128 await uploadVideo(server.url, accessToken, videoAttributes, 204)
131 it('Should retrieve a video rating', async function () {
132 await rateVideo(server.url, accessToken, videoId, 'like')
133 const res = await getUserVideoRating(server.url, accessToken, videoId)
134 const rating = res.body
136 expect(rating.videoId)
139 expect(rating.rating)
144 it('Should not be able to remove the video with an incorrect token', async function () {
145 await removeVideo(server.url, 'bad_token', videoId, 401)
148 it('Should not be able to remove the video with the token of another account')
150 it('Should be able to remove the video with the correct token', async function () {
151 await removeVideo(server.url, accessToken, videoId)
154 it('Should logout (revoke token)')
156 it('Should not be able to get the user information')
158 it('Should not be able to upload a video')
160 it('Should not be able to remove a video')
162 it('Should not be able to rate a video', async function () {
163 const path = '/api/v1/videos/'
170 path: path + videoId,
171 token: 'wrong token',
173 statusCodeExpected: 401
175 await makePutBodyRequest(options)
178 it('Should be able to login again')
180 it('Should have an expired access token')
182 it('Should refresh the token')
184 it('Should be able to upload a video again')
186 it('Should be able to create a new user', async function () {
187 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
190 it('Should be able to login with this user', async function () {
193 password: 'super password'
196 accessTokenUser = await loginAndGetAccessToken(server)
199 it('Should be able to get the user information', async function () {
200 const res = await getMyUserInformation(server.url, accessTokenUser)
201 const user = res.body
203 expect(user.username)
208 .equal('user_1@example.com')
209 expect(user.displayNSFW).to.be.false
210 expect(user.videoQuota)
212 .equal(2 * 1024 * 1024)
213 expect(user.roleLabel)
222 it('Should be able to upload a video with this user', async function () {
225 const videoAttributes = {
226 name: 'super user video'
228 await uploadVideo(server.url, accessTokenUser, videoAttributes)
231 it('Should be able to list my videos', async function () {
232 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
233 expect(res.body.total)
237 const videos = res.body.data
243 expect(videos[ 0 ].name)
245 .equal('super user video')
248 it('Should list all the users', async function () {
249 const res = await getUsersList(server.url, server.accessToken)
250 const result = res.body
251 const total = result.total
252 const users = result.data
265 const user = users[ 0 ]
266 expect(user.username)
271 .equal('user_1@example.com')
272 expect(user.displayNSFW).to.be.false
274 const rootUser = users[ 1 ]
275 expect(rootUser.username)
278 expect(rootUser.email)
280 .equal('admin1@example.com')
281 expect(rootUser.displayNSFW).to.be.false
286 it('Should list only the first user by username asc', async function () {
287 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
289 const result = res.body
290 const total = result.total
291 const users = result.data
300 const user = users[ 0 ]
301 expect(user.username)
306 .equal('admin1@example.com')
307 expect(user.roleLabel)
309 .equal('Administrator')
310 expect(user.displayNSFW).to.be.false
313 it('Should list only the first user by username desc', async function () {
314 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
315 const result = res.body
316 const total = result.total
317 const users = result.data
326 const user = users[ 0 ]
327 expect(user.username)
332 .equal('user_1@example.com')
333 expect(user.displayNSFW).to.be.false
336 it('Should list only the second user by createdAt desc', async function () {
337 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
338 const result = res.body
339 const total = result.total
340 const users = result.data
349 const user = users[ 0 ]
350 expect(user.username)
355 .equal('user_1@example.com')
356 expect(user.displayNSFW).to.be.false
359 it('Should list all the users by createdAt asc', async function () {
360 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
361 const result = res.body
362 const total = result.total
363 const users = result.data
372 expect(users[ 0 ].username)
375 expect(users[ 0 ].email)
377 .equal('admin1@example.com')
378 expect(users[ 0 ].displayNSFW).to.be.false
380 expect(users[ 1 ].username)
383 expect(users[ 1 ].email)
385 .equal('user_1@example.com')
386 expect(users[ 1 ].displayNSFW).to.be.false
389 it('Should update my password', async function () {
390 await updateMyUser(server.url, accessTokenUser, 'new password')
391 server.user.password = 'new password'
393 await login(server.url, server.client, server.user, 200)
396 it('Should be able to change the NSFW display attribute', async function () {
397 await updateMyUser(server.url, accessTokenUser, undefined, true)
399 const res = await getMyUserInformation(server.url, accessTokenUser)
400 const user = res.body
402 expect(user.username)
407 .equal('user_1@example.com')
408 expect(user.displayNSFW).to.be.ok
409 expect(user.videoQuota)
411 .equal(2 * 1024 * 1024)
418 it('Should be able to change the email display attribute', async function () {
419 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
421 const res = await getMyUserInformation(server.url, accessTokenUser)
422 const user = res.body
424 expect(user.username)
429 .equal('updated@example.com')
430 expect(user.displayNSFW).to.be.ok
431 expect(user.videoQuota)
433 .equal(2 * 1024 * 1024)
440 it('Should be able to update another user', async function () {
441 await updateUser(server.url, userId, accessToken, 'updated2@example.com', 42, UserRole.MODERATOR)
443 const res = await getUserInformation(server.url, accessToken, userId)
444 const user = res.body
446 expect(user.username)
451 .equal('updated2@example.com')
452 expect(user.displayNSFW).to.be.ok
453 expect(user.videoQuota)
456 expect(user.roleLabel)
465 it('Should not be able to delete a user by a moderator', async function () {
466 await removeUser(server.url, 2, accessTokenUser, 403)
469 it('Should be able to list video blacklist by a moderator', async function () {
470 await getBlacklistedVideosList(server.url, accessTokenUser)
473 it('Should be able to remove this user', async function () {
474 await removeUser(server.url, userId, accessToken)
477 it('Should not be able to login with this user', async function () {
478 // server.user is already set to user 1
479 await login(server.url, server.client, server.user, 400)
482 it('Should not have videos of this user', async function () {
483 const res = await getVideosList(server.url)
485 expect(res.body.total)
489 const video = res.body.data[ 0 ]
490 expect(video.accountName)
495 it('Should register a new user', async function () {
496 await registerUser(server.url, 'user_15', 'my super password')
499 it('Should be able to login with this registered user', async function () {
502 password: 'my super password'
505 accessToken = await loginAndGetAccessToken(server)
508 it('Should have the correct video quota', async function () {
509 const res = await getMyUserInformation(server.url, accessToken)
510 const user = res.body
512 expect(user.videoQuota)
514 .equal(5 * 1024 * 1024)
517 after(async function () {
518 killallServers([ server ])
520 // Keep the logs if the test failed