]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users.ts
enable email verification by admin (#1348)
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
CommitLineData
0e1dc3e7 1/* tslint:disable:no-unused-expression */
86d13ec2 2
0e1dc3e7 3import * as chai from 'chai'
afffe988 4import 'mocha'
a76138ff 5import { User, UserRole } from '../../../../shared/index'
0e1dc3e7 6import {
a890d1e0
C
7 blockUser,
8 createUser,
9 deleteMe,
10 flushTests,
11 getBlacklistedVideosList,
12 getMyUserInformation,
13 getMyUserVideoQuotaUsed,
14 getMyUserVideoRating,
15 getUserInformation,
16 getUsersList,
17 getUsersListPaginationAndSort,
18 getVideosList,
19 killallServers,
20 login,
21 makePutBodyRequest,
22 rateVideo,
23 registerUser,
24 removeUser,
25 removeVideo,
26 runServer,
27 ServerInfo,
28 testImage,
29 unblockUser,
30 updateMyAvatar,
31 updateMyUser,
32 updateUser,
33 uploadVideo,
34 userLogin
c5d31dba
C
35} from '../../utils/index'
36import { follow } from '../../utils/server/follows'
c5d31dba 37import { setAccessTokensToServers } from '../../utils/users/login'
11ba2ab3 38import { getMyVideos } from '../../utils/videos/videos'
0e1dc3e7 39
afffe988
C
40const expect = chai.expect
41
0e1dc3e7
C
42describe('Test users', function () {
43 let server: ServerInfo
44 let accessToken: string
45 let accessTokenUser: string
46 let videoId: number
47 let userId: number
f8b8c36b
C
48 const user = {
49 username: 'user_1',
50 password: 'super password'
51 }
0e1dc3e7
C
52
53 before(async function () {
e212f887 54 this.timeout(30000)
0e1dc3e7
C
55
56 await flushTests()
57 server = await runServer(1)
86d13ec2
C
58
59 await setAccessTokensToServers([ server ])
0e1dc3e7
C
60 })
61
62 it('Should create a new client')
63
64 it('Should return the first client')
65
66 it('Should remove the last client')
67
68 it('Should not login with an invalid client id', async function () {
69 const client = { id: 'client', secret: server.client.secret }
70 const res = await login(server.url, client, server.user, 400)
71
e6921918 72 expect(res.body.error).to.contain('client is invalid')
0e1dc3e7
C
73 })
74
75 it('Should not login with an invalid client secret', async function () {
76 const client = { id: server.client.id, secret: 'coucou' }
77 const res = await login(server.url, client, server.user, 400)
78
e6921918 79 expect(res.body.error).to.contain('client is invalid')
0e1dc3e7
C
80 })
81
82 it('Should not login with an invalid username', async function () {
83 const user = { username: 'captain crochet', password: server.user.password }
84 const res = await login(server.url, server.client, user, 400)
85
e6921918 86 expect(res.body.error).to.contain('credentials are invalid')
0e1dc3e7
C
87 })
88
89 it('Should not login with an invalid password', async function () {
5f04dd2f 90 const user = { username: server.user.username, password: 'mew_three' }
0e1dc3e7
C
91 const res = await login(server.url, server.client, user, 400)
92
e6921918 93 expect(res.body.error).to.contain('credentials are invalid')
0e1dc3e7
C
94 })
95
96 it('Should not be able to upload a video', async function () {
97 accessToken = 'my_super_token'
98
99 const videoAttributes = {}
100 await uploadVideo(server.url, accessToken, videoAttributes, 401)
101 })
102
afffe988 103 it('Should not be able to follow', async function () {
0e1dc3e7 104 accessToken = 'my_super_token'
afffe988 105 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
0e1dc3e7
C
106 })
107
afffe988 108 it('Should not be able to unfollow')
0e1dc3e7
C
109
110 it('Should be able to login', async function () {
111 const res = await login(server.url, server.client, server.user, 200)
112
113 accessToken = res.body.access_token
114 })
115
116 it('Should upload the video with the correct token', async function () {
117 const videoAttributes = {}
04e0fc48 118 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7 119 const res = await getVideosList(server.url)
afffe988 120 const video = res.body.data[ 0 ]
0e1dc3e7 121
b64c950a 122 expect(video.account.name).to.equal('root')
0e1dc3e7
C
123 videoId = video.id
124 })
125
126 it('Should upload the video again with the correct token', async function () {
127 const videoAttributes = {}
04e0fc48 128 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7
C
129 })
130
131 it('Should retrieve a video rating', async function () {
132 await rateVideo(server.url, accessToken, videoId, 'like')
26d21b78 133 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
0e1dc3e7
C
134 const rating = res.body
135
a7ba16b6
C
136 expect(rating.videoId).to.equal(videoId)
137 expect(rating.rating).to.equal('like')
0e1dc3e7
C
138 })
139
140 it('Should not be able to remove the video with an incorrect token', async function () {
141 await removeVideo(server.url, 'bad_token', videoId, 401)
142 })
143
144 it('Should not be able to remove the video with the token of another account')
145
146 it('Should be able to remove the video with the correct token', async function () {
147 await removeVideo(server.url, accessToken, videoId)
148 })
149
150 it('Should logout (revoke token)')
151
152 it('Should not be able to get the user information')
153
154 it('Should not be able to upload a video')
155
156 it('Should not be able to remove a video')
157
158 it('Should not be able to rate a video', async function () {
159 const path = '/api/v1/videos/'
160 const data = {
161 rating: 'likes'
162 }
163
164 const options = {
165 url: server.url,
166 path: path + videoId,
167 token: 'wrong token',
168 fields: data,
169 statusCodeExpected: 401
170 }
171 await makePutBodyRequest(options)
172 })
173
174 it('Should be able to login again')
175
176 it('Should have an expired access token')
177
178 it('Should refresh the token')
179
180 it('Should be able to upload a video again')
181
182 it('Should be able to create a new user', async function () {
24b9417c 183 await createUser(server.url, accessToken, user.username, user.password, 2 * 1024 * 1024)
0e1dc3e7
C
184 })
185
186 it('Should be able to login with this user', async function () {
f8b8c36b 187 accessTokenUser = await userLogin(server, user)
0e1dc3e7
C
188 })
189
190 it('Should be able to get the user information', async function () {
5c98d3bf 191 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
192 const user = res.body
193
a7ba16b6
C
194 expect(user.username).to.equal('user_1')
195 expect(user.email).to.equal('user_1@example.com')
0883b324 196 expect(user.nsfwPolicy).to.equal('display')
a7ba16b6
C
197 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
198 expect(user.roleLabel).to.equal('User')
199 expect(user.id).to.be.a('number')
ed56ad11 200 expect(user.account.displayName).to.equal('user_1')
2422c46b 201 expect(user.account.description).to.be.null
0e1dc3e7
C
202 })
203
204 it('Should be able to upload a video with this user', async function () {
205 this.timeout(5000)
206
11474c3c 207 const videoAttributes = {
ce5496d6
C
208 name: 'super user video',
209 fixture: 'video_short.webm'
11474c3c 210 }
0e1dc3e7
C
211 await uploadVideo(server.url, accessTokenUser, videoAttributes)
212 })
213
ce5496d6
C
214 it('Should have video quota updated', async function () {
215 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
216 const data = res.body
217
218 expect(data.videoQuotaUsed).to.equal(218910)
a76138ff
C
219
220 const resUsers = await getUsersList(server.url, server.accessToken)
221
222 const users: User[] = resUsers.body.data
223 const tmpUser = users.find(u => u.username === user.username)
224 expect(tmpUser.videoQuotaUsed).to.equal(218910)
ce5496d6
C
225 })
226
11474c3c
C
227 it('Should be able to list my videos', async function () {
228 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
a7ba16b6 229 expect(res.body.total).to.equal(1)
11474c3c
C
230
231 const videos = res.body.data
a7ba16b6 232 expect(videos).to.have.lengthOf(1)
11474c3c 233
a7ba16b6 234 expect(videos[ 0 ].name).to.equal('super user video')
11474c3c
C
235 })
236
0e1dc3e7 237 it('Should list all the users', async function () {
86d13ec2 238 const res = await getUsersList(server.url, server.accessToken)
0e1dc3e7
C
239 const result = res.body
240 const total = result.total
241 const users = result.data
242
a7ba16b6
C
243 expect(total).to.equal(2)
244 expect(users).to.be.an('array')
245 expect(users.length).to.equal(2)
afffe988
C
246
247 const user = users[ 0 ]
a7ba16b6
C
248 expect(user.username).to.equal('user_1')
249 expect(user.email).to.equal('user_1@example.com')
0883b324 250 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7 251
afffe988 252 const rootUser = users[ 1 ]
a7ba16b6
C
253 expect(rootUser.username).to.equal('root')
254 expect(rootUser.email).to.equal('admin1@example.com')
0883b324 255 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
256
257 userId = user.id
258 })
259
260 it('Should list only the first user by username asc', async function () {
86d13ec2 261 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
0e1dc3e7
C
262
263 const result = res.body
264 const total = result.total
265 const users = result.data
266
a7ba16b6
C
267 expect(total).to.equal(2)
268 expect(users.length).to.equal(1)
afffe988
C
269
270 const user = users[ 0 ]
a7ba16b6
C
271 expect(user.username).to.equal('root')
272 expect(user.email).to.equal('admin1@example.com')
273 expect(user.roleLabel).to.equal('Administrator')
0883b324 274 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
275 })
276
277 it('Should list only the first user by username desc', async function () {
86d13ec2 278 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
0e1dc3e7
C
279 const result = res.body
280 const total = result.total
281 const users = result.data
282
a7ba16b6
C
283 expect(total).to.equal(2)
284 expect(users.length).to.equal(1)
afffe988
C
285
286 const user = users[ 0 ]
a7ba16b6
C
287 expect(user.username).to.equal('user_1')
288 expect(user.email).to.equal('user_1@example.com')
0883b324 289 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
290 })
291
292 it('Should list only the second user by createdAt desc', async function () {
86d13ec2 293 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
0e1dc3e7
C
294 const result = res.body
295 const total = result.total
296 const users = result.data
297
a7ba16b6
C
298 expect(total).to.equal(2)
299 expect(users.length).to.equal(1)
afffe988
C
300
301 const user = users[ 0 ]
a7ba16b6
C
302 expect(user.username).to.equal('user_1')
303 expect(user.email).to.equal('user_1@example.com')
0883b324 304 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
305 })
306
307 it('Should list all the users by createdAt asc', async function () {
86d13ec2 308 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
0e1dc3e7
C
309 const result = res.body
310 const total = result.total
311 const users = result.data
312
a7ba16b6
C
313 expect(total).to.equal(2)
314 expect(users.length).to.equal(2)
315
316 expect(users[ 0 ].username).to.equal('root')
317 expect(users[ 0 ].email).to.equal('admin1@example.com')
0883b324 318 expect(users[ 0 ].nsfwPolicy).to.equal('display')
afffe988 319
a7ba16b6
C
320 expect(users[ 1 ].username).to.equal('user_1')
321 expect(users[ 1 ].email).to.equal('user_1@example.com')
0883b324 322 expect(users[ 1 ].nsfwPolicy).to.equal('display')
0e1dc3e7
C
323 })
324
24b9417c
C
325 it('Should search user by username', async function () {
326 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
327 const users = res.body.data as User[]
328
329 expect(res.body.total).to.equal(1)
330 expect(users.length).to.equal(1)
331
332 expect(users[ 0 ].username).to.equal('root')
333 })
334
335 it('Should search user by email', async function () {
336 {
337 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
338 const users = res.body.data as User[]
339
340 expect(res.body.total).to.equal(1)
341 expect(users.length).to.equal(1)
342
343 expect(users[ 0 ].username).to.equal('user_1')
344 expect(users[ 0 ].email).to.equal('user_1@example.com')
345 }
346
347 {
348 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
349 const users = res.body.data as User[]
350
351 expect(res.body.total).to.equal(2)
352 expect(users.length).to.equal(2)
353
354 expect(users[ 0 ].username).to.equal('root')
355 expect(users[ 1 ].username).to.equal('user_1')
356 }
357 })
358
5c98d3bf 359 it('Should update my password', async function () {
11ba2ab3
C
360 await updateMyUser({
361 url: server.url,
362 accessToken: accessTokenUser,
a890d1e0 363 currentPassword: 'super password',
11ba2ab3
C
364 newPassword: 'new password'
365 })
f8b8c36b 366 user.password = 'new password'
0e1dc3e7 367
f8b8c36b 368 await userLogin(server, user, 200)
0e1dc3e7
C
369 })
370
371 it('Should be able to change the NSFW display attribute', async function () {
11ba2ab3
C
372 await updateMyUser({
373 url: server.url,
374 accessToken: accessTokenUser,
0883b324 375 nsfwPolicy: 'do_not_list'
11ba2ab3 376 })
0e1dc3e7 377
5c98d3bf 378 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
379 const user = res.body
380
a7ba16b6
C
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('user_1@example.com')
0883b324 383 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
384 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
385 expect(user.id).to.be.a('number')
ed56ad11 386 expect(user.account.displayName).to.equal('user_1')
2422c46b 387 expect(user.account.description).to.be.null
5c98d3bf
C
388 })
389
7efe153b 390 it('Should be able to change the autoPlayVideo attribute', async function () {
11ba2ab3
C
391 await updateMyUser({
392 url: server.url,
393 accessToken: accessTokenUser,
394 autoPlayVideo: false
395 })
7efe153b
AL
396
397 const res = await getMyUserInformation(server.url, accessTokenUser)
398 const user = res.body
399
400 expect(user.autoPlayVideo).to.be.false
401 })
402
5c98d3bf 403 it('Should be able to change the email display attribute', async function () {
11ba2ab3
C
404 await updateMyUser({
405 url: server.url,
406 accessToken: accessTokenUser,
407 email: 'updated@example.com'
408 })
5c98d3bf
C
409
410 const res = await getMyUserInformation(server.url, accessTokenUser)
411 const user = res.body
412
a7ba16b6
C
413 expect(user.username).to.equal('user_1')
414 expect(user.email).to.equal('updated@example.com')
0883b324 415 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
416 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
417 expect(user.id).to.be.a('number')
ed56ad11 418 expect(user.account.displayName).to.equal('user_1')
2422c46b 419 expect(user.account.description).to.be.null
5c98d3bf
C
420 })
421
c5911fd3
C
422 it('Should be able to update my avatar', async function () {
423 const fixture = 'avatar.png'
424
425 await updateMyAvatar({
426 url: server.url,
427 accessToken: accessTokenUser,
428 fixture
429 })
430
431 const res = await getMyUserInformation(server.url, accessTokenUser)
432 const user = res.body
433
7b0956ec 434 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
c5911fd3
C
435 })
436
ed56ad11
C
437 it('Should be able to update my display name', async function () {
438 await updateMyUser({
439 url: server.url,
440 accessToken: accessTokenUser,
441 displayName: 'new display name'
442 })
443
444 const res = await getMyUserInformation(server.url, accessTokenUser)
445 const user = res.body
446
447 expect(user.username).to.equal('user_1')
448 expect(user.email).to.equal('updated@example.com')
449 expect(user.nsfwPolicy).to.equal('do_not_list')
450 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
451 expect(user.id).to.be.a('number')
452 expect(user.account.displayName).to.equal('new display name')
453 expect(user.account.description).to.be.null
454 })
455
2422c46b
C
456 it('Should be able to update my description', async function () {
457 await updateMyUser({
458 url: server.url,
459 accessToken: accessTokenUser,
460 description: 'my super description updated'
461 })
462
463 const res = await getMyUserInformation(server.url, accessTokenUser)
464 const user = res.body
465
466 expect(user.username).to.equal('user_1')
467 expect(user.email).to.equal('updated@example.com')
0883b324 468 expect(user.nsfwPolicy).to.equal('do_not_list')
2422c46b
C
469 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
470 expect(user.id).to.be.a('number')
ed56ad11 471 expect(user.account.displayName).to.equal('new display name')
2422c46b
C
472 expect(user.account.description).to.equal('my super description updated')
473 })
474
5c98d3bf 475 it('Should be able to update another user', async function () {
11ba2ab3
C
476 await updateUser({
477 url: server.url,
478 userId,
479 accessToken,
480 email: 'updated2@example.com',
fc2ec87a 481 emailVerified: true,
11ba2ab3
C
482 videoQuota: 42,
483 role: UserRole.MODERATOR
484 })
5c98d3bf 485
77a5501f 486 const res = await getUserInformation(server.url, accessToken, userId)
5c98d3bf
C
487 const user = res.body
488
a7ba16b6
C
489 expect(user.username).to.equal('user_1')
490 expect(user.email).to.equal('updated2@example.com')
fc2ec87a 491 expect(user.emailVerified).to.be.true
0883b324 492 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
493 expect(user.videoQuota).to.equal(42)
494 expect(user.roleLabel).to.equal('Moderator')
495 expect(user.id).to.be.a('number')
0e1dc3e7
C
496 })
497
f8b8c36b
C
498 it('Should have removed the user token', async function () {
499 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
500
501 accessTokenUser = await userLogin(server, user)
502 })
503
757f0da3
C
504 it('Should not be able to delete a user by a moderator', async function () {
505 await removeUser(server.url, 2, accessTokenUser, 403)
506 })
507
508 it('Should be able to list video blacklist by a moderator', async function () {
509 await getBlacklistedVideosList(server.url, accessTokenUser)
510 })
511
0e1dc3e7
C
512 it('Should be able to remove this user', async function () {
513 await removeUser(server.url, userId, accessToken)
514 })
515
516 it('Should not be able to login with this user', async function () {
f8b8c36b 517 await userLogin(server, user, 400)
0e1dc3e7
C
518 })
519
520 it('Should not have videos of this user', async function () {
521 const res = await getVideosList(server.url)
522
a7ba16b6 523 expect(res.body.total).to.equal(1)
0e1dc3e7 524
afffe988 525 const video = res.body.data[ 0 ]
b64c950a 526 expect(video.account.name).to.equal('root')
0e1dc3e7
C
527 })
528
529 it('Should register a new user', async function () {
530 await registerUser(server.url, 'user_15', 'my super password')
531 })
532
533 it('Should be able to login with this registered user', async function () {
f8b8c36b 534 const user15 = {
0e1dc3e7
C
535 username: 'user_15',
536 password: 'my super password'
537 }
538
f8b8c36b 539 accessToken = await userLogin(server, user15)
5c98d3bf
C
540 })
541
542 it('Should have the correct video quota', async function () {
543 const res = await getMyUserInformation(server.url, accessToken)
544 const user = res.body
545
a7ba16b6 546 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
0e1dc3e7
C
547 })
548
92b9d60c
C
549 it('Should remove me', async function () {
550 {
551 const res = await getUsersList(server.url, server.accessToken)
552 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
553 }
554
555 await deleteMe(server.url, accessToken)
556
557 {
558 const res = await getUsersList(server.url, server.accessToken)
559 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
560 }
561 })
562
e6921918
C
563 it('Should block and unblock a user', async function () {
564 const user16 = {
565 username: 'user_16',
566 password: 'my super password'
567 }
568 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
569 const user16Id = resUser.body.user.id
570
571 accessToken = await userLogin(server, user16)
572
573 await getMyUserInformation(server.url, accessToken, 200)
574 await blockUser(server.url, user16Id, server.accessToken)
575
576 await getMyUserInformation(server.url, accessToken, 401)
577 await userLogin(server, user16, 400)
578
579 await unblockUser(server.url, user16Id, server.accessToken)
580 accessToken = await userLogin(server, user16)
581 await getMyUserInformation(server.url, accessToken, 200)
582 })
583
0e1dc3e7
C
584 after(async function () {
585 killallServers([ server ])
586
587 // Keep the logs if the test failed
afffe988 588 if (this[ 'ok' ]) {
0e1dc3e7
C
589 await flushTests()
590 }
591 })
592})