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