1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { 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,
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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)
197 it('Should be able to list my videos', async function () {
198 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
199 expect(res.body.total).to.equal(1)
201 const videos = res.body.data
202 expect(videos).to.have.lengthOf(1)
204 expect(videos[ 0 ].name).to.equal('super user video')
207 it('Should list all the users', async function () {
208 const res = await getUsersList(server.url, server.accessToken)
209 const result = res.body
210 const total = result.total
211 const users = result.data
213 expect(total).to.equal(2)
214 expect(users).to.be.an('array')
215 expect(users.length).to.equal(2)
217 const user = users[ 0 ]
218 expect(user.username).to.equal('user_1')
219 expect(user.email).to.equal('user_1@example.com')
220 expect(user.nsfwPolicy).to.equal('display')
222 const rootUser = users[ 1 ]
223 expect(rootUser.username).to.equal('root')
224 expect(rootUser.email).to.equal('admin1@example.com')
225 expect(user.nsfwPolicy).to.equal('display')
230 it('Should list only the first user by username asc', async function () {
231 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
233 const result = res.body
234 const total = result.total
235 const users = result.data
237 expect(total).to.equal(2)
238 expect(users.length).to.equal(1)
240 const user = users[ 0 ]
241 expect(user.username).to.equal('root')
242 expect(user.email).to.equal('admin1@example.com')
243 expect(user.roleLabel).to.equal('Administrator')
244 expect(user.nsfwPolicy).to.equal('display')
247 it('Should list only the first user by username desc', async function () {
248 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
249 const result = res.body
250 const total = result.total
251 const users = result.data
253 expect(total).to.equal(2)
254 expect(users.length).to.equal(1)
256 const user = users[ 0 ]
257 expect(user.username).to.equal('user_1')
258 expect(user.email).to.equal('user_1@example.com')
259 expect(user.nsfwPolicy).to.equal('display')
262 it('Should list only the second user by createdAt desc', async function () {
263 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
264 const result = res.body
265 const total = result.total
266 const users = result.data
268 expect(total).to.equal(2)
269 expect(users.length).to.equal(1)
271 const user = users[ 0 ]
272 expect(user.username).to.equal('user_1')
273 expect(user.email).to.equal('user_1@example.com')
274 expect(user.nsfwPolicy).to.equal('display')
277 it('Should list all the users by createdAt asc', async function () {
278 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
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(2)
286 expect(users[ 0 ].username).to.equal('root')
287 expect(users[ 0 ].email).to.equal('admin1@example.com')
288 expect(users[ 0 ].nsfwPolicy).to.equal('display')
290 expect(users[ 1 ].username).to.equal('user_1')
291 expect(users[ 1 ].email).to.equal('user_1@example.com')
292 expect(users[ 1 ].nsfwPolicy).to.equal('display')
295 it('Should update my password', async function () {
298 accessToken: accessTokenUser,
299 newPassword: 'new password'
301 user.password = 'new password'
303 await userLogin(server, user, 200)
306 it('Should be able to change the NSFW display attribute', async function () {
309 accessToken: accessTokenUser,
310 nsfwPolicy: 'do_not_list'
313 const res = await getMyUserInformation(server.url, accessTokenUser)
314 const user = res.body
316 expect(user.username).to.equal('user_1')
317 expect(user.email).to.equal('user_1@example.com')
318 expect(user.nsfwPolicy).to.equal('do_not_list')
319 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
320 expect(user.id).to.be.a('number')
321 expect(user.account.displayName).to.equal('user_1')
322 expect(user.account.description).to.be.null
325 it('Should be able to change the autoPlayVideo attribute', async function () {
328 accessToken: accessTokenUser,
332 const res = await getMyUserInformation(server.url, accessTokenUser)
333 const user = res.body
335 expect(user.autoPlayVideo).to.be.false
338 it('Should be able to change the email display attribute', async function () {
341 accessToken: accessTokenUser,
342 email: 'updated@example.com'
345 const res = await getMyUserInformation(server.url, accessTokenUser)
346 const user = res.body
348 expect(user.username).to.equal('user_1')
349 expect(user.email).to.equal('updated@example.com')
350 expect(user.nsfwPolicy).to.equal('do_not_list')
351 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
352 expect(user.id).to.be.a('number')
353 expect(user.account.displayName).to.equal('user_1')
354 expect(user.account.description).to.be.null
357 it('Should be able to update my avatar', async function () {
358 const fixture = 'avatar.png'
360 await updateMyAvatar({
362 accessToken: accessTokenUser,
366 const res = await getMyUserInformation(server.url, accessTokenUser)
367 const user = res.body
369 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
372 it('Should be able to update my display name', async function () {
375 accessToken: accessTokenUser,
376 displayName: 'new display name'
379 const res = await getMyUserInformation(server.url, accessTokenUser)
380 const user = res.body
382 expect(user.username).to.equal('user_1')
383 expect(user.email).to.equal('updated@example.com')
384 expect(user.nsfwPolicy).to.equal('do_not_list')
385 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
386 expect(user.id).to.be.a('number')
387 expect(user.account.displayName).to.equal('new display name')
388 expect(user.account.description).to.be.null
391 it('Should be able to update my description', async function () {
394 accessToken: accessTokenUser,
395 description: 'my super description updated'
398 const res = await getMyUserInformation(server.url, accessTokenUser)
399 const user = res.body
401 expect(user.username).to.equal('user_1')
402 expect(user.email).to.equal('updated@example.com')
403 expect(user.nsfwPolicy).to.equal('do_not_list')
404 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
405 expect(user.id).to.be.a('number')
406 expect(user.account.displayName).to.equal('new display name')
407 expect(user.account.description).to.equal('my super description updated')
410 it('Should be able to update another user', async function () {
415 email: 'updated2@example.com',
417 role: UserRole.MODERATOR
420 const res = await getUserInformation(server.url, accessToken, userId)
421 const user = res.body
423 expect(user.username).to.equal('user_1')
424 expect(user.email).to.equal('updated2@example.com')
425 expect(user.nsfwPolicy).to.equal('do_not_list')
426 expect(user.videoQuota).to.equal(42)
427 expect(user.roleLabel).to.equal('Moderator')
428 expect(user.id).to.be.a('number')
431 it('Should have removed the user token', async function () {
432 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
434 accessTokenUser = await userLogin(server, user)
437 it('Should not be able to delete a user by a moderator', async function () {
438 await removeUser(server.url, 2, accessTokenUser, 403)
441 it('Should be able to list video blacklist by a moderator', async function () {
442 await getBlacklistedVideosList(server.url, accessTokenUser)
445 it('Should be able to remove this user', async function () {
446 await removeUser(server.url, userId, accessToken)
449 it('Should not be able to login with this user', async function () {
450 await userLogin(server, user, 400)
453 it('Should not have videos of this user', async function () {
454 const res = await getVideosList(server.url)
456 expect(res.body.total).to.equal(1)
458 const video = res.body.data[ 0 ]
459 expect(video.account.name).to.equal('root')
462 it('Should register a new user', async function () {
463 await registerUser(server.url, 'user_15', 'my super password')
466 it('Should be able to login with this registered user', async function () {
469 password: 'my super password'
472 accessToken = await userLogin(server, user15)
475 it('Should have the correct video quota', async function () {
476 const res = await getMyUserInformation(server.url, accessToken)
477 const user = res.body
479 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
482 it('Should remove me', async function () {
484 const res = await getUsersList(server.url, server.accessToken)
485 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
488 await deleteMe(server.url, accessToken)
491 const res = await getUsersList(server.url, server.accessToken)
492 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
496 after(async function () {
497 killallServers([ server ])
499 // Keep the logs if the test failed