]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
d8004ff24f560e0e36d72bcf235b1bbb2dee2486
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { UserRole } from '../../../../shared/index'
6 import {
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating, getUserInformation,
8 getUsersList,
9 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
10 runServer, ServerInfo, serverLogin, testImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo
11 } from '../../utils/index'
12 import { follow } from '../../utils/server/follows'
13 import { setAccessTokensToServers } from '../../utils/users/login'
14 import { getMyVideos } from '../../utils/videos/videos'
15
16 const expect = chai.expect
17
18 describe('Test users', function () {
19 let server: ServerInfo
20 let accessToken: string
21 let accessTokenUser: string
22 let videoId: number
23 let userId: number
24
25 before(async function () {
26 this.timeout(10000)
27
28 await flushTests()
29 server = await runServer(1)
30
31 await setAccessTokensToServers([ server ])
32 })
33
34 it('Should create a new client')
35
36 it('Should return the first client')
37
38 it('Should remove the last client')
39
40 it('Should not login with an invalid client id', async function () {
41 const client = { id: 'client', secret: server.client.secret }
42 const res = await login(server.url, client, server.user, 400)
43
44 expect(res.body.error).to.equal('Authentication failed.')
45 })
46
47 it('Should not login with an invalid client secret', async function () {
48 const client = { id: server.client.id, secret: 'coucou' }
49 const res = await login(server.url, client, server.user, 400)
50
51 expect(res.body.error).to.equal('Authentication failed.')
52 })
53
54 it('Should not login with an invalid username', async function () {
55 const user = { username: 'captain crochet', password: server.user.password }
56 const res = await login(server.url, server.client, user, 400)
57
58 expect(res.body.error).to.equal('Authentication failed.')
59 })
60
61 it('Should not login with an invalid password', async function () {
62 const user = { username: server.user.username, password: 'mew_three' }
63 const res = await login(server.url, server.client, user, 400)
64
65 expect(res.body.error).to.equal('Authentication failed.')
66 })
67
68 it('Should not be able to upload a video', async function () {
69 accessToken = 'my_super_token'
70
71 const videoAttributes = {}
72 await uploadVideo(server.url, accessToken, videoAttributes, 401)
73 })
74
75 it('Should not be able to follow', async function () {
76 accessToken = 'my_super_token'
77 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
78 })
79
80 it('Should not be able to unfollow')
81
82 it('Should be able to login', async function () {
83 const res = await login(server.url, server.client, server.user, 200)
84
85 accessToken = res.body.access_token
86 })
87
88 it('Should upload the video with the correct token', async function () {
89 const videoAttributes = {}
90 await uploadVideo(server.url, accessToken, videoAttributes)
91 const res = await getVideosList(server.url)
92 const video = res.body.data[ 0 ]
93
94 expect(video.accountName).to.equal('root')
95 videoId = video.id
96 })
97
98 it('Should upload the video again with the correct token', async function () {
99 const videoAttributes = {}
100 await uploadVideo(server.url, accessToken, videoAttributes)
101 })
102
103 it('Should retrieve a video rating', async function () {
104 await rateVideo(server.url, accessToken, videoId, 'like')
105 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
106 const rating = res.body
107
108 expect(rating.videoId).to.equal(videoId)
109 expect(rating.rating).to.equal('like')
110 })
111
112 it('Should not be able to remove the video with an incorrect token', async function () {
113 await removeVideo(server.url, 'bad_token', videoId, 401)
114 })
115
116 it('Should not be able to remove the video with the token of another account')
117
118 it('Should be able to remove the video with the correct token', async function () {
119 await removeVideo(server.url, accessToken, videoId)
120 })
121
122 it('Should logout (revoke token)')
123
124 it('Should not be able to get the user information')
125
126 it('Should not be able to upload a video')
127
128 it('Should not be able to remove a video')
129
130 it('Should not be able to rate a video', async function () {
131 const path = '/api/v1/videos/'
132 const data = {
133 rating: 'likes'
134 }
135
136 const options = {
137 url: server.url,
138 path: path + videoId,
139 token: 'wrong token',
140 fields: data,
141 statusCodeExpected: 401
142 }
143 await makePutBodyRequest(options)
144 })
145
146 it('Should be able to login again')
147
148 it('Should have an expired access token')
149
150 it('Should refresh the token')
151
152 it('Should be able to upload a video again')
153
154 it('Should be able to create a new user', async function () {
155 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
156 })
157
158 it('Should be able to login with this user', async function () {
159 server.user = {
160 username: 'user_1',
161 password: 'super password'
162 }
163
164 accessTokenUser = await serverLogin(server)
165 })
166
167 it('Should be able to get the user information', async function () {
168 const res = await getMyUserInformation(server.url, accessTokenUser)
169 const user = res.body
170
171 expect(user.username).to.equal('user_1')
172 expect(user.email).to.equal('user_1@example.com')
173 expect(user.displayNSFW).to.be.false
174 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
175 expect(user.roleLabel).to.equal('User')
176 expect(user.id).to.be.a('number')
177 })
178
179 it('Should be able to upload a video with this user', async function () {
180 this.timeout(5000)
181
182 const videoAttributes = {
183 name: 'super user video',
184 fixture: 'video_short.webm'
185 }
186 await uploadVideo(server.url, accessTokenUser, videoAttributes)
187 })
188
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
196 it('Should be able to list my videos', async function () {
197 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
198 expect(res.body.total).to.equal(1)
199
200 const videos = res.body.data
201 expect(videos).to.have.lengthOf(1)
202
203 expect(videos[ 0 ].name).to.equal('super user video')
204 })
205
206 it('Should list all the users', async function () {
207 const res = await getUsersList(server.url, server.accessToken)
208 const result = res.body
209 const total = result.total
210 const users = result.data
211
212 expect(total).to.equal(2)
213 expect(users).to.be.an('array')
214 expect(users.length).to.equal(2)
215
216 const user = users[ 0 ]
217 expect(user.username).to.equal('user_1')
218 expect(user.email).to.equal('user_1@example.com')
219 expect(user.displayNSFW).to.be.false
220
221 const rootUser = users[ 1 ]
222 expect(rootUser.username).to.equal('root')
223 expect(rootUser.email).to.equal('admin1@example.com')
224 expect(rootUser.displayNSFW).to.be.false
225
226 userId = user.id
227 })
228
229 it('Should list only the first user by username asc', async function () {
230 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
231
232 const result = res.body
233 const total = result.total
234 const users = result.data
235
236 expect(total).to.equal(2)
237 expect(users.length).to.equal(1)
238
239 const user = users[ 0 ]
240 expect(user.username).to.equal('root')
241 expect(user.email).to.equal('admin1@example.com')
242 expect(user.roleLabel).to.equal('Administrator')
243 expect(user.displayNSFW).to.be.false
244 })
245
246 it('Should list only the first user by username desc', async function () {
247 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
248 const result = res.body
249 const total = result.total
250 const users = result.data
251
252 expect(total).to.equal(2)
253 expect(users.length).to.equal(1)
254
255 const user = users[ 0 ]
256 expect(user.username).to.equal('user_1')
257 expect(user.email).to.equal('user_1@example.com')
258 expect(user.displayNSFW).to.be.false
259 })
260
261 it('Should list only the second user by createdAt desc', async function () {
262 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
263 const result = res.body
264 const total = result.total
265 const users = result.data
266
267 expect(total).to.equal(2)
268 expect(users.length).to.equal(1)
269
270 const user = users[ 0 ]
271 expect(user.username).to.equal('user_1')
272 expect(user.email).to.equal('user_1@example.com')
273 expect(user.displayNSFW).to.be.false
274 })
275
276 it('Should list all the users by createdAt asc', async function () {
277 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
278 const result = res.body
279 const total = result.total
280 const users = result.data
281
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')
287 expect(users[ 0 ].displayNSFW).to.be.false
288
289 expect(users[ 1 ].username).to.equal('user_1')
290 expect(users[ 1 ].email).to.equal('user_1@example.com')
291 expect(users[ 1 ].displayNSFW).to.be.false
292 })
293
294 it('Should update my password', async function () {
295 await updateMyUser({
296 url: server.url,
297 accessToken: accessTokenUser,
298 newPassword: 'new password'
299 })
300 server.user.password = 'new password'
301
302 await login(server.url, server.client, server.user, 200)
303 })
304
305 it('Should be able to change the NSFW display attribute', async function () {
306 await updateMyUser({
307 url: server.url,
308 accessToken: accessTokenUser,
309 displayNSFW: true
310 })
311
312 const res = await getMyUserInformation(server.url, accessTokenUser)
313 const user = res.body
314
315 expect(user.username).to.equal('user_1')
316 expect(user.email).to.equal('user_1@example.com')
317 expect(user.displayNSFW).to.be.ok
318 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
319 expect(user.id).to.be.a('number')
320 })
321
322 it('Should be able to change the autoPlayVideo attribute', async function () {
323 await updateMyUser({
324 url: server.url,
325 accessToken: accessTokenUser,
326 autoPlayVideo: false
327 })
328
329 const res = await getMyUserInformation(server.url, accessTokenUser)
330 const user = res.body
331
332 expect(user.autoPlayVideo).to.be.false
333 })
334
335 it('Should be able to change the email display attribute', async function () {
336 await updateMyUser({
337 url: server.url,
338 accessToken: accessTokenUser,
339 email: 'updated@example.com'
340 })
341
342 const res = await getMyUserInformation(server.url, accessTokenUser)
343 const user = res.body
344
345 expect(user.username).to.equal('user_1')
346 expect(user.email).to.equal('updated@example.com')
347 expect(user.displayNSFW).to.be.ok
348 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
349 expect(user.id).to.be.a('number')
350 })
351
352 it('Should be able to update my avatar', async function () {
353 const fixture = 'avatar.png'
354
355 await updateMyAvatar({
356 url: server.url,
357 accessToken: accessTokenUser,
358 fixture
359 })
360
361 const res = await getMyUserInformation(server.url, accessTokenUser)
362 const user = res.body
363
364 const test = await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
365 expect(test).to.equal(true)
366 })
367
368 it('Should be able to update another user', async function () {
369 await updateUser({
370 url: server.url,
371 userId,
372 accessToken,
373 email: 'updated2@example.com',
374 videoQuota: 42,
375 role: UserRole.MODERATOR
376 })
377
378 const res = await getUserInformation(server.url, accessToken, userId)
379 const user = res.body
380
381 expect(user.username).to.equal('user_1')
382 expect(user.email).to.equal('updated2@example.com')
383 expect(user.displayNSFW).to.be.ok
384 expect(user.videoQuota).to.equal(42)
385 expect(user.roleLabel).to.equal('Moderator')
386 expect(user.id).to.be.a('number')
387 })
388
389 it('Should not be able to delete a user by a moderator', async function () {
390 await removeUser(server.url, 2, accessTokenUser, 403)
391 })
392
393 it('Should be able to list video blacklist by a moderator', async function () {
394 await getBlacklistedVideosList(server.url, accessTokenUser)
395 })
396
397 it('Should be able to remove this user', async function () {
398 await removeUser(server.url, userId, accessToken)
399 })
400
401 it('Should not be able to login with this user', async function () {
402 // server.user is already set to user 1
403 await login(server.url, server.client, server.user, 400)
404 })
405
406 it('Should not have videos of this user', async function () {
407 const res = await getVideosList(server.url)
408
409 expect(res.body.total).to.equal(1)
410
411 const video = res.body.data[ 0 ]
412 expect(video.accountName).to.equal('root')
413 })
414
415 it('Should register a new user', async function () {
416 await registerUser(server.url, 'user_15', 'my super password')
417 })
418
419 it('Should be able to login with this registered user', async function () {
420 server.user = {
421 username: 'user_15',
422 password: 'my super password'
423 }
424
425 accessToken = await serverLogin(server)
426 })
427
428 it('Should have the correct video quota', async function () {
429 const res = await getMyUserInformation(server.url, accessToken)
430 const user = res.body
431
432 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
433 })
434
435 after(async function () {
436 killallServers([ server ])
437
438 // Keep the logs if the test failed
439 if (this[ 'ok' ]) {
440 await flushTests()
441 }
442 })
443 })