]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
1192ef9e4ad9fc5b248a718a6c0d307ec6ab8137
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { UserRole } from '../../../../shared/index'
6 import {
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'
14
15 const expect = chai.expect
16
17 describe('Test users', function () {
18 let server: ServerInfo
19 let accessToken: string
20 let accessTokenUser: string
21 let videoId: number
22 let userId: number
23 const user = {
24 username: 'user_1',
25 password: 'super password'
26 }
27
28 before(async function () {
29 this.timeout(30000)
30
31 await flushTests()
32 server = await runServer(1)
33
34 await setAccessTokensToServers([ server ])
35 })
36
37 it('Should create a new client')
38
39 it('Should return the first client')
40
41 it('Should remove the last client')
42
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)
46
47 expect(res.body.error).to.equal('Authentication failed.')
48 })
49
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)
53
54 expect(res.body.error).to.equal('Authentication failed.')
55 })
56
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)
60
61 expect(res.body.error).to.equal('Authentication failed.')
62 })
63
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)
67
68 expect(res.body.error).to.equal('Authentication failed.')
69 })
70
71 it('Should not be able to upload a video', async function () {
72 accessToken = 'my_super_token'
73
74 const videoAttributes = {}
75 await uploadVideo(server.url, accessToken, videoAttributes, 401)
76 })
77
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)
81 })
82
83 it('Should not be able to unfollow')
84
85 it('Should be able to login', async function () {
86 const res = await login(server.url, server.client, server.user, 200)
87
88 accessToken = res.body.access_token
89 })
90
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 ]
96
97 expect(video.account.name).to.equal('root')
98 videoId = video.id
99 })
100
101 it('Should upload the video again with the correct token', async function () {
102 const videoAttributes = {}
103 await uploadVideo(server.url, accessToken, videoAttributes)
104 })
105
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
110
111 expect(rating.videoId).to.equal(videoId)
112 expect(rating.rating).to.equal('like')
113 })
114
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)
117 })
118
119 it('Should not be able to remove the video with the token of another account')
120
121 it('Should be able to remove the video with the correct token', async function () {
122 await removeVideo(server.url, accessToken, videoId)
123 })
124
125 it('Should logout (revoke token)')
126
127 it('Should not be able to get the user information')
128
129 it('Should not be able to upload a video')
130
131 it('Should not be able to remove a video')
132
133 it('Should not be able to rate a video', async function () {
134 const path = '/api/v1/videos/'
135 const data = {
136 rating: 'likes'
137 }
138
139 const options = {
140 url: server.url,
141 path: path + videoId,
142 token: 'wrong token',
143 fields: data,
144 statusCodeExpected: 401
145 }
146 await makePutBodyRequest(options)
147 })
148
149 it('Should be able to login again')
150
151 it('Should have an expired access token')
152
153 it('Should refresh the token')
154
155 it('Should be able to upload a video again')
156
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)
159 })
160
161 it('Should be able to login with this user', async function () {
162 accessTokenUser = await userLogin(server, user)
163 })
164
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
168
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.description).to.be.null
176 })
177
178 it('Should be able to upload a video with this user', async function () {
179 this.timeout(5000)
180
181 const videoAttributes = {
182 name: 'super user video',
183 fixture: 'video_short.webm'
184 }
185 await uploadVideo(server.url, accessTokenUser, videoAttributes)
186 })
187
188 it('Should have video quota updated', async function () {
189 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
190 const data = res.body
191
192 expect(data.videoQuotaUsed).to.equal(218910)
193 })
194
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)
198
199 const videos = res.body.data
200 expect(videos).to.have.lengthOf(1)
201
202 expect(videos[ 0 ].name).to.equal('super user video')
203 })
204
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
210
211 expect(total).to.equal(2)
212 expect(users).to.be.an('array')
213 expect(users.length).to.equal(2)
214
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.nsfwPolicy).to.equal('display')
219
220 const rootUser = users[ 1 ]
221 expect(rootUser.username).to.equal('root')
222 expect(rootUser.email).to.equal('admin1@example.com')
223 expect(user.nsfwPolicy).to.equal('display')
224
225 userId = user.id
226 })
227
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')
230
231 const result = res.body
232 const total = result.total
233 const users = result.data
234
235 expect(total).to.equal(2)
236 expect(users.length).to.equal(1)
237
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.nsfwPolicy).to.equal('display')
243 })
244
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
250
251 expect(total).to.equal(2)
252 expect(users.length).to.equal(1)
253
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.nsfwPolicy).to.equal('display')
258 })
259
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
265
266 expect(total).to.equal(2)
267 expect(users.length).to.equal(1)
268
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.nsfwPolicy).to.equal('display')
273 })
274
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
280
281 expect(total).to.equal(2)
282 expect(users.length).to.equal(2)
283
284 expect(users[ 0 ].username).to.equal('root')
285 expect(users[ 0 ].email).to.equal('admin1@example.com')
286 expect(users[ 0 ].nsfwPolicy).to.equal('display')
287
288 expect(users[ 1 ].username).to.equal('user_1')
289 expect(users[ 1 ].email).to.equal('user_1@example.com')
290 expect(users[ 1 ].nsfwPolicy).to.equal('display')
291 })
292
293 it('Should update my password', async function () {
294 await updateMyUser({
295 url: server.url,
296 accessToken: accessTokenUser,
297 newPassword: 'new password'
298 })
299 user.password = 'new password'
300
301 await userLogin(server, user, 200)
302 })
303
304 it('Should be able to change the NSFW display attribute', async function () {
305 await updateMyUser({
306 url: server.url,
307 accessToken: accessTokenUser,
308 nsfwPolicy: 'do_not_list'
309 })
310
311 const res = await getMyUserInformation(server.url, accessTokenUser)
312 const user = res.body
313
314 expect(user.username).to.equal('user_1')
315 expect(user.email).to.equal('user_1@example.com')
316 expect(user.nsfwPolicy).to.equal('do_not_list')
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
320 })
321
322 it('Should be able to change the autoPlayVideo attribute', async function () {
323 await updateMyUser({
324 url: server.url,
325 accessToken: accessTokenUser,
326 autoPlayVideo: false
327 })
328
329 const res = await getMyUserInformation(server.url, accessTokenUser)
330 const user = res.body
331
332 expect(user.autoPlayVideo).to.be.false
333 })
334
335 it('Should be able to change the email display attribute', async function () {
336 await updateMyUser({
337 url: server.url,
338 accessToken: accessTokenUser,
339 email: 'updated@example.com'
340 })
341
342 const res = await getMyUserInformation(server.url, accessTokenUser)
343 const user = res.body
344
345 expect(user.username).to.equal('user_1')
346 expect(user.email).to.equal('updated@example.com')
347 expect(user.nsfwPolicy).to.equal('do_not_list')
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
351 })
352
353 it('Should be able to update my avatar', async function () {
354 const fixture = 'avatar.png'
355
356 await updateMyAvatar({
357 url: server.url,
358 accessToken: accessTokenUser,
359 fixture
360 })
361
362 const res = await getMyUserInformation(server.url, accessTokenUser)
363 const user = res.body
364
365 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
366 })
367
368 it('Should be able to update my description', async function () {
369 await updateMyUser({
370 url: server.url,
371 accessToken: accessTokenUser,
372 description: 'my super description updated'
373 })
374
375 const res = await getMyUserInformation(server.url, accessTokenUser)
376 const user = res.body
377
378 expect(user.username).to.equal('user_1')
379 expect(user.email).to.equal('updated@example.com')
380 expect(user.nsfwPolicy).to.equal('do_not_list')
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')
384 })
385
386 it('Should be able to update another user', async function () {
387 await updateUser({
388 url: server.url,
389 userId,
390 accessToken,
391 email: 'updated2@example.com',
392 videoQuota: 42,
393 role: UserRole.MODERATOR
394 })
395
396 const res = await getUserInformation(server.url, accessToken, userId)
397 const user = res.body
398
399 expect(user.username).to.equal('user_1')
400 expect(user.email).to.equal('updated2@example.com')
401 expect(user.nsfwPolicy).to.equal('do_not_list')
402 expect(user.videoQuota).to.equal(42)
403 expect(user.roleLabel).to.equal('Moderator')
404 expect(user.id).to.be.a('number')
405 })
406
407 it('Should have removed the user token', async function () {
408 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
409
410 accessTokenUser = await userLogin(server, user)
411 })
412
413 it('Should not be able to delete a user by a moderator', async function () {
414 await removeUser(server.url, 2, accessTokenUser, 403)
415 })
416
417 it('Should be able to list video blacklist by a moderator', async function () {
418 await getBlacklistedVideosList(server.url, accessTokenUser)
419 })
420
421 it('Should be able to remove this user', async function () {
422 await removeUser(server.url, userId, accessToken)
423 })
424
425 it('Should not be able to login with this user', async function () {
426 await userLogin(server, user, 400)
427 })
428
429 it('Should not have videos of this user', async function () {
430 const res = await getVideosList(server.url)
431
432 expect(res.body.total).to.equal(1)
433
434 const video = res.body.data[ 0 ]
435 expect(video.account.name).to.equal('root')
436 })
437
438 it('Should register a new user', async function () {
439 await registerUser(server.url, 'user_15', 'my super password')
440 })
441
442 it('Should be able to login with this registered user', async function () {
443 const user15 = {
444 username: 'user_15',
445 password: 'my super password'
446 }
447
448 accessToken = await userLogin(server, user15)
449 })
450
451 it('Should have the correct video quota', async function () {
452 const res = await getMyUserInformation(server.url, accessToken)
453 const user = res.body
454
455 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
456 })
457
458 after(async function () {
459 killallServers([ server ])
460
461 // Keep the logs if the test failed
462 if (this[ 'ok' ]) {
463 await flushTests()
464 }
465 })
466 })