]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users.ts
Add ability to delete our account
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
CommitLineData
0e1dc3e7 1/* tslint:disable:no-unused-expression */
86d13ec2 2
0e1dc3e7 3import * as chai from 'chai'
afffe988 4import 'mocha'
c5d31dba 5import { UserRole } from '../../../../shared/index'
0e1dc3e7 6import {
f8b8c36b
C
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating,
8 getUserInformation, getUsersList, getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo,
92b9d60c
C
9 registerUser, removeUser, removeVideo, runServer, ServerInfo, testImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo, userLogin,
10 deleteMe
c5d31dba
C
11} from '../../utils/index'
12import { follow } from '../../utils/server/follows'
c5d31dba 13import { setAccessTokensToServers } from '../../utils/users/login'
11ba2ab3 14import { getMyVideos } from '../../utils/videos/videos'
0e1dc3e7 15
afffe988
C
16const expect = chai.expect
17
0e1dc3e7
C
18describe('Test users', function () {
19 let server: ServerInfo
20 let accessToken: string
21 let accessTokenUser: string
22 let videoId: number
23 let userId: number
f8b8c36b
C
24 const user = {
25 username: 'user_1',
26 password: 'super password'
27 }
0e1dc3e7
C
28
29 before(async function () {
e212f887 30 this.timeout(30000)
0e1dc3e7
C
31
32 await flushTests()
33 server = await runServer(1)
86d13ec2
C
34
35 await setAccessTokensToServers([ server ])
0e1dc3e7
C
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
a7ba16b6 48 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
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
a7ba16b6 55 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
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
a7ba16b6 62 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
63 })
64
65 it('Should not login with an invalid password', async function () {
5f04dd2f 66 const user = { username: server.user.username, password: 'mew_three' }
0e1dc3e7
C
67 const res = await login(server.url, server.client, user, 400)
68
a7ba16b6 69 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
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
afffe988 79 it('Should not be able to follow', async function () {
0e1dc3e7 80 accessToken = 'my_super_token'
afffe988 81 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
0e1dc3e7
C
82 })
83
afffe988 84 it('Should not be able to unfollow')
0e1dc3e7
C
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 = {}
04e0fc48 94 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7 95 const res = await getVideosList(server.url)
afffe988 96 const video = res.body.data[ 0 ]
0e1dc3e7 97
b64c950a 98 expect(video.account.name).to.equal('root')
0e1dc3e7
C
99 videoId = video.id
100 })
101
102 it('Should upload the video again with the correct token', async function () {
103 const videoAttributes = {}
04e0fc48 104 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7
C
105 })
106
107 it('Should retrieve a video rating', async function () {
108 await rateVideo(server.url, accessToken, videoId, 'like')
26d21b78 109 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
0e1dc3e7
C
110 const rating = res.body
111
a7ba16b6
C
112 expect(rating.videoId).to.equal(videoId)
113 expect(rating.rating).to.equal('like')
0e1dc3e7
C
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 () {
f8b8c36b 159 await createUser(server.url, accessToken, user.username,user.password, 2 * 1024 * 1024)
0e1dc3e7
C
160 })
161
162 it('Should be able to login with this user', async function () {
f8b8c36b 163 accessTokenUser = await userLogin(server, user)
0e1dc3e7
C
164 })
165
166 it('Should be able to get the user information', async function () {
5c98d3bf 167 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
168 const user = res.body
169
a7ba16b6
C
170 expect(user.username).to.equal('user_1')
171 expect(user.email).to.equal('user_1@example.com')
0883b324 172 expect(user.nsfwPolicy).to.equal('display')
a7ba16b6
C
173 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
174 expect(user.roleLabel).to.equal('User')
175 expect(user.id).to.be.a('number')
ed56ad11 176 expect(user.account.displayName).to.equal('user_1')
2422c46b 177 expect(user.account.description).to.be.null
0e1dc3e7
C
178 })
179
180 it('Should be able to upload a video with this user', async function () {
181 this.timeout(5000)
182
11474c3c 183 const videoAttributes = {
ce5496d6
C
184 name: 'super user video',
185 fixture: 'video_short.webm'
11474c3c 186 }
0e1dc3e7
C
187 await uploadVideo(server.url, accessTokenUser, videoAttributes)
188 })
189
ce5496d6
C
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
11474c3c
C
197 it('Should be able to list my videos', async function () {
198 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
a7ba16b6 199 expect(res.body.total).to.equal(1)
11474c3c
C
200
201 const videos = res.body.data
a7ba16b6 202 expect(videos).to.have.lengthOf(1)
11474c3c 203
a7ba16b6 204 expect(videos[ 0 ].name).to.equal('super user video')
11474c3c
C
205 })
206
0e1dc3e7 207 it('Should list all the users', async function () {
86d13ec2 208 const res = await getUsersList(server.url, server.accessToken)
0e1dc3e7
C
209 const result = res.body
210 const total = result.total
211 const users = result.data
212
a7ba16b6
C
213 expect(total).to.equal(2)
214 expect(users).to.be.an('array')
215 expect(users.length).to.equal(2)
afffe988
C
216
217 const user = users[ 0 ]
a7ba16b6
C
218 expect(user.username).to.equal('user_1')
219 expect(user.email).to.equal('user_1@example.com')
0883b324 220 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7 221
afffe988 222 const rootUser = users[ 1 ]
a7ba16b6
C
223 expect(rootUser.username).to.equal('root')
224 expect(rootUser.email).to.equal('admin1@example.com')
0883b324 225 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
226
227 userId = user.id
228 })
229
230 it('Should list only the first user by username asc', async function () {
86d13ec2 231 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
0e1dc3e7
C
232
233 const result = res.body
234 const total = result.total
235 const users = result.data
236
a7ba16b6
C
237 expect(total).to.equal(2)
238 expect(users.length).to.equal(1)
afffe988
C
239
240 const user = users[ 0 ]
a7ba16b6
C
241 expect(user.username).to.equal('root')
242 expect(user.email).to.equal('admin1@example.com')
243 expect(user.roleLabel).to.equal('Administrator')
0883b324 244 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
245 })
246
247 it('Should list only the first user by username desc', async function () {
86d13ec2 248 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
0e1dc3e7
C
249 const result = res.body
250 const total = result.total
251 const users = result.data
252
a7ba16b6
C
253 expect(total).to.equal(2)
254 expect(users.length).to.equal(1)
afffe988
C
255
256 const user = users[ 0 ]
a7ba16b6
C
257 expect(user.username).to.equal('user_1')
258 expect(user.email).to.equal('user_1@example.com')
0883b324 259 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
260 })
261
262 it('Should list only the second user by createdAt desc', async function () {
86d13ec2 263 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
0e1dc3e7
C
264 const result = res.body
265 const total = result.total
266 const users = result.data
267
a7ba16b6
C
268 expect(total).to.equal(2)
269 expect(users.length).to.equal(1)
afffe988
C
270
271 const user = users[ 0 ]
a7ba16b6
C
272 expect(user.username).to.equal('user_1')
273 expect(user.email).to.equal('user_1@example.com')
0883b324 274 expect(user.nsfwPolicy).to.equal('display')
0e1dc3e7
C
275 })
276
277 it('Should list all the users by createdAt asc', async function () {
86d13ec2 278 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
0e1dc3e7
C
279 const result = res.body
280 const total = result.total
281 const users = result.data
282
a7ba16b6
C
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')
0883b324 288 expect(users[ 0 ].nsfwPolicy).to.equal('display')
afffe988 289
a7ba16b6
C
290 expect(users[ 1 ].username).to.equal('user_1')
291 expect(users[ 1 ].email).to.equal('user_1@example.com')
0883b324 292 expect(users[ 1 ].nsfwPolicy).to.equal('display')
0e1dc3e7
C
293 })
294
5c98d3bf 295 it('Should update my password', async function () {
11ba2ab3
C
296 await updateMyUser({
297 url: server.url,
298 accessToken: accessTokenUser,
299 newPassword: 'new password'
300 })
f8b8c36b 301 user.password = 'new password'
0e1dc3e7 302
f8b8c36b 303 await userLogin(server, user, 200)
0e1dc3e7
C
304 })
305
306 it('Should be able to change the NSFW display attribute', async function () {
11ba2ab3
C
307 await updateMyUser({
308 url: server.url,
309 accessToken: accessTokenUser,
0883b324 310 nsfwPolicy: 'do_not_list'
11ba2ab3 311 })
0e1dc3e7 312
5c98d3bf 313 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
314 const user = res.body
315
a7ba16b6
C
316 expect(user.username).to.equal('user_1')
317 expect(user.email).to.equal('user_1@example.com')
0883b324 318 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
319 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
320 expect(user.id).to.be.a('number')
ed56ad11 321 expect(user.account.displayName).to.equal('user_1')
2422c46b 322 expect(user.account.description).to.be.null
5c98d3bf
C
323 })
324
7efe153b 325 it('Should be able to change the autoPlayVideo attribute', async function () {
11ba2ab3
C
326 await updateMyUser({
327 url: server.url,
328 accessToken: accessTokenUser,
329 autoPlayVideo: false
330 })
7efe153b
AL
331
332 const res = await getMyUserInformation(server.url, accessTokenUser)
333 const user = res.body
334
335 expect(user.autoPlayVideo).to.be.false
336 })
337
5c98d3bf 338 it('Should be able to change the email display attribute', async function () {
11ba2ab3
C
339 await updateMyUser({
340 url: server.url,
341 accessToken: accessTokenUser,
342 email: 'updated@example.com'
343 })
5c98d3bf
C
344
345 const res = await getMyUserInformation(server.url, accessTokenUser)
346 const user = res.body
347
a7ba16b6
C
348 expect(user.username).to.equal('user_1')
349 expect(user.email).to.equal('updated@example.com')
0883b324 350 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
351 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
352 expect(user.id).to.be.a('number')
ed56ad11 353 expect(user.account.displayName).to.equal('user_1')
2422c46b 354 expect(user.account.description).to.be.null
5c98d3bf
C
355 })
356
c5911fd3
C
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
7b0956ec 369 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
c5911fd3
C
370 })
371
ed56ad11
C
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
2422c46b
C
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')
0883b324 403 expect(user.nsfwPolicy).to.equal('do_not_list')
2422c46b
C
404 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
405 expect(user.id).to.be.a('number')
ed56ad11 406 expect(user.account.displayName).to.equal('new display name')
2422c46b
C
407 expect(user.account.description).to.equal('my super description updated')
408 })
409
5c98d3bf 410 it('Should be able to update another user', async function () {
11ba2ab3
C
411 await updateUser({
412 url: server.url,
413 userId,
414 accessToken,
415 email: 'updated2@example.com',
416 videoQuota: 42,
417 role: UserRole.MODERATOR
418 })
5c98d3bf 419
77a5501f 420 const res = await getUserInformation(server.url, accessToken, userId)
5c98d3bf
C
421 const user = res.body
422
a7ba16b6
C
423 expect(user.username).to.equal('user_1')
424 expect(user.email).to.equal('updated2@example.com')
0883b324 425 expect(user.nsfwPolicy).to.equal('do_not_list')
a7ba16b6
C
426 expect(user.videoQuota).to.equal(42)
427 expect(user.roleLabel).to.equal('Moderator')
428 expect(user.id).to.be.a('number')
0e1dc3e7
C
429 })
430
f8b8c36b
C
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
757f0da3
C
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
0e1dc3e7
C
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 () {
f8b8c36b 450 await userLogin(server, user, 400)
0e1dc3e7
C
451 })
452
453 it('Should not have videos of this user', async function () {
454 const res = await getVideosList(server.url)
455
a7ba16b6 456 expect(res.body.total).to.equal(1)
0e1dc3e7 457
afffe988 458 const video = res.body.data[ 0 ]
b64c950a 459 expect(video.account.name).to.equal('root')
0e1dc3e7
C
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 () {
f8b8c36b 467 const user15 = {
0e1dc3e7
C
468 username: 'user_15',
469 password: 'my super password'
470 }
471
f8b8c36b 472 accessToken = await userLogin(server, user15)
5c98d3bf
C
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
a7ba16b6 479 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
0e1dc3e7
C
480 })
481
92b9d60c
C
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
0e1dc3e7
C
496 after(async function () {
497 killallServers([ server ])
498
499 // Keep the logs if the test failed
afffe988 500 if (this[ 'ok' ]) {
0e1dc3e7
C
501 await flushTests()
502 }
503 })
504})