1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, MockSmtpServer } from '@server/tests/shared'
5 import { buildAbsoluteFixturePath } from '@shared/core-utils'
6 import { HttpStatusCode, UserRole, VideoCreateResult } from '@shared/models'
14 setAccessTokensToServers,
16 } from '@shared/server-commands'
18 describe('Test my user API validators', function () {
19 const path = '/api/v1/users/'
22 let moderatorId: number
23 let video: VideoCreateResult
24 let server: PeerTubeServer
26 let moderatorToken = ''
28 // ---------------------------------------------------------------
30 before(async function () {
34 server = await createSingleServer(1)
35 await setAccessTokensToServers([ server ])
39 const result = await server.users.generate('user1')
40 userToken = result.token
41 userId = result.userId
45 const result = await server.users.generate('moderator1', UserRole.MODERATOR)
46 moderatorToken = result.token
50 const result = await server.users.generate('moderator2', UserRole.MODERATOR)
51 moderatorId = result.userId
55 video = await server.videos.upload()
59 describe('When updating my account', function () {
61 it('Should fail with an invalid email attribute', async function () {
66 await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
69 it('Should fail with a too small password', async function () {
71 currentPassword: 'password',
75 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
78 it('Should fail with a too long password', async function () {
80 currentPassword: 'password',
81 password: 'super'.repeat(61)
84 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
87 it('Should fail without the current password', async function () {
89 currentPassword: 'password',
90 password: 'super'.repeat(61)
93 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
96 it('Should fail with an invalid current password', async function () {
98 currentPassword: 'my super password fail',
99 password: 'super'.repeat(61)
102 await makePutBodyRequest({
107 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
111 it('Should fail with an invalid NSFW policy attribute', async function () {
116 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
119 it('Should fail with an invalid autoPlayVideo attribute', async function () {
124 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
127 it('Should fail with an invalid autoPlayNextVideo attribute', async function () {
129 autoPlayNextVideo: -1
132 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
135 it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
137 videosHistoryEnabled: -1
140 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
143 it('Should fail with an non authenticated user', async function () {
145 currentPassword: 'password',
146 password: 'my super password'
149 await makePutBodyRequest({
152 token: 'super token',
154 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
158 it('Should fail with a too long description', async function () {
160 description: 'super'.repeat(201)
163 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
166 it('Should fail with an invalid videoLanguages attribute', async function () {
169 videoLanguages: 'toto'
172 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
177 for (let i = 0; i < 1000; i++) {
182 videoLanguages: languages
185 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
189 it('Should fail with an invalid theme', async function () {
190 const fields = { theme: 'invalid' }
191 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
194 it('Should fail with an unknown theme', async function () {
195 const fields = { theme: 'peertube-theme-unknown' }
196 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
199 it('Should fail with invalid no modal attributes', async function () {
201 'noInstanceConfigWarningModal',
202 'noAccountSetupWarningModal',
206 for (const key of keys) {
211 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
215 it('Should succeed to change password with the correct params', async function () {
217 currentPassword: 'password',
218 password: 'my super password',
220 autoPlayVideo: false,
221 email: 'super_email@example.com',
223 noInstanceConfigWarningModal: true,
224 noWelcomeModal: true,
225 noAccountSetupWarningModal: true
228 await makePutBodyRequest({
233 expectedStatus: HttpStatusCode.NO_CONTENT_204
237 it('Should succeed without password change with the correct params', async function () {
243 await makePutBodyRequest({
248 expectedStatus: HttpStatusCode.NO_CONTENT_204
253 describe('When updating my avatar', function () {
254 it('Should fail without an incorrect input file', async function () {
257 avatarfile: buildAbsoluteFixturePath('video_short.mp4')
259 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
262 it('Should fail with a big file', async function () {
265 avatarfile: buildAbsoluteFixturePath('avatar-big.png')
267 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
270 it('Should fail with an unauthenticated user', async function () {
273 avatarfile: buildAbsoluteFixturePath('avatar.png')
275 await makeUploadRequest({
277 path: path + '/me/avatar/pick',
280 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
284 it('Should succeed with the correct params', async function () {
287 avatarfile: buildAbsoluteFixturePath('avatar.png')
289 await makeUploadRequest({
291 path: path + '/me/avatar/pick',
292 token: server.accessToken,
295 expectedStatus: HttpStatusCode.OK_200
300 describe('When managing my scoped tokens', function () {
302 it('Should fail to get my scoped tokens with an non authenticated user', async function () {
303 await server.users.getMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
306 it('Should fail to get my scoped tokens with a bad token', async function () {
307 await server.users.getMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
311 it('Should succeed to get my scoped tokens', async function () {
312 await server.users.getMyScopedTokens()
315 it('Should fail to renew my scoped tokens with an non authenticated user', async function () {
316 await server.users.renewMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
319 it('Should fail to renew my scoped tokens with a bad token', async function () {
320 await server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
323 it('Should succeed to renew my scoped tokens', async function () {
324 await server.users.renewMyScopedTokens()
328 describe('When getting my information', function () {
329 it('Should fail with a non authenticated user', async function () {
330 await server.users.getMyInfo({ token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
333 it('Should success with the correct parameters', async function () {
334 await server.users.getMyInfo({ token: userToken })
338 describe('When getting my video rating', function () {
339 let command: UsersCommand
342 command = server.users
345 it('Should fail with a non authenticated user', async function () {
346 await command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
349 it('Should fail with an incorrect video uuid', async function () {
350 await command.getMyRating({ videoId: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
353 it('Should fail with an unknown video', async function () {
354 await command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
357 it('Should succeed with the correct parameters', async function () {
358 await command.getMyRating({ videoId: video.id })
359 await command.getMyRating({ videoId: video.uuid })
360 await command.getMyRating({ videoId: video.shortUUID })
364 describe('When retrieving my global ratings', function () {
365 const path = '/api/v1/accounts/user1/ratings'
367 it('Should fail with a bad start pagination', async function () {
368 await checkBadStartPagination(server.url, path, userToken)
371 it('Should fail with a bad count pagination', async function () {
372 await checkBadCountPagination(server.url, path, userToken)
375 it('Should fail with an incorrect sort', async function () {
376 await checkBadSortPagination(server.url, path, userToken)
379 it('Should fail with a unauthenticated user', async function () {
380 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
383 it('Should fail with a another user', async function () {
384 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
387 it('Should fail with a bad type', async function () {
388 await makeGetRequest({
392 query: { rating: 'toto ' },
393 expectedStatus: HttpStatusCode.BAD_REQUEST_400
397 it('Should succeed with the correct params', async function () {
398 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
402 describe('When getting my global followers', function () {
403 const path = '/api/v1/accounts/user1/followers'
405 it('Should fail with a bad start pagination', async function () {
406 await checkBadStartPagination(server.url, path, userToken)
409 it('Should fail with a bad count pagination', async function () {
410 await checkBadCountPagination(server.url, path, userToken)
413 it('Should fail with an incorrect sort', async function () {
414 await checkBadSortPagination(server.url, path, userToken)
417 it('Should fail with a unauthenticated user', async function () {
418 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
421 it('Should fail with a another user', async function () {
422 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
425 it('Should succeed with the correct params', async function () {
426 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
430 describe('When blocking/unblocking/removing user', function () {
432 it('Should fail with an incorrect id', async function () {
433 const options = { userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
435 await server.users.remove(options)
436 await server.users.banUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
437 await server.users.unbanUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
440 it('Should fail with the root user', async function () {
441 const options = { userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
443 await server.users.remove(options)
444 await server.users.banUser(options)
445 await server.users.unbanUser(options)
448 it('Should return 404 with a non existing id', async function () {
449 const options = { userId: 4545454, expectedStatus: HttpStatusCode.NOT_FOUND_404 }
451 await server.users.remove(options)
452 await server.users.banUser(options)
453 await server.users.unbanUser(options)
456 it('Should fail with a non admin user', async function () {
457 const options = { userId, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
459 await server.users.remove(options)
460 await server.users.banUser(options)
461 await server.users.unbanUser(options)
464 it('Should fail on a moderator with a moderator', async function () {
465 const options = { userId: moderatorId, token: moderatorToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
467 await server.users.remove(options)
468 await server.users.banUser(options)
469 await server.users.unbanUser(options)
472 it('Should succeed on a user with a moderator', async function () {
473 const options = { userId, token: moderatorToken }
475 await server.users.banUser(options)
476 await server.users.unbanUser(options)
480 describe('When deleting our account', function () {
482 it('Should fail with with the root account', async function () {
483 await server.users.deleteMe({ expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
487 after(async function () {
488 MockSmtpServer.Instance.kill()
490 await cleanupTests([ server ])