Add ability to delete our account
[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 { 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
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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.equal('Authentication failed.')
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
197   it('Should be able to list my videos', async function () {
198     const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
199     expect(res.body.total).to.equal(1)
200
201     const videos = res.body.data
202     expect(videos).to.have.lengthOf(1)
203
204     expect(videos[ 0 ].name).to.equal('super user video')
205   })
206
207   it('Should list all the users', async function () {
208     const res = await getUsersList(server.url, server.accessToken)
209     const result = res.body
210     const total = result.total
211     const users = result.data
212
213     expect(total).to.equal(2)
214     expect(users).to.be.an('array')
215     expect(users.length).to.equal(2)
216
217     const user = users[ 0 ]
218     expect(user.username).to.equal('user_1')
219     expect(user.email).to.equal('user_1@example.com')
220     expect(user.nsfwPolicy).to.equal('display')
221
222     const rootUser = users[ 1 ]
223     expect(rootUser.username).to.equal('root')
224     expect(rootUser.email).to.equal('admin1@example.com')
225     expect(user.nsfwPolicy).to.equal('display')
226
227     userId = user.id
228   })
229
230   it('Should list only the first user by username asc', async function () {
231     const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
232
233     const result = res.body
234     const total = result.total
235     const users = result.data
236
237     expect(total).to.equal(2)
238     expect(users.length).to.equal(1)
239
240     const user = users[ 0 ]
241     expect(user.username).to.equal('root')
242     expect(user.email).to.equal('admin1@example.com')
243     expect(user.roleLabel).to.equal('Administrator')
244     expect(user.nsfwPolicy).to.equal('display')
245   })
246
247   it('Should list only the first user by username desc', async function () {
248     const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
249     const result = res.body
250     const total = result.total
251     const users = result.data
252
253     expect(total).to.equal(2)
254     expect(users.length).to.equal(1)
255
256     const user = users[ 0 ]
257     expect(user.username).to.equal('user_1')
258     expect(user.email).to.equal('user_1@example.com')
259     expect(user.nsfwPolicy).to.equal('display')
260   })
261
262   it('Should list only the second user by createdAt desc', async function () {
263     const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
264     const result = res.body
265     const total = result.total
266     const users = result.data
267
268     expect(total).to.equal(2)
269     expect(users.length).to.equal(1)
270
271     const user = users[ 0 ]
272     expect(user.username).to.equal('user_1')
273     expect(user.email).to.equal('user_1@example.com')
274     expect(user.nsfwPolicy).to.equal('display')
275   })
276
277   it('Should list all the users by createdAt asc', async function () {
278     const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
279     const result = res.body
280     const total = result.total
281     const users = result.data
282
283     expect(total).to.equal(2)
284     expect(users.length).to.equal(2)
285
286     expect(users[ 0 ].username).to.equal('root')
287     expect(users[ 0 ].email).to.equal('admin1@example.com')
288     expect(users[ 0 ].nsfwPolicy).to.equal('display')
289
290     expect(users[ 1 ].username).to.equal('user_1')
291     expect(users[ 1 ].email).to.equal('user_1@example.com')
292     expect(users[ 1 ].nsfwPolicy).to.equal('display')
293   })
294
295   it('Should update my password', async function () {
296     await updateMyUser({
297       url: server.url,
298       accessToken: accessTokenUser,
299       newPassword: 'new password'
300     })
301     user.password = 'new password'
302
303     await userLogin(server, user, 200)
304   })
305
306   it('Should be able to change the NSFW display attribute', async function () {
307     await updateMyUser({
308       url: server.url,
309       accessToken: accessTokenUser,
310       nsfwPolicy: 'do_not_list'
311     })
312
313     const res = await getMyUserInformation(server.url, accessTokenUser)
314     const user = res.body
315
316     expect(user.username).to.equal('user_1')
317     expect(user.email).to.equal('user_1@example.com')
318     expect(user.nsfwPolicy).to.equal('do_not_list')
319     expect(user.videoQuota).to.equal(2 * 1024 * 1024)
320     expect(user.id).to.be.a('number')
321     expect(user.account.displayName).to.equal('user_1')
322     expect(user.account.description).to.be.null
323   })
324
325   it('Should be able to change the autoPlayVideo attribute', async function () {
326     await updateMyUser({
327       url: server.url,
328       accessToken: accessTokenUser,
329       autoPlayVideo: false
330     })
331
332     const res = await getMyUserInformation(server.url, accessTokenUser)
333     const user = res.body
334
335     expect(user.autoPlayVideo).to.be.false
336   })
337
338   it('Should be able to change the email display attribute', async function () {
339     await updateMyUser({
340       url: server.url,
341       accessToken: accessTokenUser,
342       email: 'updated@example.com'
343     })
344
345     const res = await getMyUserInformation(server.url, accessTokenUser)
346     const user = res.body
347
348     expect(user.username).to.equal('user_1')
349     expect(user.email).to.equal('updated@example.com')
350     expect(user.nsfwPolicy).to.equal('do_not_list')
351     expect(user.videoQuota).to.equal(2 * 1024 * 1024)
352     expect(user.id).to.be.a('number')
353     expect(user.account.displayName).to.equal('user_1')
354     expect(user.account.description).to.be.null
355   })
356
357   it('Should be able to update my avatar', async function () {
358     const fixture = 'avatar.png'
359
360     await updateMyAvatar({
361       url: server.url,
362       accessToken: accessTokenUser,
363       fixture
364     })
365
366     const res = await getMyUserInformation(server.url, accessTokenUser)
367     const user = res.body
368
369     await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
370   })
371
372   it('Should be able to update my display name', async function () {
373     await updateMyUser({
374       url: server.url,
375       accessToken: accessTokenUser,
376       displayName: 'new display name'
377     })
378
379     const res = await getMyUserInformation(server.url, accessTokenUser)
380     const user = res.body
381
382     expect(user.username).to.equal('user_1')
383     expect(user.email).to.equal('updated@example.com')
384     expect(user.nsfwPolicy).to.equal('do_not_list')
385     expect(user.videoQuota).to.equal(2 * 1024 * 1024)
386     expect(user.id).to.be.a('number')
387     expect(user.account.displayName).to.equal('new display name')
388     expect(user.account.description).to.be.null
389   })
390
391   it('Should be able to update my description', async function () {
392     await updateMyUser({
393       url: server.url,
394       accessToken: accessTokenUser,
395       description: 'my super description updated'
396     })
397
398     const res = await getMyUserInformation(server.url, accessTokenUser)
399     const user = res.body
400
401     expect(user.username).to.equal('user_1')
402     expect(user.email).to.equal('updated@example.com')
403     expect(user.nsfwPolicy).to.equal('do_not_list')
404     expect(user.videoQuota).to.equal(2 * 1024 * 1024)
405     expect(user.id).to.be.a('number')
406     expect(user.account.displayName).to.equal('new display name')
407     expect(user.account.description).to.equal('my super description updated')
408   })
409
410   it('Should be able to update another user', async function () {
411     await updateUser({
412       url: server.url,
413       userId,
414       accessToken,
415       email: 'updated2@example.com',
416       videoQuota: 42,
417       role: UserRole.MODERATOR
418     })
419
420     const res = await getUserInformation(server.url, accessToken, userId)
421     const user = res.body
422
423     expect(user.username).to.equal('user_1')
424     expect(user.email).to.equal('updated2@example.com')
425     expect(user.nsfwPolicy).to.equal('do_not_list')
426     expect(user.videoQuota).to.equal(42)
427     expect(user.roleLabel).to.equal('Moderator')
428     expect(user.id).to.be.a('number')
429   })
430
431   it('Should have removed the user token', async function () {
432     await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
433
434     accessTokenUser = await userLogin(server, user)
435   })
436
437   it('Should not be able to delete a user by a moderator', async function () {
438     await removeUser(server.url, 2, accessTokenUser, 403)
439   })
440
441   it('Should be able to list video blacklist by a moderator', async function () {
442     await getBlacklistedVideosList(server.url, accessTokenUser)
443   })
444
445   it('Should be able to remove this user', async function () {
446     await removeUser(server.url, userId, accessToken)
447   })
448
449   it('Should not be able to login with this user', async function () {
450     await userLogin(server, user, 400)
451   })
452
453   it('Should not have videos of this user', async function () {
454     const res = await getVideosList(server.url)
455
456     expect(res.body.total).to.equal(1)
457
458     const video = res.body.data[ 0 ]
459     expect(video.account.name).to.equal('root')
460   })
461
462   it('Should register a new user', async function () {
463     await registerUser(server.url, 'user_15', 'my super password')
464   })
465
466   it('Should be able to login with this registered user', async function () {
467     const user15 = {
468       username: 'user_15',
469       password: 'my super password'
470     }
471
472     accessToken = await userLogin(server, user15)
473   })
474
475   it('Should have the correct video quota', async function () {
476     const res = await getMyUserInformation(server.url, accessToken)
477     const user = res.body
478
479     expect(user.videoQuota).to.equal(5 * 1024 * 1024)
480   })
481
482   it('Should remove me', async function () {
483     {
484       const res = await getUsersList(server.url, server.accessToken)
485       expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
486     }
487
488     await deleteMe(server.url, accessToken)
489
490     {
491       const res = await getUsersList(server.url, server.accessToken)
492       expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
493     }
494   })
495
496   after(async function () {
497     killallServers([ server ])
498
499     // Keep the logs if the test failed
500     if (this[ 'ok' ]) {
501       await flushTests()
502     }
503   })
504 })