1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { UserRole } from '../../../../shared/index'
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList,
8 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
9 runServer, ServerInfo, serverLogin, updateMyUser, updateUser, uploadVideo
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
24 before(async function () {
28 server = await runServer(1)
30 await setAccessTokensToServers([ server ])
33 it('Should create a new client')
35 it('Should return the first client')
37 it('Should remove the last client')
39 it('Should not login with an invalid client id', async function () {
40 const client = { id: 'client', secret: server.client.secret }
41 const res = await login(server.url, client, server.user, 400)
43 expect(res.body.error)
45 .equal('Authentication failed.')
48 it('Should not login with an invalid client secret', async function () {
49 const client = { id: server.client.id, secret: 'coucou' }
50 const res = await login(server.url, client, server.user, 400)
52 expect(res.body.error)
54 .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)
63 .equal('Authentication failed.')
66 it('Should not login with an invalid password', async function () {
67 const user = { username: server.user.username, password: 'mew_three' }
68 const res = await login(server.url, server.client, user, 400)
70 expect(res.body.error)
72 .equal('Authentication failed.')
75 it('Should not be able to upload a video', async function () {
76 accessToken = 'my_super_token'
78 const videoAttributes = {}
79 await uploadVideo(server.url, accessToken, videoAttributes, 401)
82 it('Should not be able to follow', async function () {
83 accessToken = 'my_super_token'
84 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
87 it('Should not be able to unfollow')
89 it('Should be able to login', async function () {
90 const res = await login(server.url, server.client, server.user, 200)
92 accessToken = res.body.access_token
95 it('Should upload the video with the correct token', async function () {
96 const videoAttributes = {}
97 await uploadVideo(server.url, accessToken, videoAttributes)
98 const res = await getVideosList(server.url)
99 const video = res.body.data[ 0 ]
101 expect(video.accountName)
107 it('Should upload the video again with the correct token', async function () {
108 const videoAttributes = {}
109 await uploadVideo(server.url, accessToken, videoAttributes)
112 it('Should retrieve a video rating', async function () {
113 await rateVideo(server.url, accessToken, videoId, 'like')
114 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
115 const rating = res.body
117 expect(rating.videoId)
120 expect(rating.rating)
125 it('Should not be able to remove the video with an incorrect token', async function () {
126 await removeVideo(server.url, 'bad_token', videoId, 401)
129 it('Should not be able to remove the video with the token of another account')
131 it('Should be able to remove the video with the correct token', async function () {
132 await removeVideo(server.url, accessToken, videoId)
135 it('Should logout (revoke token)')
137 it('Should not be able to get the user information')
139 it('Should not be able to upload a video')
141 it('Should not be able to remove a video')
143 it('Should not be able to rate a video', async function () {
144 const path = '/api/v1/videos/'
151 path: path + videoId,
152 token: 'wrong token',
154 statusCodeExpected: 401
156 await makePutBodyRequest(options)
159 it('Should be able to login again')
161 it('Should have an expired access token')
163 it('Should refresh the token')
165 it('Should be able to upload a video again')
167 it('Should be able to create a new user', async function () {
168 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
171 it('Should be able to login with this user', async function () {
174 password: 'super password'
177 accessTokenUser = await serverLogin(server)
180 it('Should be able to get the user information', async function () {
181 const res = await getMyUserInformation(server.url, accessTokenUser)
182 const user = res.body
184 expect(user.username)
189 .equal('user_1@example.com')
190 expect(user.displayNSFW).to.be.false
191 expect(user.videoQuota)
193 .equal(2 * 1024 * 1024)
194 expect(user.roleLabel)
203 it('Should be able to upload a video with this user', async function () {
206 const videoAttributes = {
207 name: 'super user video'
209 await uploadVideo(server.url, accessTokenUser, videoAttributes)
212 it('Should be able to list my videos', async function () {
213 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
214 expect(res.body.total)
218 const videos = res.body.data
224 expect(videos[ 0 ].name)
226 .equal('super user video')
229 it('Should list all the users', async function () {
230 const res = await getUsersList(server.url, server.accessToken)
231 const result = res.body
232 const total = result.total
233 const users = result.data
246 const user = users[ 0 ]
247 expect(user.username)
252 .equal('user_1@example.com')
253 expect(user.displayNSFW).to.be.false
255 const rootUser = users[ 1 ]
256 expect(rootUser.username)
259 expect(rootUser.email)
261 .equal('admin1@example.com')
262 expect(rootUser.displayNSFW).to.be.false
267 it('Should list only the first user by username asc', async function () {
268 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
270 const result = res.body
271 const total = result.total
272 const users = result.data
281 const user = users[ 0 ]
282 expect(user.username)
287 .equal('admin1@example.com')
288 expect(user.roleLabel)
290 .equal('Administrator')
291 expect(user.displayNSFW).to.be.false
294 it('Should list only the first user by username desc', async function () {
295 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
296 const result = res.body
297 const total = result.total
298 const users = result.data
307 const user = users[ 0 ]
308 expect(user.username)
313 .equal('user_1@example.com')
314 expect(user.displayNSFW).to.be.false
317 it('Should list only the second user by createdAt desc', async function () {
318 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
319 const result = res.body
320 const total = result.total
321 const users = result.data
330 const user = users[ 0 ]
331 expect(user.username)
336 .equal('user_1@example.com')
337 expect(user.displayNSFW).to.be.false
340 it('Should list all the users by createdAt asc', async function () {
341 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
342 const result = res.body
343 const total = result.total
344 const users = result.data
353 expect(users[ 0 ].username)
356 expect(users[ 0 ].email)
358 .equal('admin1@example.com')
359 expect(users[ 0 ].displayNSFW).to.be.false
361 expect(users[ 1 ].username)
364 expect(users[ 1 ].email)
366 .equal('user_1@example.com')
367 expect(users[ 1 ].displayNSFW).to.be.false
370 it('Should update my password', async function () {
373 accessToken: accessTokenUser,
374 newPassword: 'new password'
376 server.user.password = 'new password'
378 await login(server.url, server.client, server.user, 200)
381 it('Should be able to change the NSFW display attribute', async function () {
384 accessToken: accessTokenUser,
388 const res = await getMyUserInformation(server.url, accessTokenUser)
389 const user = res.body
391 expect(user.username)
396 .equal('user_1@example.com')
397 expect(user.displayNSFW).to.be.ok
398 expect(user.videoQuota)
400 .equal(2 * 1024 * 1024)
407 it('Should be able to change the autoPlayVideo attribute', async function () {
410 accessToken: accessTokenUser,
414 const res = await getMyUserInformation(server.url, accessTokenUser)
415 const user = res.body
417 expect(user.autoPlayVideo).to.be.false
420 it('Should be able to change the email display attribute', async function () {
423 accessToken: accessTokenUser,
424 email: 'updated@example.com'
427 const res = await getMyUserInformation(server.url, accessTokenUser)
428 const user = res.body
430 expect(user.username)
435 .equal('updated@example.com')
436 expect(user.displayNSFW).to.be.ok
437 expect(user.videoQuota)
439 .equal(2 * 1024 * 1024)
446 it('Should be able to update another user', async function () {
451 email: 'updated2@example.com',
453 role: UserRole.MODERATOR
456 const res = await getUserInformation(server.url, accessToken, userId)
457 const user = res.body
459 expect(user.username)
464 .equal('updated2@example.com')
465 expect(user.displayNSFW).to.be.ok
466 expect(user.videoQuota)
469 expect(user.roleLabel)
478 it('Should not be able to delete a user by a moderator', async function () {
479 await removeUser(server.url, 2, accessTokenUser, 403)
482 it('Should be able to list video blacklist by a moderator', async function () {
483 await getBlacklistedVideosList(server.url, accessTokenUser)
486 it('Should be able to remove this user', async function () {
487 await removeUser(server.url, userId, accessToken)
490 it('Should not be able to login with this user', async function () {
491 // server.user is already set to user 1
492 await login(server.url, server.client, server.user, 400)
495 it('Should not have videos of this user', async function () {
496 const res = await getVideosList(server.url)
498 expect(res.body.total)
502 const video = res.body.data[ 0 ]
503 expect(video.accountName)
508 it('Should register a new user', async function () {
509 await registerUser(server.url, 'user_15', 'my super password')
512 it('Should be able to login with this registered user', async function () {
515 password: 'my super password'
518 accessToken = await serverLogin(server)
521 it('Should have the correct video quota', async function () {
522 const res = await getMyUserInformation(server.url, accessToken)
523 const user = res.body
525 expect(user.videoQuota)
527 .equal(5 * 1024 * 1024)
530 after(async function () {
531 killallServers([ server ])
533 // Keep the logs if the test failed