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
10 } from '../../utils/index'
11 import { follow } from '../../utils/server/follows'
12 import { setAccessTokensToServers } from '../../utils/users/login'
13 import { getMyVideos } from '../../utils/videos/videos'
15 const expect = chai.expect
17 describe('Test users', function () {
18 let server: ServerInfo
19 let accessToken: string
20 let accessTokenUser: string
25 password: 'super password'
28 before(async function () {
32 server = await runServer(1)
34 await setAccessTokensToServers([ server ])
37 it('Should create a new client')
39 it('Should return the first client')
41 it('Should remove the last client')
43 it('Should not login with an invalid client id', async function () {
44 const client = { id: 'client', secret: server.client.secret }
45 const res = await login(server.url, client, server.user, 400)
47 expect(res.body.error).to.equal('Authentication failed.')
50 it('Should not login with an invalid client secret', async function () {
51 const client = { id: server.client.id, secret: 'coucou' }
52 const res = await login(server.url, client, server.user, 400)
54 expect(res.body.error).to.equal('Authentication failed.')
57 it('Should not login with an invalid username', async function () {
58 const user = { username: 'captain crochet', password: server.user.password }
59 const res = await login(server.url, server.client, user, 400)
61 expect(res.body.error).to.equal('Authentication failed.')
64 it('Should not login with an invalid password', async function () {
65 const user = { username: server.user.username, password: 'mew_three' }
66 const res = await login(server.url, server.client, user, 400)
68 expect(res.body.error).to.equal('Authentication failed.')
71 it('Should not be able to upload a video', async function () {
72 accessToken = 'my_super_token'
74 const videoAttributes = {}
75 await uploadVideo(server.url, accessToken, videoAttributes, 401)
78 it('Should not be able to follow', async function () {
79 accessToken = 'my_super_token'
80 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
83 it('Should not be able to unfollow')
85 it('Should be able to login', async function () {
86 const res = await login(server.url, server.client, server.user, 200)
88 accessToken = res.body.access_token
91 it('Should upload the video with the correct token', async function () {
92 const videoAttributes = {}
93 await uploadVideo(server.url, accessToken, videoAttributes)
94 const res = await getVideosList(server.url)
95 const video = res.body.data[ 0 ]
97 expect(video.account.name).to.equal('root')
101 it('Should upload the video again with the correct token', async function () {
102 const videoAttributes = {}
103 await uploadVideo(server.url, accessToken, videoAttributes)
106 it('Should retrieve a video rating', async function () {
107 await rateVideo(server.url, accessToken, videoId, 'like')
108 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
109 const rating = res.body
111 expect(rating.videoId).to.equal(videoId)
112 expect(rating.rating).to.equal('like')
115 it('Should not be able to remove the video with an incorrect token', async function () {
116 await removeVideo(server.url, 'bad_token', videoId, 401)
119 it('Should not be able to remove the video with the token of another account')
121 it('Should be able to remove the video with the correct token', async function () {
122 await removeVideo(server.url, accessToken, videoId)
125 it('Should logout (revoke token)')
127 it('Should not be able to get the user information')
129 it('Should not be able to upload a video')
131 it('Should not be able to remove a video')
133 it('Should not be able to rate a video', async function () {
134 const path = '/api/v1/videos/'
141 path: path + videoId,
142 token: 'wrong token',
144 statusCodeExpected: 401
146 await makePutBodyRequest(options)
149 it('Should be able to login again')
151 it('Should have an expired access token')
153 it('Should refresh the token')
155 it('Should be able to upload a video again')
157 it('Should be able to create a new user', async function () {
158 await createUser(server.url, accessToken, user.username,user.password, 2 * 1024 * 1024)
161 it('Should be able to login with this user', async function () {
162 accessTokenUser = await userLogin(server, user)
165 it('Should be able to get the user information', async function () {
166 const res = await getMyUserInformation(server.url, accessTokenUser)
167 const user = res.body
169 expect(user.username).to.equal('user_1')
170 expect(user.email).to.equal('user_1@example.com')
171 expect(user.nsfwPolicy).to.equal('display')
172 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
173 expect(user.roleLabel).to.equal('User')
174 expect(user.id).to.be.a('number')
175 expect(user.account.displayName).to.equal('user_1')
176 expect(user.account.description).to.be.null
179 it('Should be able to upload a video with this user', async function () {
182 const videoAttributes = {
183 name: 'super user video',
184 fixture: 'video_short.webm'
186 await uploadVideo(server.url, accessTokenUser, videoAttributes)
189 it('Should have video quota updated', async function () {
190 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
191 const data = res.body
193 expect(data.videoQuotaUsed).to.equal(218910)
196 it('Should be able to list my videos', async function () {
197 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
198 expect(res.body.total).to.equal(1)
200 const videos = res.body.data
201 expect(videos).to.have.lengthOf(1)
203 expect(videos[ 0 ].name).to.equal('super user video')
206 it('Should list all the users', async function () {
207 const res = await getUsersList(server.url, server.accessToken)
208 const result = res.body
209 const total = result.total
210 const users = result.data
212 expect(total).to.equal(2)
213 expect(users).to.be.an('array')
214 expect(users.length).to.equal(2)
216 const user = users[ 0 ]
217 expect(user.username).to.equal('user_1')
218 expect(user.email).to.equal('user_1@example.com')
219 expect(user.nsfwPolicy).to.equal('display')
221 const rootUser = users[ 1 ]
222 expect(rootUser.username).to.equal('root')
223 expect(rootUser.email).to.equal('admin1@example.com')
224 expect(user.nsfwPolicy).to.equal('display')
229 it('Should list only the first user by username asc', async function () {
230 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
232 const result = res.body
233 const total = result.total
234 const users = result.data
236 expect(total).to.equal(2)
237 expect(users.length).to.equal(1)
239 const user = users[ 0 ]
240 expect(user.username).to.equal('root')
241 expect(user.email).to.equal('admin1@example.com')
242 expect(user.roleLabel).to.equal('Administrator')
243 expect(user.nsfwPolicy).to.equal('display')
246 it('Should list only the first user by username desc', async function () {
247 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
248 const result = res.body
249 const total = result.total
250 const users = result.data
252 expect(total).to.equal(2)
253 expect(users.length).to.equal(1)
255 const user = users[ 0 ]
256 expect(user.username).to.equal('user_1')
257 expect(user.email).to.equal('user_1@example.com')
258 expect(user.nsfwPolicy).to.equal('display')
261 it('Should list only the second user by createdAt desc', async function () {
262 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
263 const result = res.body
264 const total = result.total
265 const users = result.data
267 expect(total).to.equal(2)
268 expect(users.length).to.equal(1)
270 const user = users[ 0 ]
271 expect(user.username).to.equal('user_1')
272 expect(user.email).to.equal('user_1@example.com')
273 expect(user.nsfwPolicy).to.equal('display')
276 it('Should list all the users by createdAt asc', async function () {
277 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
278 const result = res.body
279 const total = result.total
280 const users = result.data
282 expect(total).to.equal(2)
283 expect(users.length).to.equal(2)
285 expect(users[ 0 ].username).to.equal('root')
286 expect(users[ 0 ].email).to.equal('admin1@example.com')
287 expect(users[ 0 ].nsfwPolicy).to.equal('display')
289 expect(users[ 1 ].username).to.equal('user_1')
290 expect(users[ 1 ].email).to.equal('user_1@example.com')
291 expect(users[ 1 ].nsfwPolicy).to.equal('display')
294 it('Should update my password', async function () {
297 accessToken: accessTokenUser,
298 newPassword: 'new password'
300 user.password = 'new password'
302 await userLogin(server, user, 200)
305 it('Should be able to change the NSFW display attribute', async function () {
308 accessToken: accessTokenUser,
309 nsfwPolicy: 'do_not_list'
312 const res = await getMyUserInformation(server.url, accessTokenUser)
313 const user = res.body
315 expect(user.username).to.equal('user_1')
316 expect(user.email).to.equal('user_1@example.com')
317 expect(user.nsfwPolicy).to.equal('do_not_list')
318 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
319 expect(user.id).to.be.a('number')
320 expect(user.account.displayName).to.equal('user_1')
321 expect(user.account.description).to.be.null
324 it('Should be able to change the autoPlayVideo attribute', async function () {
327 accessToken: accessTokenUser,
331 const res = await getMyUserInformation(server.url, accessTokenUser)
332 const user = res.body
334 expect(user.autoPlayVideo).to.be.false
337 it('Should be able to change the email display attribute', async function () {
340 accessToken: accessTokenUser,
341 email: 'updated@example.com'
344 const res = await getMyUserInformation(server.url, accessTokenUser)
345 const user = res.body
347 expect(user.username).to.equal('user_1')
348 expect(user.email).to.equal('updated@example.com')
349 expect(user.nsfwPolicy).to.equal('do_not_list')
350 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
351 expect(user.id).to.be.a('number')
352 expect(user.account.displayName).to.equal('user_1')
353 expect(user.account.description).to.be.null
356 it('Should be able to update my avatar', async function () {
357 const fixture = 'avatar.png'
359 await updateMyAvatar({
361 accessToken: accessTokenUser,
365 const res = await getMyUserInformation(server.url, accessTokenUser)
366 const user = res.body
368 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
371 it('Should be able to update my display name', async function () {
374 accessToken: accessTokenUser,
375 displayName: 'new display name'
378 const res = await getMyUserInformation(server.url, accessTokenUser)
379 const user = res.body
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('updated@example.com')
383 expect(user.nsfwPolicy).to.equal('do_not_list')
384 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
385 expect(user.id).to.be.a('number')
386 expect(user.account.displayName).to.equal('new display name')
387 expect(user.account.description).to.be.null
390 it('Should be able to update my description', async function () {
393 accessToken: accessTokenUser,
394 description: 'my super description updated'
397 const res = await getMyUserInformation(server.url, accessTokenUser)
398 const user = res.body
400 expect(user.username).to.equal('user_1')
401 expect(user.email).to.equal('updated@example.com')
402 expect(user.nsfwPolicy).to.equal('do_not_list')
403 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
404 expect(user.id).to.be.a('number')
405 expect(user.account.displayName).to.equal('new display name')
406 expect(user.account.description).to.equal('my super description updated')
409 it('Should be able to update another user', async function () {
414 email: 'updated2@example.com',
416 role: UserRole.MODERATOR
419 const res = await getUserInformation(server.url, accessToken, userId)
420 const user = res.body
422 expect(user.username).to.equal('user_1')
423 expect(user.email).to.equal('updated2@example.com')
424 expect(user.nsfwPolicy).to.equal('do_not_list')
425 expect(user.videoQuota).to.equal(42)
426 expect(user.roleLabel).to.equal('Moderator')
427 expect(user.id).to.be.a('number')
430 it('Should have removed the user token', async function () {
431 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
433 accessTokenUser = await userLogin(server, user)
436 it('Should not be able to delete a user by a moderator', async function () {
437 await removeUser(server.url, 2, accessTokenUser, 403)
440 it('Should be able to list video blacklist by a moderator', async function () {
441 await getBlacklistedVideosList(server.url, accessTokenUser)
444 it('Should be able to remove this user', async function () {
445 await removeUser(server.url, userId, accessToken)
448 it('Should not be able to login with this user', async function () {
449 await userLogin(server, user, 400)
452 it('Should not have videos of this user', async function () {
453 const res = await getVideosList(server.url)
455 expect(res.body.total).to.equal(1)
457 const video = res.body.data[ 0 ]
458 expect(video.account.name).to.equal('root')
461 it('Should register a new user', async function () {
462 await registerUser(server.url, 'user_15', 'my super password')
465 it('Should be able to login with this registered user', async function () {
468 password: 'my super password'
471 accessToken = await userLogin(server, user15)
474 it('Should have the correct video quota', async function () {
475 const res = await getMyUserInformation(server.url, accessToken)
476 const user = res.body
478 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
481 after(async function () {
482 killallServers([ server ])
484 // Keep the logs if the test failed