]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users.ts
Client bulk delete
[github/Chocobozzz/PeerTube.git] / server / tests / api / users.ts
CommitLineData
0e1dc3e7 1/* tslint:disable:no-unused-expression */
86d13ec2 2
0e1dc3e7 3import * as chai from 'chai'
afffe988
C
4import 'mocha'
5import { UserRole } from '../../../shared'
0e1dc3e7 6import {
0e1dc3e7 7 createUser,
afffe988
C
8 flushTests,
9 getBlacklistedVideosList,
5c98d3bf 10 getMyUserInformation,
afffe988 11 getUserInformation,
0e1dc3e7
C
12 getUsersList,
13 getUsersListPaginationAndSort,
afffe988
C
14 getUserVideoRating,
15 getVideosList,
16 killallServers,
17 login,
18 loginAndGetAccessToken,
19 makePutBodyRequest,
20 rateVideo,
0e1dc3e7 21 registerUser,
757f0da3 22 removeUser,
afffe988
C
23 removeVideo,
24 runServer,
25 ServerInfo,
26 updateMyUser,
27 updateUser,
28 uploadVideo
0e1dc3e7 29} from '../utils'
afffe988 30import { follow } from '../utils/follows'
11474c3c 31import { getMyVideos } from '../utils/videos'
86d13ec2 32import { setAccessTokensToServers } from '../utils/login'
0e1dc3e7 33
afffe988
C
34const expect = chai.expect
35
0e1dc3e7
C
36describe('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 () {
572f8d3d 44 this.timeout(10000)
0e1dc3e7
C
45
46 await flushTests()
47 server = await runServer(1)
86d13ec2
C
48
49 await setAccessTokensToServers([ server ])
0e1dc3e7
C
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
afffe988
C
62 expect(res.body.error)
63 .to
64 .equal('invalid_client')
0e1dc3e7
C
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
afffe988
C
71 expect(res.body.error)
72 .to
73 .equal('invalid_client')
0e1dc3e7
C
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
afffe988
C
80 expect(res.body.error)
81 .to
82 .equal('invalid_grant')
0e1dc3e7
C
83 })
84
85 it('Should not login with an invalid password', async function () {
5f04dd2f 86 const user = { username: server.user.username, password: 'mew_three' }
0e1dc3e7
C
87 const res = await login(server.url, server.client, user, 400)
88
afffe988
C
89 expect(res.body.error)
90 .to
91 .equal('invalid_grant')
0e1dc3e7
C
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
afffe988 101 it('Should not be able to follow', async function () {
0e1dc3e7 102 accessToken = 'my_super_token'
afffe988 103 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
0e1dc3e7
C
104 })
105
afffe988 106 it('Should not be able to unfollow')
0e1dc3e7
C
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, 204)
117 const res = await getVideosList(server.url)
afffe988 118 const video = res.body.data[ 0 ]
0e1dc3e7 119
afffe988
C
120 expect(video.account)
121 .to
122 .equal('root')
0e1dc3e7
C
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, 204)
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
afffe988
C
136 expect(rating.videoId)
137 .to
138 .equal(videoId)
139 expect(rating.rating)
140 .to
141 .equal('like')
0e1dc3e7
C
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 () {
5c98d3bf 187 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
0e1dc3e7
C
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 () {
5c98d3bf 200 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
201 const user = res.body
202
afffe988
C
203 expect(user.username)
204 .to
205 .equal('user_1')
206 expect(user.email)
207 .to
208 .equal('user_1@example.com')
0e1dc3e7 209 expect(user.displayNSFW).to.be.false
afffe988
C
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')
0e1dc3e7
C
220 })
221
222 it('Should be able to upload a video with this user', async function () {
223 this.timeout(5000)
224
11474c3c
C
225 const videoAttributes = {
226 name: 'super user video'
227 }
0e1dc3e7
C
228 await uploadVideo(server.url, accessTokenUser, videoAttributes)
229 })
230
11474c3c
C
231 it('Should be able to list my videos', async function () {
232 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
afffe988
C
233 expect(res.body.total)
234 .to
235 .equal(1)
11474c3c
C
236
237 const videos = res.body.data
afffe988
C
238 expect(videos)
239 .to
240 .have
241 .lengthOf(1)
11474c3c 242
afffe988
C
243 expect(videos[ 0 ].name)
244 .to
245 .equal('super user video')
11474c3c
C
246 })
247
0e1dc3e7 248 it('Should list all the users', async function () {
86d13ec2 249 const res = await getUsersList(server.url, server.accessToken)
0e1dc3e7
C
250 const result = res.body
251 const total = result.total
252 const users = result.data
253
afffe988
C
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')
0e1dc3e7
C
272 expect(user.displayNSFW).to.be.false
273
afffe988
C
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')
0e1dc3e7
C
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 () {
86d13ec2 287 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
0e1dc3e7
C
288
289 const result = res.body
290 const total = result.total
291 const users = result.data
292
afffe988
C
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')
0e1dc3e7
C
310 expect(user.displayNSFW).to.be.false
311 })
312
313 it('Should list only the first user by username desc', async function () {
86d13ec2 314 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
0e1dc3e7
C
315 const result = res.body
316 const total = result.total
317 const users = result.data
318
afffe988
C
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')
0e1dc3e7
C
333 expect(user.displayNSFW).to.be.false
334 })
335
336 it('Should list only the second user by createdAt desc', async function () {
86d13ec2 337 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
0e1dc3e7
C
338 const result = res.body
339 const total = result.total
340 const users = result.data
341
afffe988
C
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')
0e1dc3e7
C
356 expect(user.displayNSFW).to.be.false
357 })
358
359 it('Should list all the users by createdAt asc', async function () {
86d13ec2 360 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
0e1dc3e7
C
361 const result = res.body
362 const total = result.total
363 const users = result.data
364
afffe988
C
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
0e1dc3e7
C
387 })
388
5c98d3bf
C
389 it('Should update my password', async function () {
390 await updateMyUser(server.url, accessTokenUser, 'new password')
0e1dc3e7
C
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 () {
5c98d3bf 397 await updateMyUser(server.url, accessTokenUser, undefined, true)
0e1dc3e7 398
5c98d3bf 399 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
400 const user = res.body
401
afffe988
C
402 expect(user.username)
403 .to
404 .equal('user_1')
405 expect(user.email)
406 .to
407 .equal('user_1@example.com')
0e1dc3e7 408 expect(user.displayNSFW).to.be.ok
afffe988
C
409 expect(user.videoQuota)
410 .to
411 .equal(2 * 1024 * 1024)
412 expect(user.id)
413 .to
414 .be
415 .a('number')
5c98d3bf
C
416 })
417
418 it('Should be able to change the email display attribute', async function () {
419 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
420
421 const res = await getMyUserInformation(server.url, accessTokenUser)
422 const user = res.body
423
afffe988
C
424 expect(user.username)
425 .to
426 .equal('user_1')
427 expect(user.email)
428 .to
429 .equal('updated@example.com')
5c98d3bf 430 expect(user.displayNSFW).to.be.ok
afffe988
C
431 expect(user.videoQuota)
432 .to
433 .equal(2 * 1024 * 1024)
434 expect(user.id)
435 .to
436 .be
437 .a('number')
5c98d3bf
C
438 })
439
440 it('Should be able to update another user', async function () {
757f0da3 441 await updateUser(server.url, userId, accessToken, 'updated2@example.com', 42, UserRole.MODERATOR)
5c98d3bf 442
77a5501f 443 const res = await getUserInformation(server.url, accessToken, userId)
5c98d3bf
C
444 const user = res.body
445
afffe988
C
446 expect(user.username)
447 .to
448 .equal('user_1')
449 expect(user.email)
450 .to
451 .equal('updated2@example.com')
5c98d3bf 452 expect(user.displayNSFW).to.be.ok
afffe988
C
453 expect(user.videoQuota)
454 .to
455 .equal(42)
456 expect(user.roleLabel)
457 .to
458 .equal('Moderator')
459 expect(user.id)
460 .to
461 .be
462 .a('number')
0e1dc3e7
C
463 })
464
757f0da3
C
465 it('Should not be able to delete a user by a moderator', async function () {
466 await removeUser(server.url, 2, accessTokenUser, 403)
467 })
468
469 it('Should be able to list video blacklist by a moderator', async function () {
470 await getBlacklistedVideosList(server.url, accessTokenUser)
471 })
472
0e1dc3e7
C
473 it('Should be able to remove this user', async function () {
474 await removeUser(server.url, userId, accessToken)
475 })
476
477 it('Should not be able to login with this user', async function () {
478 // server.user is already set to user 1
479 await login(server.url, server.client, server.user, 400)
480 })
481
482 it('Should not have videos of this user', async function () {
483 const res = await getVideosList(server.url)
484
afffe988
C
485 expect(res.body.total)
486 .to
487 .equal(1)
0e1dc3e7 488
afffe988
C
489 const video = res.body.data[ 0 ]
490 expect(video.account)
491 .to
492 .equal('root')
0e1dc3e7
C
493 })
494
495 it('Should register a new user', async function () {
496 await registerUser(server.url, 'user_15', 'my super password')
497 })
498
499 it('Should be able to login with this registered user', async function () {
500 server.user = {
501 username: 'user_15',
502 password: 'my super password'
503 }
504
5c98d3bf
C
505 accessToken = await loginAndGetAccessToken(server)
506 })
507
508 it('Should have the correct video quota', async function () {
509 const res = await getMyUserInformation(server.url, accessToken)
510 const user = res.body
511
afffe988
C
512 expect(user.videoQuota)
513 .to
514 .equal(5 * 1024 * 1024)
0e1dc3e7
C
515 })
516
517 after(async function () {
518 killallServers([ server ])
519
520 // Keep the logs if the test failed
afffe988 521 if (this[ 'ok' ]) {
0e1dc3e7
C
522 await flushTests()
523 }
524 })
525})