Merge branch 'release/v1.3.0' into develop
[oweals/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 { User, UserRole, Video } from '../../../../shared/index'
6 import {
7   blockUser,
8   cleanupTests,
9   createUser,
10   deleteMe,
11   flushAndRunServer,
12   getAccountRatings,
13   getBlacklistedVideosList,
14   getMyUserInformation,
15   getMyUserVideoQuotaUsed,
16   getMyUserVideoRating,
17   getUserInformation,
18   getUsersList,
19   getUsersListPaginationAndSort,
20   getVideosList,
21   login,
22   makePutBodyRequest,
23   rateVideo,
24   registerUser,
25   removeUser,
26   removeVideo,
27   ServerInfo,
28   testImage,
29   unblockUser,
30   updateMyAvatar,
31   updateMyUser,
32   updateUser,
33   uploadVideo,
34   userLogin,
35   registerUserWithChannel, getVideoChannel
36 } from '../../../../shared/extra-utils'
37 import { follow } from '../../../../shared/extra-utils/server/follows'
38 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
39 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
40 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
41
42 const expect = chai.expect
43
44 describe('Test users', function () {
45   let server: ServerInfo
46   let accessToken: string
47   let accessTokenUser: string
48   let videoId: number
49   let userId: number
50   const user = {
51     username: 'user_1',
52     password: 'super password'
53   }
54
55   before(async function () {
56     this.timeout(30000)
57     server = await flushAndRunServer(1)
58
59     await setAccessTokensToServers([ server ])
60   })
61
62   describe('OAuth client', function () {
63     it('Should create a new client')
64
65     it('Should return the first client')
66
67     it('Should remove the last client')
68
69     it('Should not login with an invalid client id', async function () {
70       const client = { id: 'client', secret: server.client.secret }
71       const res = await login(server.url, client, server.user, 400)
72
73       expect(res.body.error).to.contain('client is invalid')
74     })
75
76     it('Should not login with an invalid client secret', async function () {
77       const client = { id: server.client.id, secret: 'coucou' }
78       const res = await login(server.url, client, server.user, 400)
79
80       expect(res.body.error).to.contain('client is invalid')
81     })
82   })
83
84   describe('Login', function () {
85
86     it('Should not login with an invalid username', async function () {
87       const user = { username: 'captain crochet', password: server.user.password }
88       const res = await login(server.url, server.client, user, 400)
89
90       expect(res.body.error).to.contain('credentials are invalid')
91     })
92
93     it('Should not login with an invalid password', async function () {
94       const user = { username: server.user.username, password: 'mew_three' }
95       const res = await login(server.url, server.client, user, 400)
96
97       expect(res.body.error).to.contain('credentials are invalid')
98     })
99
100     it('Should not be able to upload a video', async function () {
101       accessToken = 'my_super_token'
102
103       const videoAttributes = {}
104       await uploadVideo(server.url, accessToken, videoAttributes, 401)
105     })
106
107     it('Should not be able to follow', async function () {
108       accessToken = 'my_super_token'
109       await follow(server.url, [ 'http://example.com' ], accessToken, 401)
110     })
111
112     it('Should not be able to unfollow')
113
114     it('Should be able to login', async function () {
115       const res = await login(server.url, server.client, server.user, 200)
116
117       accessToken = res.body.access_token
118     })
119   })
120
121   describe('Upload', function () {
122
123     it('Should upload the video with the correct token', async function () {
124       const videoAttributes = {}
125       await uploadVideo(server.url, accessToken, videoAttributes)
126       const res = await getVideosList(server.url)
127       const video = res.body.data[ 0 ]
128
129       expect(video.account.name).to.equal('root')
130       videoId = video.id
131     })
132
133     it('Should upload the video again with the correct token', async function () {
134       const videoAttributes = {}
135       await uploadVideo(server.url, accessToken, videoAttributes)
136     })
137   })
138
139   describe('Ratings', function () {
140
141     it('Should retrieve a video rating', async function () {
142       await rateVideo(server.url, accessToken, videoId, 'like')
143       const res = await getMyUserVideoRating(server.url, accessToken, videoId)
144       const rating = res.body
145
146       expect(rating.videoId).to.equal(videoId)
147       expect(rating.rating).to.equal('like')
148     })
149
150     it('Should retrieve ratings list', async function () {
151       await rateVideo(server.url, accessToken, videoId, 'like')
152
153       const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
154       const ratings = res.body
155
156       expect(ratings.total).to.equal(1)
157       expect(ratings.data[ 0 ].video.id).to.equal(videoId)
158       expect(ratings.data[ 0 ].rating).to.equal('like')
159     })
160
161     it('Should retrieve ratings list by rating type', async function () {
162       {
163         const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
164         const ratings = res.body
165         expect(ratings.data.length).to.equal(1)
166       }
167
168       {
169         const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
170         const ratings = res.body
171         expect(ratings.data.length).to.equal(0)
172       }
173     })
174   })
175
176   describe('Remove video', function () {
177     it('Should not be able to remove the video with an incorrect token', async function () {
178       await removeVideo(server.url, 'bad_token', videoId, 401)
179     })
180
181     it('Should not be able to remove the video with the token of another account')
182
183     it('Should be able to remove the video with the correct token', async function () {
184       await removeVideo(server.url, accessToken, videoId)
185     })
186   })
187
188   describe('Logout', function () {
189     it('Should logout (revoke token)')
190
191     it('Should not be able to get the user information')
192
193     it('Should not be able to upload a video')
194
195     it('Should not be able to remove a video')
196
197     it('Should not be able to rate a video', async function () {
198       const path = '/api/v1/videos/'
199       const data = {
200         rating: 'likes'
201       }
202
203       const options = {
204         url: server.url,
205         path: path + videoId,
206         token: 'wrong token',
207         fields: data,
208         statusCodeExpected: 401
209       }
210       await makePutBodyRequest(options)
211     })
212
213     it('Should be able to login again')
214
215     it('Should have an expired access token')
216
217     it('Should refresh the token')
218
219     it('Should be able to upload a video again')
220   })
221
222   describe('Creating a user', function () {
223
224     it('Should be able to create a new user', async function () {
225       await createUser({
226         url: server.url,
227         accessToken: accessToken,
228         username: user.username,
229         password: user.password,
230         videoQuota: 2 * 1024 * 1024,
231         adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
232       })
233     })
234
235     it('Should be able to login with this user', async function () {
236       accessTokenUser = await userLogin(server, user)
237     })
238
239     it('Should be able to get user information', async function () {
240       const res1 = await getMyUserInformation(server.url, accessTokenUser)
241       const userMe: User = res1.body
242
243       const res2 = await getUserInformation(server.url, server.accessToken, userMe.id)
244       const userGet: User = res2.body
245
246       for (const user of [ userMe, userGet ]) {
247         expect(user.username).to.equal('user_1')
248         expect(user.email).to.equal('user_1@example.com')
249         expect(user.nsfwPolicy).to.equal('display')
250         expect(user.videoQuota).to.equal(2 * 1024 * 1024)
251         expect(user.roleLabel).to.equal('User')
252         expect(user.id).to.be.a('number')
253         expect(user.account.displayName).to.equal('user_1')
254         expect(user.account.description).to.be.null
255       }
256
257       expect(userMe.adminFlags).to.be.undefined
258       expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)
259     })
260   })
261
262   describe('My videos & quotas', function () {
263
264     it('Should be able to upload a video with this user', async function () {
265       this.timeout(5000)
266
267       const videoAttributes = {
268         name: 'super user video',
269         fixture: 'video_short.webm'
270       }
271       await uploadVideo(server.url, accessTokenUser, videoAttributes)
272     })
273
274     it('Should have video quota updated', async function () {
275       const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
276       const data = res.body
277
278       expect(data.videoQuotaUsed).to.equal(218910)
279
280       const resUsers = await getUsersList(server.url, server.accessToken)
281
282       const users: User[] = resUsers.body.data
283       const tmpUser = users.find(u => u.username === user.username)
284       expect(tmpUser.videoQuotaUsed).to.equal(218910)
285     })
286
287     it('Should be able to list my videos', async function () {
288       const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
289       expect(res.body.total).to.equal(1)
290
291       const videos = res.body.data
292       expect(videos).to.have.lengthOf(1)
293
294       const video: Video = videos[ 0 ]
295       expect(video.name).to.equal('super user video')
296       expect(video.thumbnailPath).to.not.be.null
297       expect(video.previewPath).to.not.be.null
298     })
299   })
300
301   describe('Users listing', function () {
302
303     it('Should list all the users', async function () {
304       const res = await getUsersList(server.url, server.accessToken)
305       const result = res.body
306       const total = result.total
307       const users = result.data
308
309       expect(total).to.equal(2)
310       expect(users).to.be.an('array')
311       expect(users.length).to.equal(2)
312
313       const user = users[ 0 ]
314       expect(user.username).to.equal('user_1')
315       expect(user.email).to.equal('user_1@example.com')
316       expect(user.nsfwPolicy).to.equal('display')
317
318       const rootUser = users[ 1 ]
319       expect(rootUser.username).to.equal('root')
320       expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
321       expect(user.nsfwPolicy).to.equal('display')
322
323       userId = user.id
324     })
325
326     it('Should list only the first user by username asc', async function () {
327       const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
328
329       const result = res.body
330       const total = result.total
331       const users = result.data
332
333       expect(total).to.equal(2)
334       expect(users.length).to.equal(1)
335
336       const user = users[ 0 ]
337       expect(user.username).to.equal('root')
338       expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
339       expect(user.roleLabel).to.equal('Administrator')
340       expect(user.nsfwPolicy).to.equal('display')
341     })
342
343     it('Should list only the first user by username desc', async function () {
344       const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
345       const result = res.body
346       const total = result.total
347       const users = result.data
348
349       expect(total).to.equal(2)
350       expect(users.length).to.equal(1)
351
352       const user = users[ 0 ]
353       expect(user.username).to.equal('user_1')
354       expect(user.email).to.equal('user_1@example.com')
355       expect(user.nsfwPolicy).to.equal('display')
356     })
357
358     it('Should list only the second user by createdAt desc', async function () {
359       const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
360       const result = res.body
361       const total = result.total
362       const users = result.data
363
364       expect(total).to.equal(2)
365       expect(users.length).to.equal(1)
366
367       const user = users[ 0 ]
368       expect(user.username).to.equal('user_1')
369       expect(user.email).to.equal('user_1@example.com')
370       expect(user.nsfwPolicy).to.equal('display')
371     })
372
373     it('Should list all the users by createdAt asc', async function () {
374       const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
375       const result = res.body
376       const total = result.total
377       const users = result.data
378
379       expect(total).to.equal(2)
380       expect(users.length).to.equal(2)
381
382       expect(users[ 0 ].username).to.equal('root')
383       expect(users[ 0 ].email).to.equal('admin' + server.internalServerNumber + '@example.com')
384       expect(users[ 0 ].nsfwPolicy).to.equal('display')
385
386       expect(users[ 1 ].username).to.equal('user_1')
387       expect(users[ 1 ].email).to.equal('user_1@example.com')
388       expect(users[ 1 ].nsfwPolicy).to.equal('display')
389     })
390
391     it('Should search user by username', async function () {
392       const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
393       const users = res.body.data as User[]
394
395       expect(res.body.total).to.equal(1)
396       expect(users.length).to.equal(1)
397
398       expect(users[ 0 ].username).to.equal('root')
399     })
400
401     it('Should search user by email', async function () {
402       {
403         const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
404         const users = res.body.data as User[]
405
406         expect(res.body.total).to.equal(1)
407         expect(users.length).to.equal(1)
408
409         expect(users[ 0 ].username).to.equal('user_1')
410         expect(users[ 0 ].email).to.equal('user_1@example.com')
411       }
412
413       {
414         const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
415         const users = res.body.data as User[]
416
417         expect(res.body.total).to.equal(2)
418         expect(users.length).to.equal(2)
419
420         expect(users[ 0 ].username).to.equal('root')
421         expect(users[ 1 ].username).to.equal('user_1')
422       }
423     })
424   })
425
426   describe('Update my account', function () {
427     it('Should update my password', async function () {
428       await updateMyUser({
429         url: server.url,
430         accessToken: accessTokenUser,
431         currentPassword: 'super password',
432         newPassword: 'new password'
433       })
434       user.password = 'new password'
435
436       await userLogin(server, user, 200)
437     })
438
439     it('Should be able to change the NSFW display attribute', async function () {
440       await updateMyUser({
441         url: server.url,
442         accessToken: accessTokenUser,
443         nsfwPolicy: 'do_not_list'
444       })
445
446       const res = await getMyUserInformation(server.url, accessTokenUser)
447       const user = res.body
448
449       expect(user.username).to.equal('user_1')
450       expect(user.email).to.equal('user_1@example.com')
451       expect(user.nsfwPolicy).to.equal('do_not_list')
452       expect(user.videoQuota).to.equal(2 * 1024 * 1024)
453       expect(user.id).to.be.a('number')
454       expect(user.account.displayName).to.equal('user_1')
455       expect(user.account.description).to.be.null
456     })
457
458     it('Should be able to change the autoPlayVideo attribute', async function () {
459       await updateMyUser({
460         url: server.url,
461         accessToken: accessTokenUser,
462         autoPlayVideo: false
463       })
464
465       const res = await getMyUserInformation(server.url, accessTokenUser)
466       const user = res.body
467
468       expect(user.autoPlayVideo).to.be.false
469     })
470
471     it('Should be able to change the email display attribute', async function () {
472       await updateMyUser({
473         url: server.url,
474         accessToken: accessTokenUser,
475         email: 'updated@example.com'
476       })
477
478       const res = await getMyUserInformation(server.url, accessTokenUser)
479       const user = res.body
480
481       expect(user.username).to.equal('user_1')
482       expect(user.email).to.equal('updated@example.com')
483       expect(user.nsfwPolicy).to.equal('do_not_list')
484       expect(user.videoQuota).to.equal(2 * 1024 * 1024)
485       expect(user.id).to.be.a('number')
486       expect(user.account.displayName).to.equal('user_1')
487       expect(user.account.description).to.be.null
488     })
489
490     it('Should be able to update my avatar', async function () {
491       const fixture = 'avatar.png'
492
493       await updateMyAvatar({
494         url: server.url,
495         accessToken: accessTokenUser,
496         fixture
497       })
498
499       const res = await getMyUserInformation(server.url, accessTokenUser)
500       const user = res.body
501
502       await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
503     })
504
505     it('Should be able to update my display name', async function () {
506       await updateMyUser({
507         url: server.url,
508         accessToken: accessTokenUser,
509         displayName: 'new display name'
510       })
511
512       const res = await getMyUserInformation(server.url, accessTokenUser)
513       const user = res.body
514
515       expect(user.username).to.equal('user_1')
516       expect(user.email).to.equal('updated@example.com')
517       expect(user.nsfwPolicy).to.equal('do_not_list')
518       expect(user.videoQuota).to.equal(2 * 1024 * 1024)
519       expect(user.id).to.be.a('number')
520       expect(user.account.displayName).to.equal('new display name')
521       expect(user.account.description).to.be.null
522     })
523
524     it('Should be able to update my description', async function () {
525       await updateMyUser({
526         url: server.url,
527         accessToken: accessTokenUser,
528         description: 'my super description updated'
529       })
530
531       const res = await getMyUserInformation(server.url, accessTokenUser)
532       const user = res.body
533
534       expect(user.username).to.equal('user_1')
535       expect(user.email).to.equal('updated@example.com')
536       expect(user.nsfwPolicy).to.equal('do_not_list')
537       expect(user.videoQuota).to.equal(2 * 1024 * 1024)
538       expect(user.id).to.be.a('number')
539       expect(user.account.displayName).to.equal('new display name')
540       expect(user.account.description).to.equal('my super description updated')
541     })
542   })
543
544   describe('Updating another user', function () {
545
546     it('Should be able to update another user', async function () {
547       await updateUser({
548         url: server.url,
549         userId,
550         accessToken,
551         email: 'updated2@example.com',
552         emailVerified: true,
553         videoQuota: 42,
554         role: UserRole.MODERATOR,
555         adminFlags: UserAdminFlag.NONE
556       })
557
558       const res = await getUserInformation(server.url, accessToken, userId)
559       const user = res.body
560
561       expect(user.username).to.equal('user_1')
562       expect(user.email).to.equal('updated2@example.com')
563       expect(user.emailVerified).to.be.true
564       expect(user.nsfwPolicy).to.equal('do_not_list')
565       expect(user.videoQuota).to.equal(42)
566       expect(user.roleLabel).to.equal('Moderator')
567       expect(user.id).to.be.a('number')
568       expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
569     })
570
571     it('Should have removed the user token', async function () {
572       await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
573
574       accessTokenUser = await userLogin(server, user)
575     })
576
577     it('Should be able to update another user password', async function () {
578       await updateUser({
579         url: server.url,
580         userId,
581         accessToken,
582         password: 'password updated'
583       })
584
585       await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
586
587       await userLogin(server, user, 400)
588
589       user.password = 'password updated'
590       accessTokenUser = await userLogin(server, user)
591     })
592   })
593
594   describe('Video blacklists', function () {
595     it('Should be able to list video blacklist by a moderator', async function () {
596       await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
597     })
598   })
599
600   describe('Remove a user', function () {
601     it('Should be able to remove this user', async function () {
602       await removeUser(server.url, userId, accessToken)
603     })
604
605     it('Should not be able to login with this user', async function () {
606       await userLogin(server, user, 400)
607     })
608
609     it('Should not have videos of this user', async function () {
610       const res = await getVideosList(server.url)
611
612       expect(res.body.total).to.equal(1)
613
614       const video = res.body.data[ 0 ]
615       expect(video.account.name).to.equal('root')
616     })
617   })
618
619   describe('Registering a new user', function () {
620     it('Should register a new user', async function () {
621       const user = { username: 'user_15', password: 'my super password' }
622       const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
623
624       await registerUserWithChannel({ url: server.url, user, channel })
625     })
626
627     it('Should be able to login with this registered user', async function () {
628       const user15 = {
629         username: 'user_15',
630         password: 'my super password'
631       }
632
633       accessToken = await userLogin(server, user15)
634     })
635
636     it('Should have the correct video quota', async function () {
637       const res = await getMyUserInformation(server.url, accessToken)
638       const user = res.body
639
640       expect(user.videoQuota).to.equal(5 * 1024 * 1024)
641     })
642
643     it('Should have created the channel', async function () {
644       const res = await getVideoChannel(server.url, 'my_user_15_channel')
645
646       expect(res.body.displayName).to.equal('my channel rocks')
647     })
648
649     it('Should remove me', async function () {
650       {
651         const res = await getUsersList(server.url, server.accessToken)
652         expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
653       }
654
655       await deleteMe(server.url, accessToken)
656
657       {
658         const res = await getUsersList(server.url, server.accessToken)
659         expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
660       }
661     })
662   })
663
664   describe('User blocking', function () {
665     it('Should block and unblock a user', async function () {
666       const user16 = {
667         username: 'user_16',
668         password: 'my super password'
669       }
670       const resUser = await createUser({
671         url: server.url,
672         accessToken: server.accessToken,
673         username: user16.username,
674         password: user16.password
675       })
676       const user16Id = resUser.body.user.id
677
678       accessToken = await userLogin(server, user16)
679
680       await getMyUserInformation(server.url, accessToken, 200)
681       await blockUser(server.url, user16Id, server.accessToken)
682
683       await getMyUserInformation(server.url, accessToken, 401)
684       await userLogin(server, user16, 400)
685
686       await unblockUser(server.url, user16Id, server.accessToken)
687       accessToken = await userLogin(server, user16)
688       await getMyUserInformation(server.url, accessToken, 200)
689     })
690   })
691
692   after(async function () {
693     await cleanupTests([ server ])
694   })
695 })