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.accountName).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.displayNSFW).to.be.false
172 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
173 expect(user.roleLabel).to.equal('User')
174 expect(user.id).to.be.a('number')
177 it('Should be able to upload a video with this user', async function () {
180 const videoAttributes = {
181 name: 'super user video',
182 fixture: 'video_short.webm'
184 await uploadVideo(server.url, accessTokenUser, videoAttributes)
187 it('Should have video quota updated', async function () {
188 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
189 const data = res.body
191 expect(data.videoQuotaUsed).to.equal(218910)
194 it('Should be able to list my videos', async function () {
195 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
196 expect(res.body.total).to.equal(1)
198 const videos = res.body.data
199 expect(videos).to.have.lengthOf(1)
201 expect(videos[ 0 ].name).to.equal('super user video')
204 it('Should list all the users', async function () {
205 const res = await getUsersList(server.url, server.accessToken)
206 const result = res.body
207 const total = result.total
208 const users = result.data
210 expect(total).to.equal(2)
211 expect(users).to.be.an('array')
212 expect(users.length).to.equal(2)
214 const user = users[ 0 ]
215 expect(user.username).to.equal('user_1')
216 expect(user.email).to.equal('user_1@example.com')
217 expect(user.displayNSFW).to.be.false
219 const rootUser = users[ 1 ]
220 expect(rootUser.username).to.equal('root')
221 expect(rootUser.email).to.equal('admin1@example.com')
222 expect(rootUser.displayNSFW).to.be.false
227 it('Should list only the first user by username asc', async function () {
228 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
230 const result = res.body
231 const total = result.total
232 const users = result.data
234 expect(total).to.equal(2)
235 expect(users.length).to.equal(1)
237 const user = users[ 0 ]
238 expect(user.username).to.equal('root')
239 expect(user.email).to.equal('admin1@example.com')
240 expect(user.roleLabel).to.equal('Administrator')
241 expect(user.displayNSFW).to.be.false
244 it('Should list only the first user by username desc', async function () {
245 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
246 const result = res.body
247 const total = result.total
248 const users = result.data
250 expect(total).to.equal(2)
251 expect(users.length).to.equal(1)
253 const user = users[ 0 ]
254 expect(user.username).to.equal('user_1')
255 expect(user.email).to.equal('user_1@example.com')
256 expect(user.displayNSFW).to.be.false
259 it('Should list only the second user by createdAt desc', async function () {
260 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
261 const result = res.body
262 const total = result.total
263 const users = result.data
265 expect(total).to.equal(2)
266 expect(users.length).to.equal(1)
268 const user = users[ 0 ]
269 expect(user.username).to.equal('user_1')
270 expect(user.email).to.equal('user_1@example.com')
271 expect(user.displayNSFW).to.be.false
274 it('Should list all the users by createdAt asc', async function () {
275 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
276 const result = res.body
277 const total = result.total
278 const users = result.data
280 expect(total).to.equal(2)
281 expect(users.length).to.equal(2)
283 expect(users[ 0 ].username).to.equal('root')
284 expect(users[ 0 ].email).to.equal('admin1@example.com')
285 expect(users[ 0 ].displayNSFW).to.be.false
287 expect(users[ 1 ].username).to.equal('user_1')
288 expect(users[ 1 ].email).to.equal('user_1@example.com')
289 expect(users[ 1 ].displayNSFW).to.be.false
292 it('Should update my password', async function () {
295 accessToken: accessTokenUser,
296 newPassword: 'new password'
298 user.password = 'new password'
300 await userLogin(server, user, 200)
303 it('Should be able to change the NSFW display attribute', async function () {
306 accessToken: accessTokenUser,
310 const res = await getMyUserInformation(server.url, accessTokenUser)
311 const user = res.body
313 expect(user.username).to.equal('user_1')
314 expect(user.email).to.equal('user_1@example.com')
315 expect(user.displayNSFW).to.be.ok
316 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
317 expect(user.id).to.be.a('number')
320 it('Should be able to change the autoPlayVideo attribute', async function () {
323 accessToken: accessTokenUser,
327 const res = await getMyUserInformation(server.url, accessTokenUser)
328 const user = res.body
330 expect(user.autoPlayVideo).to.be.false
333 it('Should be able to change the email display attribute', async function () {
336 accessToken: accessTokenUser,
337 email: 'updated@example.com'
340 const res = await getMyUserInformation(server.url, accessTokenUser)
341 const user = res.body
343 expect(user.username).to.equal('user_1')
344 expect(user.email).to.equal('updated@example.com')
345 expect(user.displayNSFW).to.be.ok
346 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
347 expect(user.id).to.be.a('number')
350 it('Should be able to update my avatar', async function () {
351 const fixture = 'avatar.png'
353 await updateMyAvatar({
355 accessToken: accessTokenUser,
359 const res = await getMyUserInformation(server.url, accessTokenUser)
360 const user = res.body
362 const test = await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
363 expect(test).to.equal(true)
366 it('Should be able to update another user', async function () {
371 email: 'updated2@example.com',
373 role: UserRole.MODERATOR
376 const res = await getUserInformation(server.url, accessToken, userId)
377 const user = res.body
379 expect(user.username).to.equal('user_1')
380 expect(user.email).to.equal('updated2@example.com')
381 expect(user.displayNSFW).to.be.ok
382 expect(user.videoQuota).to.equal(42)
383 expect(user.roleLabel).to.equal('Moderator')
384 expect(user.id).to.be.a('number')
387 it('Should have removed the user token', async function () {
388 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
390 accessTokenUser = await userLogin(server, user)
393 it('Should not be able to delete a user by a moderator', async function () {
394 await removeUser(server.url, 2, accessTokenUser, 403)
397 it('Should be able to list video blacklist by a moderator', async function () {
398 await getBlacklistedVideosList(server.url, accessTokenUser)
401 it('Should be able to remove this user', async function () {
402 await removeUser(server.url, userId, accessToken)
405 it('Should not be able to login with this user', async function () {
406 await userLogin(server, user, 400)
409 it('Should not have videos of this user', async function () {
410 const res = await getVideosList(server.url)
412 expect(res.body.total).to.equal(1)
414 const video = res.body.data[ 0 ]
415 expect(video.accountName).to.equal('root')
418 it('Should register a new user', async function () {
419 await registerUser(server.url, 'user_15', 'my super password')
422 it('Should be able to login with this registered user', async function () {
425 password: 'my super password'
428 accessToken = await userLogin(server, user15)
431 it('Should have the correct video quota', async function () {
432 const res = await getMyUserInformation(server.url, accessToken)
433 const user = res.body
435 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
438 after(async function () {
439 killallServers([ server ])
441 // Keep the logs if the test failed