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, getUserInformation,
9 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
10 runServer, ServerInfo, serverLogin, testVideoImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo
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
25 before(async function () {
29 server = await runServer(1)
31 await setAccessTokensToServers([ server ])
34 it('Should create a new client')
36 it('Should return the first client')
38 it('Should remove the last client')
40 it('Should not login with an invalid client id', async function () {
41 const client = { id: 'client', secret: server.client.secret }
42 const res = await login(server.url, client, server.user, 400)
44 expect(res.body.error).to.equal('Authentication failed.')
47 it('Should not login with an invalid client secret', async function () {
48 const client = { id: server.client.id, secret: 'coucou' }
49 const res = await login(server.url, client, server.user, 400)
51 expect(res.body.error).to.equal('Authentication failed.')
54 it('Should not login with an invalid username', async function () {
55 const user = { username: 'captain crochet', password: server.user.password }
56 const res = await login(server.url, server.client, user, 400)
58 expect(res.body.error).to.equal('Authentication failed.')
61 it('Should not login with an invalid password', async function () {
62 const user = { username: server.user.username, password: 'mew_three' }
63 const res = await login(server.url, server.client, user, 400)
65 expect(res.body.error).to.equal('Authentication failed.')
68 it('Should not be able to upload a video', async function () {
69 accessToken = 'my_super_token'
71 const videoAttributes = {}
72 await uploadVideo(server.url, accessToken, videoAttributes, 401)
75 it('Should not be able to follow', async function () {
76 accessToken = 'my_super_token'
77 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
80 it('Should not be able to unfollow')
82 it('Should be able to login', async function () {
83 const res = await login(server.url, server.client, server.user, 200)
85 accessToken = res.body.access_token
88 it('Should upload the video with the correct token', async function () {
89 const videoAttributes = {}
90 await uploadVideo(server.url, accessToken, videoAttributes)
91 const res = await getVideosList(server.url)
92 const video = res.body.data[ 0 ]
94 expect(video.accountName).to.equal('root')
98 it('Should upload the video again with the correct token', async function () {
99 const videoAttributes = {}
100 await uploadVideo(server.url, accessToken, videoAttributes)
103 it('Should retrieve a video rating', async function () {
104 await rateVideo(server.url, accessToken, videoId, 'like')
105 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
106 const rating = res.body
108 expect(rating.videoId).to.equal(videoId)
109 expect(rating.rating).to.equal('like')
112 it('Should not be able to remove the video with an incorrect token', async function () {
113 await removeVideo(server.url, 'bad_token', videoId, 401)
116 it('Should not be able to remove the video with the token of another account')
118 it('Should be able to remove the video with the correct token', async function () {
119 await removeVideo(server.url, accessToken, videoId)
122 it('Should logout (revoke token)')
124 it('Should not be able to get the user information')
126 it('Should not be able to upload a video')
128 it('Should not be able to remove a video')
130 it('Should not be able to rate a video', async function () {
131 const path = '/api/v1/videos/'
138 path: path + videoId,
139 token: 'wrong token',
141 statusCodeExpected: 401
143 await makePutBodyRequest(options)
146 it('Should be able to login again')
148 it('Should have an expired access token')
150 it('Should refresh the token')
152 it('Should be able to upload a video again')
154 it('Should be able to create a new user', async function () {
155 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
158 it('Should be able to login with this user', async function () {
161 password: 'super password'
164 accessTokenUser = await serverLogin(server)
167 it('Should be able to get the user information', async function () {
168 const res = await getMyUserInformation(server.url, accessTokenUser)
169 const user = res.body
171 expect(user.username).to.equal('user_1')
172 expect(user.email).to.equal('user_1@example.com')
173 expect(user.displayNSFW).to.be.false
174 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
175 expect(user.roleLabel).to.equal('User')
176 expect(user.id).to.be.a('number')
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.displayNSFW).to.be.false
221 const rootUser = users[ 1 ]
222 expect(rootUser.username).to.equal('root')
223 expect(rootUser.email).to.equal('admin1@example.com')
224 expect(rootUser.displayNSFW).to.be.false
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.displayNSFW).to.be.false
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.displayNSFW).to.be.false
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.displayNSFW).to.be.false
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 ].displayNSFW).to.be.false
289 expect(users[ 1 ].username).to.equal('user_1')
290 expect(users[ 1 ].email).to.equal('user_1@example.com')
291 expect(users[ 1 ].displayNSFW).to.be.false
294 it('Should update my password', async function () {
297 accessToken: accessTokenUser,
298 newPassword: 'new password'
300 server.user.password = 'new password'
302 await login(server.url, server.client, server.user, 200)
305 it('Should be able to change the NSFW display attribute', async function () {
308 accessToken: accessTokenUser,
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.displayNSFW).to.be.ok
318 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
319 expect(user.id).to.be.a('number')
322 it('Should be able to change the autoPlayVideo attribute', async function () {
325 accessToken: accessTokenUser,
329 const res = await getMyUserInformation(server.url, accessTokenUser)
330 const user = res.body
332 expect(user.autoPlayVideo).to.be.false
335 it('Should be able to change the email display attribute', async function () {
338 accessToken: accessTokenUser,
339 email: 'updated@example.com'
342 const res = await getMyUserInformation(server.url, accessTokenUser)
343 const user = res.body
345 expect(user.username).to.equal('user_1')
346 expect(user.email).to.equal('updated@example.com')
347 expect(user.displayNSFW).to.be.ok
348 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
349 expect(user.id).to.be.a('number')
352 it('Should be able to update my avatar', async function () {
353 const fixture = 'avatar.png'
355 await updateMyAvatar({
357 accessToken: accessTokenUser,
361 const res = await getMyUserInformation(server.url, accessTokenUser)
362 const user = res.body
364 const test = await testVideoImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
365 expect(test).to.equal(true)
368 it('Should be able to update another user', async function () {
373 email: 'updated2@example.com',
375 role: UserRole.MODERATOR
378 const res = await getUserInformation(server.url, accessToken, userId)
379 const user = res.body
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('updated2@example.com')
383 expect(user.displayNSFW).to.be.ok
384 expect(user.videoQuota).to.equal(42)
385 expect(user.roleLabel).to.equal('Moderator')
386 expect(user.id).to.be.a('number')
389 it('Should not be able to delete a user by a moderator', async function () {
390 await removeUser(server.url, 2, accessTokenUser, 403)
393 it('Should be able to list video blacklist by a moderator', async function () {
394 await getBlacklistedVideosList(server.url, accessTokenUser)
397 it('Should be able to remove this user', async function () {
398 await removeUser(server.url, userId, accessToken)
401 it('Should not be able to login with this user', async function () {
402 // server.user is already set to user 1
403 await login(server.url, server.client, server.user, 400)
406 it('Should not have videos of this user', async function () {
407 const res = await getVideosList(server.url)
409 expect(res.body.total).to.equal(1)
411 const video = res.body.data[ 0 ]
412 expect(video.accountName).to.equal('root')
415 it('Should register a new user', async function () {
416 await registerUser(server.url, 'user_15', 'my super password')
419 it('Should be able to login with this registered user', async function () {
422 password: 'my super password'
425 accessToken = await serverLogin(server)
428 it('Should have the correct video quota', async function () {
429 const res = await getMyUserInformation(server.url, accessToken)
430 const user = res.body
432 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
435 after(async function () {
436 killallServers([ server ])
438 // Keep the logs if the test failed