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