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