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