1 /* tslint:disable:no-unused-expression */
2 import * as chai from 'chai'
4 import { UserRole } from '../../../shared'
8 getBlacklistedVideosList,
12 getUsersListPaginationAndSort,
17 loginAndGetAccessToken,
29 import { follow } from '../utils/follows'
30 import { getMyVideos } from '../utils/videos'
32 const expect = chai.expect
34 describe('Test users', function () {
35 let server: ServerInfo
36 let accessToken: string
37 let accessTokenUser: string
41 before(async function () {
45 server = await runServer(1)
48 it('Should create a new client')
50 it('Should return the first client')
52 it('Should remove the last client')
54 it('Should not login with an invalid client id', async function () {
55 const client = { id: 'client', secret: server.client.secret }
56 const res = await login(server.url, client, server.user, 400)
58 expect(res.body.error)
60 .equal('invalid_client')
63 it('Should not login with an invalid client secret', async function () {
64 const client = { id: server.client.id, secret: 'coucou' }
65 const res = await login(server.url, client, server.user, 400)
67 expect(res.body.error)
69 .equal('invalid_client')
72 it('Should not login with an invalid username', async function () {
73 const user = { username: 'captain crochet', password: server.user.password }
74 const res = await login(server.url, server.client, user, 400)
76 expect(res.body.error)
78 .equal('invalid_grant')
81 it('Should not login with an invalid password', async function () {
82 const user = { username: server.user.username, password: 'mew_three' }
83 const res = await login(server.url, server.client, user, 400)
85 expect(res.body.error)
87 .equal('invalid_grant')
90 it('Should not be able to upload a video', async function () {
91 accessToken = 'my_super_token'
93 const videoAttributes = {}
94 await uploadVideo(server.url, accessToken, videoAttributes, 401)
97 it('Should not be able to follow', async function () {
98 accessToken = 'my_super_token'
99 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
102 it('Should not be able to unfollow')
104 it('Should be able to login', async function () {
105 const res = await login(server.url, server.client, server.user, 200)
107 accessToken = res.body.access_token
110 it('Should upload the video with the correct token', async function () {
111 const videoAttributes = {}
112 await uploadVideo(server.url, accessToken, videoAttributes, 204)
113 const res = await getVideosList(server.url)
114 const video = res.body.data[ 0 ]
116 expect(video.account)
122 it('Should upload the video again with the correct token', async function () {
123 const videoAttributes = {}
124 await uploadVideo(server.url, accessToken, videoAttributes, 204)
127 it('Should retrieve a video rating', async function () {
128 await rateVideo(server.url, accessToken, videoId, 'like')
129 const res = await getUserVideoRating(server.url, accessToken, videoId)
130 const rating = res.body
132 expect(rating.videoId)
135 expect(rating.rating)
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_1', 'super password', 2 * 1024 * 1024)
186 it('Should be able to login with this user', async function () {
189 password: 'super password'
192 accessTokenUser = await loginAndGetAccessToken(server)
195 it('Should be able to get the user information', async function () {
196 const res = await getMyUserInformation(server.url, accessTokenUser)
197 const user = res.body
199 expect(user.username)
204 .equal('user_1@example.com')
205 expect(user.displayNSFW).to.be.false
206 expect(user.videoQuota)
208 .equal(2 * 1024 * 1024)
209 expect(user.roleLabel)
218 it('Should be able to upload a video with this user', async function () {
221 const videoAttributes = {
222 name: 'super user video'
224 await uploadVideo(server.url, accessTokenUser, videoAttributes)
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)
233 const videos = res.body.data
239 expect(videos[ 0 ].name)
241 .equal('super user video')
244 it('Should list all the users', async function () {
245 const res = await getUsersList(server.url)
246 const result = res.body
247 const total = result.total
248 const users = result.data
261 const user = users[ 0 ]
262 expect(user.username)
267 .equal('user_1@example.com')
268 expect(user.displayNSFW).to.be.false
270 const rootUser = users[ 1 ]
271 expect(rootUser.username)
274 expect(rootUser.email)
276 .equal('admin1@example.com')
277 expect(rootUser.displayNSFW).to.be.false
282 it('Should list only the first user by username asc', async function () {
283 const res = await getUsersListPaginationAndSort(server.url, 0, 1, 'username')
285 const result = res.body
286 const total = result.total
287 const users = result.data
296 const user = users[ 0 ]
297 expect(user.username)
302 .equal('admin1@example.com')
303 expect(user.roleLabel)
305 .equal('Administrator')
306 expect(user.displayNSFW).to.be.false
309 it('Should list only the first user by username desc', async function () {
310 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-username')
311 const result = res.body
312 const total = result.total
313 const users = result.data
322 const user = users[ 0 ]
323 expect(user.username)
328 .equal('user_1@example.com')
329 expect(user.displayNSFW).to.be.false
332 it('Should list only the second user by createdAt desc', async function () {
333 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt')
334 const result = res.body
335 const total = result.total
336 const users = result.data
345 const user = users[ 0 ]
346 expect(user.username)
351 .equal('user_1@example.com')
352 expect(user.displayNSFW).to.be.false
355 it('Should list all the users by createdAt asc', async function () {
356 const res = await getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt')
357 const result = res.body
358 const total = result.total
359 const users = result.data
368 expect(users[ 0 ].username)
371 expect(users[ 0 ].email)
373 .equal('admin1@example.com')
374 expect(users[ 0 ].displayNSFW).to.be.false
376 expect(users[ 1 ].username)
379 expect(users[ 1 ].email)
381 .equal('user_1@example.com')
382 expect(users[ 1 ].displayNSFW).to.be.false
385 it('Should update my password', async function () {
386 await updateMyUser(server.url, accessTokenUser, 'new password')
387 server.user.password = 'new password'
389 await login(server.url, server.client, server.user, 200)
392 it('Should be able to change the NSFW display attribute', async function () {
393 await updateMyUser(server.url, accessTokenUser, undefined, true)
395 const res = await getMyUserInformation(server.url, accessTokenUser)
396 const user = res.body
398 expect(user.username)
403 .equal('user_1@example.com')
404 expect(user.displayNSFW).to.be.ok
405 expect(user.videoQuota)
407 .equal(2 * 1024 * 1024)
414 it('Should be able to change the email display attribute', async function () {
415 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
417 const res = await getMyUserInformation(server.url, accessTokenUser)
418 const user = res.body
420 expect(user.username)
425 .equal('updated@example.com')
426 expect(user.displayNSFW).to.be.ok
427 expect(user.videoQuota)
429 .equal(2 * 1024 * 1024)
436 it('Should be able to update another user', async function () {
437 await updateUser(server.url, userId, accessToken, 'updated2@example.com', 42, UserRole.MODERATOR)
439 const res = await getUserInformation(server.url, accessToken, userId)
440 const user = res.body
442 expect(user.username)
447 .equal('updated2@example.com')
448 expect(user.displayNSFW).to.be.ok
449 expect(user.videoQuota)
452 expect(user.roleLabel)
461 it('Should not be able to delete a user by a moderator', async function () {
462 await removeUser(server.url, 2, accessTokenUser, 403)
465 it('Should be able to list video blacklist by a moderator', async function () {
466 await getBlacklistedVideosList(server.url, accessTokenUser)
469 it('Should be able to remove this user', async function () {
470 await removeUser(server.url, userId, accessToken)
473 it('Should not be able to login with this user', async function () {
474 // server.user is already set to user 1
475 await login(server.url, server.client, server.user, 400)
478 it('Should not have videos of this user', async function () {
479 const res = await getVideosList(server.url)
481 expect(res.body.total)
485 const video = res.body.data[ 0 ]
486 expect(video.account)
491 it('Should register a new user', async function () {
492 await registerUser(server.url, 'user_15', 'my super password')
495 it('Should be able to login with this registered user', async function () {
498 password: 'my super password'
501 accessToken = await loginAndGetAccessToken(server)
504 it('Should have the correct video quota', async function () {
505 const res = await getMyUserInformation(server.url, accessToken)
506 const user = res.body
508 expect(user.videoQuota)
510 .equal(5 * 1024 * 1024)
513 after(async function () {
514 killallServers([ server ])
516 // Keep the logs if the test failed