1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { User, UserRole } from '../../../../shared/index'
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating,
8 getUserInformation, getUsersList, getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo,
9 registerUser, removeUser, removeVideo, runServer, ServerInfo, testImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo, userLogin,
10 deleteMe, blockUser, unblockUser
11 } from '../../utils/index'
12 import { follow } from '../../utils/server/follows'
13 import { setAccessTokensToServers } from '../../utils/users/login'
14 import { getMyVideos } from '../../utils/videos/videos'
16 const expect = chai.expect
18 describe('Test users', function () {
19 let server: ServerInfo
20 let accessToken: string
21 let accessTokenUser: string
26 password: 'super password'
29 before(async function () {
33 server = await runServer(1)
35 await setAccessTokensToServers([ server ])
38 it('Should create a new client')
40 it('Should return the first client')
42 it('Should remove the last client')
44 it('Should not login with an invalid client id', async function () {
45 const client = { id: 'client', secret: server.client.secret }
46 const res = await login(server.url, client, server.user, 400)
48 expect(res.body.error).to.contain('client is invalid')
51 it('Should not login with an invalid client secret', async function () {
52 const client = { id: server.client.id, secret: 'coucou' }
53 const res = await login(server.url, client, server.user, 400)
55 expect(res.body.error).to.contain('client is invalid')
58 it('Should not login with an invalid username', async function () {
59 const user = { username: 'captain crochet', password: server.user.password }
60 const res = await login(server.url, server.client, user, 400)
62 expect(res.body.error).to.contain('credentials are invalid')
65 it('Should not login with an invalid password', async function () {
66 const user = { username: server.user.username, password: 'mew_three' }
67 const res = await login(server.url, server.client, user, 400)
69 expect(res.body.error).to.contain('credentials are invalid')
72 it('Should not be able to upload a video', async function () {
73 accessToken = 'my_super_token'
75 const videoAttributes = {}
76 await uploadVideo(server.url, accessToken, videoAttributes, 401)
79 it('Should not be able to follow', async function () {
80 accessToken = 'my_super_token'
81 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
84 it('Should not be able to unfollow')
86 it('Should be able to login', async function () {
87 const res = await login(server.url, server.client, server.user, 200)
89 accessToken = res.body.access_token
92 it('Should upload the video with the correct token', async function () {
93 const videoAttributes = {}
94 await uploadVideo(server.url, accessToken, videoAttributes)
95 const res = await getVideosList(server.url)
96 const video = res.body.data[ 0 ]
98 expect(video.account.name).to.equal('root')
102 it('Should upload the video again with the correct token', async function () {
103 const videoAttributes = {}
104 await uploadVideo(server.url, accessToken, videoAttributes)
107 it('Should retrieve a video rating', async function () {
108 await rateVideo(server.url, accessToken, videoId, 'like')
109 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
110 const rating = res.body
112 expect(rating.videoId).to.equal(videoId)
113 expect(rating.rating).to.equal('like')
116 it('Should not be able to remove the video with an incorrect token', async function () {
117 await removeVideo(server.url, 'bad_token', videoId, 401)
120 it('Should not be able to remove the video with the token of another account')
122 it('Should be able to remove the video with the correct token', async function () {
123 await removeVideo(server.url, accessToken, videoId)
126 it('Should logout (revoke token)')
128 it('Should not be able to get the user information')
130 it('Should not be able to upload a video')
132 it('Should not be able to remove a video')
134 it('Should not be able to rate a video', async function () {
135 const path = '/api/v1/videos/'
142 path: path + videoId,
143 token: 'wrong token',
145 statusCodeExpected: 401
147 await makePutBodyRequest(options)
150 it('Should be able to login again')
152 it('Should have an expired access token')
154 it('Should refresh the token')
156 it('Should be able to upload a video again')
158 it('Should be able to create a new user', async function () {
159 await createUser(server.url, accessToken, user.username,user.password, 2 * 1024 * 1024)
162 it('Should be able to login with this user', async function () {
163 accessTokenUser = await userLogin(server, user)
166 it('Should be able to get the user information', async function () {
167 const res = await getMyUserInformation(server.url, accessTokenUser)
168 const user = res.body
170 expect(user.username).to.equal('user_1')
171 expect(user.email).to.equal('user_1@example.com')
172 expect(user.nsfwPolicy).to.equal('display')
173 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
174 expect(user.roleLabel).to.equal('User')
175 expect(user.id).to.be.a('number')
176 expect(user.account.displayName).to.equal('user_1')
177 expect(user.account.description).to.be.null
180 it('Should be able to upload a video with this user', async function () {
183 const videoAttributes = {
184 name: 'super user video',
185 fixture: 'video_short.webm'
187 await uploadVideo(server.url, accessTokenUser, videoAttributes)
190 it('Should have video quota updated', async function () {
191 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
192 const data = res.body
194 expect(data.videoQuotaUsed).to.equal(218910)
196 const resUsers = await getUsersList(server.url, server.accessToken)
198 const users: User[] = resUsers.body.data
199 const tmpUser = users.find(u => u.username === user.username)
200 expect(tmpUser.videoQuotaUsed).to.equal(218910)
203 it('Should be able to list my videos', async function () {
204 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
205 expect(res.body.total).to.equal(1)
207 const videos = res.body.data
208 expect(videos).to.have.lengthOf(1)
210 expect(videos[ 0 ].name).to.equal('super user video')
213 it('Should list all the users', async function () {
214 const res = await getUsersList(server.url, server.accessToken)
215 const result = res.body
216 const total = result.total
217 const users = result.data
219 expect(total).to.equal(2)
220 expect(users).to.be.an('array')
221 expect(users.length).to.equal(2)
223 const user = users[ 0 ]
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')
228 const rootUser = users[ 1 ]
229 expect(rootUser.username).to.equal('root')
230 expect(rootUser.email).to.equal('admin1@example.com')
231 expect(user.nsfwPolicy).to.equal('display')
236 it('Should list only the first user by username asc', async function () {
237 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
239 const result = res.body
240 const total = result.total
241 const users = result.data
243 expect(total).to.equal(2)
244 expect(users.length).to.equal(1)
246 const user = users[ 0 ]
247 expect(user.username).to.equal('root')
248 expect(user.email).to.equal('admin1@example.com')
249 expect(user.roleLabel).to.equal('Administrator')
250 expect(user.nsfwPolicy).to.equal('display')
253 it('Should list only the first user by username desc', async function () {
254 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
255 const result = res.body
256 const total = result.total
257 const users = result.data
259 expect(total).to.equal(2)
260 expect(users.length).to.equal(1)
262 const user = users[ 0 ]
263 expect(user.username).to.equal('user_1')
264 expect(user.email).to.equal('user_1@example.com')
265 expect(user.nsfwPolicy).to.equal('display')
268 it('Should list only the second user by createdAt desc', async function () {
269 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
270 const result = res.body
271 const total = result.total
272 const users = result.data
274 expect(total).to.equal(2)
275 expect(users.length).to.equal(1)
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')
283 it('Should list all the users by createdAt asc', async function () {
284 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
285 const result = res.body
286 const total = result.total
287 const users = result.data
289 expect(total).to.equal(2)
290 expect(users.length).to.equal(2)
292 expect(users[ 0 ].username).to.equal('root')
293 expect(users[ 0 ].email).to.equal('admin1@example.com')
294 expect(users[ 0 ].nsfwPolicy).to.equal('display')
296 expect(users[ 1 ].username).to.equal('user_1')
297 expect(users[ 1 ].email).to.equal('user_1@example.com')
298 expect(users[ 1 ].nsfwPolicy).to.equal('display')
301 it('Should update my password', async function () {
304 accessToken: accessTokenUser,
305 newPassword: 'new password'
307 user.password = 'new password'
309 await userLogin(server, user, 200)
312 it('Should be able to change the NSFW display attribute', async function () {
315 accessToken: accessTokenUser,
316 nsfwPolicy: 'do_not_list'
319 const res = await getMyUserInformation(server.url, accessTokenUser)
320 const user = res.body
322 expect(user.username).to.equal('user_1')
323 expect(user.email).to.equal('user_1@example.com')
324 expect(user.nsfwPolicy).to.equal('do_not_list')
325 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
326 expect(user.id).to.be.a('number')
327 expect(user.account.displayName).to.equal('user_1')
328 expect(user.account.description).to.be.null
331 it('Should be able to change the autoPlayVideo attribute', async function () {
334 accessToken: accessTokenUser,
338 const res = await getMyUserInformation(server.url, accessTokenUser)
339 const user = res.body
341 expect(user.autoPlayVideo).to.be.false
344 it('Should be able to change the email display attribute', async function () {
347 accessToken: accessTokenUser,
348 email: 'updated@example.com'
351 const res = await getMyUserInformation(server.url, accessTokenUser)
352 const user = res.body
354 expect(user.username).to.equal('user_1')
355 expect(user.email).to.equal('updated@example.com')
356 expect(user.nsfwPolicy).to.equal('do_not_list')
357 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
358 expect(user.id).to.be.a('number')
359 expect(user.account.displayName).to.equal('user_1')
360 expect(user.account.description).to.be.null
363 it('Should be able to update my avatar', async function () {
364 const fixture = 'avatar.png'
366 await updateMyAvatar({
368 accessToken: accessTokenUser,
372 const res = await getMyUserInformation(server.url, accessTokenUser)
373 const user = res.body
375 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
378 it('Should be able to update my display name', async function () {
381 accessToken: accessTokenUser,
382 displayName: 'new display name'
385 const res = await getMyUserInformation(server.url, accessTokenUser)
386 const user = res.body
388 expect(user.username).to.equal('user_1')
389 expect(user.email).to.equal('updated@example.com')
390 expect(user.nsfwPolicy).to.equal('do_not_list')
391 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
392 expect(user.id).to.be.a('number')
393 expect(user.account.displayName).to.equal('new display name')
394 expect(user.account.description).to.be.null
397 it('Should be able to update my description', async function () {
400 accessToken: accessTokenUser,
401 description: 'my super description updated'
404 const res = await getMyUserInformation(server.url, accessTokenUser)
405 const user = res.body
407 expect(user.username).to.equal('user_1')
408 expect(user.email).to.equal('updated@example.com')
409 expect(user.nsfwPolicy).to.equal('do_not_list')
410 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
411 expect(user.id).to.be.a('number')
412 expect(user.account.displayName).to.equal('new display name')
413 expect(user.account.description).to.equal('my super description updated')
416 it('Should be able to update another user', async function () {
421 email: 'updated2@example.com',
423 role: UserRole.MODERATOR
426 const res = await getUserInformation(server.url, accessToken, userId)
427 const user = res.body
429 expect(user.username).to.equal('user_1')
430 expect(user.email).to.equal('updated2@example.com')
431 expect(user.nsfwPolicy).to.equal('do_not_list')
432 expect(user.videoQuota).to.equal(42)
433 expect(user.roleLabel).to.equal('Moderator')
434 expect(user.id).to.be.a('number')
437 it('Should have removed the user token', async function () {
438 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
440 accessTokenUser = await userLogin(server, user)
443 it('Should not be able to delete a user by a moderator', async function () {
444 await removeUser(server.url, 2, accessTokenUser, 403)
447 it('Should be able to list video blacklist by a moderator', async function () {
448 await getBlacklistedVideosList(server.url, accessTokenUser)
451 it('Should be able to remove this user', async function () {
452 await removeUser(server.url, userId, accessToken)
455 it('Should not be able to login with this user', async function () {
456 await userLogin(server, user, 400)
459 it('Should not have videos of this user', async function () {
460 const res = await getVideosList(server.url)
462 expect(res.body.total).to.equal(1)
464 const video = res.body.data[ 0 ]
465 expect(video.account.name).to.equal('root')
468 it('Should register a new user', async function () {
469 await registerUser(server.url, 'user_15', 'my super password')
472 it('Should be able to login with this registered user', async function () {
475 password: 'my super password'
478 accessToken = await userLogin(server, user15)
481 it('Should have the correct video quota', async function () {
482 const res = await getMyUserInformation(server.url, accessToken)
483 const user = res.body
485 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
488 it('Should remove me', async function () {
490 const res = await getUsersList(server.url, server.accessToken)
491 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
494 await deleteMe(server.url, accessToken)
497 const res = await getUsersList(server.url, server.accessToken)
498 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
502 it('Should block and unblock a user', async function () {
505 password: 'my super password'
507 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
508 const user16Id = resUser.body.user.id
510 accessToken = await userLogin(server, user16)
512 await getMyUserInformation(server.url, accessToken, 200)
513 await blockUser(server.url, user16Id, server.accessToken)
515 await getMyUserInformation(server.url, accessToken, 401)
516 await userLogin(server, user16, 400)
518 await unblockUser(server.url, user16Id, server.accessToken)
519 accessToken = await userLogin(server, user16)
520 await getMyUserInformation(server.url, accessToken, 200)
523 after(async function () {
524 killallServers([ server ])
526 // Keep the logs if the test failed