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')
175 expect(user.account.description).to.be.null
178 it('Should be able to upload a video with this user', async function () {
181 const videoAttributes = {
182 name: 'super user video',
183 fixture: 'video_short.webm'
185 await uploadVideo(server.url, accessTokenUser, videoAttributes)
188 it('Should have video quota updated', async function () {
189 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
190 const data = res.body
192 expect(data.videoQuotaUsed).to.equal(218910)
195 it('Should be able to list my videos', async function () {
196 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
197 expect(res.body.total).to.equal(1)
199 const videos = res.body.data
200 expect(videos).to.have.lengthOf(1)
202 expect(videos[ 0 ].name).to.equal('super user video')
205 it('Should list all the users', async function () {
206 const res = await getUsersList(server.url, server.accessToken)
207 const result = res.body
208 const total = result.total
209 const users = result.data
211 expect(total).to.equal(2)
212 expect(users).to.be.an('array')
213 expect(users.length).to.equal(2)
215 const user = users[ 0 ]
216 expect(user.username).to.equal('user_1')
217 expect(user.email).to.equal('user_1@example.com')
218 expect(user.displayNSFW).to.be.false
220 const rootUser = users[ 1 ]
221 expect(rootUser.username).to.equal('root')
222 expect(rootUser.email).to.equal('admin1@example.com')
223 expect(rootUser.displayNSFW).to.be.false
228 it('Should list only the first user by username asc', async function () {
229 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
231 const result = res.body
232 const total = result.total
233 const users = result.data
235 expect(total).to.equal(2)
236 expect(users.length).to.equal(1)
238 const user = users[ 0 ]
239 expect(user.username).to.equal('root')
240 expect(user.email).to.equal('admin1@example.com')
241 expect(user.roleLabel).to.equal('Administrator')
242 expect(user.displayNSFW).to.be.false
245 it('Should list only the first user by username desc', async function () {
246 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
247 const result = res.body
248 const total = result.total
249 const users = result.data
251 expect(total).to.equal(2)
252 expect(users.length).to.equal(1)
254 const user = users[ 0 ]
255 expect(user.username).to.equal('user_1')
256 expect(user.email).to.equal('user_1@example.com')
257 expect(user.displayNSFW).to.be.false
260 it('Should list only the second user by createdAt desc', async function () {
261 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
262 const result = res.body
263 const total = result.total
264 const users = result.data
266 expect(total).to.equal(2)
267 expect(users.length).to.equal(1)
269 const user = users[ 0 ]
270 expect(user.username).to.equal('user_1')
271 expect(user.email).to.equal('user_1@example.com')
272 expect(user.displayNSFW).to.be.false
275 it('Should list all the users by createdAt asc', async function () {
276 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
277 const result = res.body
278 const total = result.total
279 const users = result.data
281 expect(total).to.equal(2)
282 expect(users.length).to.equal(2)
284 expect(users[ 0 ].username).to.equal('root')
285 expect(users[ 0 ].email).to.equal('admin1@example.com')
286 expect(users[ 0 ].displayNSFW).to.be.false
288 expect(users[ 1 ].username).to.equal('user_1')
289 expect(users[ 1 ].email).to.equal('user_1@example.com')
290 expect(users[ 1 ].displayNSFW).to.be.false
293 it('Should update my password', async function () {
296 accessToken: accessTokenUser,
297 newPassword: 'new password'
299 user.password = 'new password'
301 await userLogin(server, user, 200)
304 it('Should be able to change the NSFW display attribute', async function () {
307 accessToken: accessTokenUser,
311 const res = await getMyUserInformation(server.url, accessTokenUser)
312 const user = res.body
314 expect(user.username).to.equal('user_1')
315 expect(user.email).to.equal('user_1@example.com')
316 expect(user.displayNSFW).to.be.ok
317 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
318 expect(user.id).to.be.a('number')
319 expect(user.account.description).to.be.null
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')
350 expect(user.account.description).to.be.null
353 it('Should be able to update my avatar', async function () {
354 const fixture = 'avatar.png'
356 await updateMyAvatar({
358 accessToken: accessTokenUser,
362 const res = await getMyUserInformation(server.url, accessTokenUser)
363 const user = res.body
365 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
368 it('Should be able to update my description', async function () {
371 accessToken: accessTokenUser,
372 description: 'my super description updated'
375 const res = await getMyUserInformation(server.url, accessTokenUser)
376 const user = res.body
378 expect(user.username).to.equal('user_1')
379 expect(user.email).to.equal('updated@example.com')
380 expect(user.displayNSFW).to.be.ok
381 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
382 expect(user.id).to.be.a('number')
383 expect(user.account.description).to.equal('my super description updated')
386 it('Should be able to update another user', async function () {
391 email: 'updated2@example.com',
393 role: UserRole.MODERATOR
396 const res = await getUserInformation(server.url, accessToken, userId)
397 const user = res.body
399 expect(user.username).to.equal('user_1')
400 expect(user.email).to.equal('updated2@example.com')
401 expect(user.displayNSFW).to.be.ok
402 expect(user.videoQuota).to.equal(42)
403 expect(user.roleLabel).to.equal('Moderator')
404 expect(user.id).to.be.a('number')
407 it('Should have removed the user token', async function () {
408 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
410 accessTokenUser = await userLogin(server, user)
413 it('Should not be able to delete a user by a moderator', async function () {
414 await removeUser(server.url, 2, accessTokenUser, 403)
417 it('Should be able to list video blacklist by a moderator', async function () {
418 await getBlacklistedVideosList(server.url, accessTokenUser)
421 it('Should be able to remove this user', async function () {
422 await removeUser(server.url, userId, accessToken)
425 it('Should not be able to login with this user', async function () {
426 await userLogin(server, user, 400)
429 it('Should not have videos of this user', async function () {
430 const res = await getVideosList(server.url)
432 expect(res.body.total).to.equal(1)
434 const video = res.body.data[ 0 ]
435 expect(video.accountName).to.equal('root')
438 it('Should register a new user', async function () {
439 await registerUser(server.url, 'user_15', 'my super password')
442 it('Should be able to login with this registered user', async function () {
445 password: 'my super password'
448 accessToken = await userLogin(server, user15)
451 it('Should have the correct video quota', async function () {
452 const res = await getMyUserInformation(server.url, accessToken)
453 const user = res.body
455 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
458 after(async function () {
459 killallServers([ server ])
461 // Keep the logs if the test failed