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