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