]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users.ts
Change transcoding default conf options
[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
a7ba16b6 97 expect(video.accountName).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')
0e1dc3e7 171 expect(user.displayNSFW).to.be.false
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')
2422c46b 175 expect(user.account.description).to.be.null
0e1dc3e7
C
176 })
177
178 it('Should be able to upload a video with this user', async function () {
179 this.timeout(5000)
180
11474c3c 181 const videoAttributes = {
ce5496d6
C
182 name: 'super user video',
183 fixture: 'video_short.webm'
11474c3c 184 }
0e1dc3e7
C
185 await uploadVideo(server.url, accessTokenUser, videoAttributes)
186 })
187
ce5496d6
C
188 it('Should have video quota updated', async function () {
189 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
190 const data = res.body
191
192 expect(data.videoQuotaUsed).to.equal(218910)
193 })
194
11474c3c
C
195 it('Should be able to list my videos', async function () {
196 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
a7ba16b6 197 expect(res.body.total).to.equal(1)
11474c3c
C
198
199 const videos = res.body.data
a7ba16b6 200 expect(videos).to.have.lengthOf(1)
11474c3c 201
a7ba16b6 202 expect(videos[ 0 ].name).to.equal('super user video')
11474c3c
C
203 })
204
0e1dc3e7 205 it('Should list all the users', async function () {
86d13ec2 206 const res = await getUsersList(server.url, server.accessToken)
0e1dc3e7
C
207 const result = res.body
208 const total = result.total
209 const users = result.data
210
a7ba16b6
C
211 expect(total).to.equal(2)
212 expect(users).to.be.an('array')
213 expect(users.length).to.equal(2)
afffe988
C
214
215 const user = users[ 0 ]
a7ba16b6
C
216 expect(user.username).to.equal('user_1')
217 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
218 expect(user.displayNSFW).to.be.false
219
afffe988 220 const rootUser = users[ 1 ]
a7ba16b6
C
221 expect(rootUser.username).to.equal('root')
222 expect(rootUser.email).to.equal('admin1@example.com')
0e1dc3e7
C
223 expect(rootUser.displayNSFW).to.be.false
224
225 userId = user.id
226 })
227
228 it('Should list only the first user by username asc', async function () {
86d13ec2 229 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
0e1dc3e7
C
230
231 const result = res.body
232 const total = result.total
233 const users = result.data
234
a7ba16b6
C
235 expect(total).to.equal(2)
236 expect(users.length).to.equal(1)
afffe988
C
237
238 const user = users[ 0 ]
a7ba16b6
C
239 expect(user.username).to.equal('root')
240 expect(user.email).to.equal('admin1@example.com')
241 expect(user.roleLabel).to.equal('Administrator')
0e1dc3e7
C
242 expect(user.displayNSFW).to.be.false
243 })
244
245 it('Should list only the first user by username desc', async function () {
86d13ec2 246 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
0e1dc3e7
C
247 const result = res.body
248 const total = result.total
249 const users = result.data
250
a7ba16b6
C
251 expect(total).to.equal(2)
252 expect(users.length).to.equal(1)
afffe988
C
253
254 const user = users[ 0 ]
a7ba16b6
C
255 expect(user.username).to.equal('user_1')
256 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
257 expect(user.displayNSFW).to.be.false
258 })
259
260 it('Should list only the second user by createdAt desc', async function () {
86d13ec2 261 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
0e1dc3e7
C
262 const result = res.body
263 const total = result.total
264 const users = result.data
265
a7ba16b6
C
266 expect(total).to.equal(2)
267 expect(users.length).to.equal(1)
afffe988
C
268
269 const user = users[ 0 ]
a7ba16b6
C
270 expect(user.username).to.equal('user_1')
271 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
272 expect(user.displayNSFW).to.be.false
273 })
274
275 it('Should list all the users by createdAt asc', async function () {
86d13ec2 276 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
0e1dc3e7
C
277 const result = res.body
278 const total = result.total
279 const users = result.data
280
a7ba16b6
C
281 expect(total).to.equal(2)
282 expect(users.length).to.equal(2)
283
284 expect(users[ 0 ].username).to.equal('root')
285 expect(users[ 0 ].email).to.equal('admin1@example.com')
afffe988
C
286 expect(users[ 0 ].displayNSFW).to.be.false
287
a7ba16b6
C
288 expect(users[ 1 ].username).to.equal('user_1')
289 expect(users[ 1 ].email).to.equal('user_1@example.com')
afffe988 290 expect(users[ 1 ].displayNSFW).to.be.false
0e1dc3e7
C
291 })
292
5c98d3bf 293 it('Should update my password', async function () {
11ba2ab3
C
294 await updateMyUser({
295 url: server.url,
296 accessToken: accessTokenUser,
297 newPassword: 'new password'
298 })
f8b8c36b 299 user.password = 'new password'
0e1dc3e7 300
f8b8c36b 301 await userLogin(server, user, 200)
0e1dc3e7
C
302 })
303
304 it('Should be able to change the NSFW display attribute', async function () {
11ba2ab3
C
305 await updateMyUser({
306 url: server.url,
307 accessToken: accessTokenUser,
308 displayNSFW: true
309 })
0e1dc3e7 310
5c98d3bf 311 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
312 const user = res.body
313
a7ba16b6
C
314 expect(user.username).to.equal('user_1')
315 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7 316 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
317 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
318 expect(user.id).to.be.a('number')
2422c46b 319 expect(user.account.description).to.be.null
5c98d3bf
C
320 })
321
7efe153b 322 it('Should be able to change the autoPlayVideo attribute', async function () {
11ba2ab3
C
323 await updateMyUser({
324 url: server.url,
325 accessToken: accessTokenUser,
326 autoPlayVideo: false
327 })
7efe153b
AL
328
329 const res = await getMyUserInformation(server.url, accessTokenUser)
330 const user = res.body
331
332 expect(user.autoPlayVideo).to.be.false
333 })
334
5c98d3bf 335 it('Should be able to change the email display attribute', async function () {
11ba2ab3
C
336 await updateMyUser({
337 url: server.url,
338 accessToken: accessTokenUser,
339 email: 'updated@example.com'
340 })
5c98d3bf
C
341
342 const res = await getMyUserInformation(server.url, accessTokenUser)
343 const user = res.body
344
a7ba16b6
C
345 expect(user.username).to.equal('user_1')
346 expect(user.email).to.equal('updated@example.com')
5c98d3bf 347 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
348 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
349 expect(user.id).to.be.a('number')
2422c46b 350 expect(user.account.description).to.be.null
5c98d3bf
C
351 })
352
c5911fd3
C
353 it('Should be able to update my avatar', async function () {
354 const fixture = 'avatar.png'
355
356 await updateMyAvatar({
357 url: server.url,
358 accessToken: accessTokenUser,
359 fixture
360 })
361
362 const res = await getMyUserInformation(server.url, accessTokenUser)
363 const user = res.body
364
7b0956ec 365 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
c5911fd3
C
366 })
367
2422c46b
C
368 it('Should be able to update my description', async function () {
369 await updateMyUser({
370 url: server.url,
371 accessToken: accessTokenUser,
372 description: 'my super description updated'
373 })
374
375 const res = await getMyUserInformation(server.url, accessTokenUser)
376 const user = res.body
377
378 expect(user.username).to.equal('user_1')
379 expect(user.email).to.equal('updated@example.com')
380 expect(user.displayNSFW).to.be.ok
381 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
382 expect(user.id).to.be.a('number')
383 expect(user.account.description).to.equal('my super description updated')
384 })
385
5c98d3bf 386 it('Should be able to update another user', async function () {
11ba2ab3
C
387 await updateUser({
388 url: server.url,
389 userId,
390 accessToken,
391 email: 'updated2@example.com',
392 videoQuota: 42,
393 role: UserRole.MODERATOR
394 })
5c98d3bf 395
77a5501f 396 const res = await getUserInformation(server.url, accessToken, userId)
5c98d3bf
C
397 const user = res.body
398
a7ba16b6
C
399 expect(user.username).to.equal('user_1')
400 expect(user.email).to.equal('updated2@example.com')
5c98d3bf 401 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
402 expect(user.videoQuota).to.equal(42)
403 expect(user.roleLabel).to.equal('Moderator')
404 expect(user.id).to.be.a('number')
0e1dc3e7
C
405 })
406
f8b8c36b
C
407 it('Should have removed the user token', async function () {
408 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
409
410 accessTokenUser = await userLogin(server, user)
411 })
412
757f0da3
C
413 it('Should not be able to delete a user by a moderator', async function () {
414 await removeUser(server.url, 2, accessTokenUser, 403)
415 })
416
417 it('Should be able to list video blacklist by a moderator', async function () {
418 await getBlacklistedVideosList(server.url, accessTokenUser)
419 })
420
0e1dc3e7
C
421 it('Should be able to remove this user', async function () {
422 await removeUser(server.url, userId, accessToken)
423 })
424
425 it('Should not be able to login with this user', async function () {
f8b8c36b 426 await userLogin(server, user, 400)
0e1dc3e7
C
427 })
428
429 it('Should not have videos of this user', async function () {
430 const res = await getVideosList(server.url)
431
a7ba16b6 432 expect(res.body.total).to.equal(1)
0e1dc3e7 433
afffe988 434 const video = res.body.data[ 0 ]
a7ba16b6 435 expect(video.accountName).to.equal('root')
0e1dc3e7
C
436 })
437
438 it('Should register a new user', async function () {
439 await registerUser(server.url, 'user_15', 'my super password')
440 })
441
442 it('Should be able to login with this registered user', async function () {
f8b8c36b 443 const user15 = {
0e1dc3e7
C
444 username: 'user_15',
445 password: 'my super password'
446 }
447
f8b8c36b 448 accessToken = await userLogin(server, user15)
5c98d3bf
C
449 })
450
451 it('Should have the correct video quota', async function () {
452 const res = await getMyUserInformation(server.url, accessToken)
453 const user = res.body
454
a7ba16b6 455 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
0e1dc3e7
C
456 })
457
458 after(async function () {
459 killallServers([ server ])
460
461 // Keep the logs if the test failed
afffe988 462 if (this[ 'ok' ]) {
0e1dc3e7
C
463 await flushTests()
464 }
465 })
466})