1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 buildAbsoluteFixturePath,
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
16 setAccessTokensToServers,
18 } from '@shared/server-commands'
19 import { HttpStatusCode, UserRole, VideoCreateResult } from '@shared/models'
21 describe('Test my user API validators', function () {
22 const path = '/api/v1/users/'
25 let moderatorId: number
26 let video: VideoCreateResult
27 let server: PeerTubeServer
29 let moderatorToken = ''
31 // ---------------------------------------------------------------
33 before(async function () {
37 server = await createSingleServer(1)
38 await setAccessTokensToServers([ server ])
42 const result = await server.users.generate('user1')
43 userToken = result.token
44 userId = result.userId
48 const result = await server.users.generate('moderator1', UserRole.MODERATOR)
49 moderatorToken = result.token
53 const result = await server.users.generate('moderator2', UserRole.MODERATOR)
54 moderatorId = result.userId
58 video = await server.videos.upload()
62 describe('When updating my account', function () {
64 it('Should fail with an invalid email attribute', async function () {
69 await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
72 it('Should fail with a too small password', async function () {
74 currentPassword: 'password',
78 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
81 it('Should fail with a too long password', async function () {
83 currentPassword: 'password',
84 password: 'super'.repeat(61)
87 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
90 it('Should fail without the current password', async function () {
92 currentPassword: 'password',
93 password: 'super'.repeat(61)
96 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
99 it('Should fail with an invalid current password', async function () {
101 currentPassword: 'my super password fail',
102 password: 'super'.repeat(61)
105 await makePutBodyRequest({
110 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
114 it('Should fail with an invalid NSFW policy attribute', async function () {
119 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
122 it('Should fail with an invalid autoPlayVideo attribute', async function () {
127 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
130 it('Should fail with an invalid autoPlayNextVideo attribute', async function () {
132 autoPlayNextVideo: -1
135 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
138 it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
140 videosHistoryEnabled: -1
143 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
146 it('Should fail with an non authenticated user', async function () {
148 currentPassword: 'password',
149 password: 'my super password'
152 await makePutBodyRequest({
155 token: 'super token',
157 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
161 it('Should fail with a too long description', async function () {
163 description: 'super'.repeat(201)
166 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
169 it('Should fail with an invalid videoLanguages attribute', async function () {
172 videoLanguages: 'toto'
175 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
180 for (let i = 0; i < 1000; i++) {
185 videoLanguages: languages
188 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
192 it('Should fail with an invalid theme', async function () {
193 const fields = { theme: 'invalid' }
194 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
197 it('Should fail with an unknown theme', async function () {
198 const fields = { theme: 'peertube-theme-unknown' }
199 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
202 it('Should fail with invalid no modal attributes', async function () {
204 'noInstanceConfigWarningModal',
205 'noAccountSetupWarningModal',
209 for (const key of keys) {
214 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
218 it('Should succeed to change password with the correct params', async function () {
220 currentPassword: 'password',
221 password: 'my super password',
223 autoPlayVideo: false,
224 email: 'super_email@example.com',
226 noInstanceConfigWarningModal: true,
227 noWelcomeModal: true,
228 noAccountSetupWarningModal: true
231 await makePutBodyRequest({
236 expectedStatus: HttpStatusCode.NO_CONTENT_204
240 it('Should succeed without password change with the correct params', async function () {
246 await makePutBodyRequest({
251 expectedStatus: HttpStatusCode.NO_CONTENT_204
256 describe('When updating my avatar', function () {
257 it('Should fail without an incorrect input file', async function () {
260 avatarfile: buildAbsoluteFixturePath('video_short.mp4')
262 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
265 it('Should fail with a big file', async function () {
268 avatarfile: buildAbsoluteFixturePath('avatar-big.png')
270 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
273 it('Should fail with an unauthenticated user', async function () {
276 avatarfile: buildAbsoluteFixturePath('avatar.png')
278 await makeUploadRequest({
280 path: path + '/me/avatar/pick',
283 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
287 it('Should succeed with the correct params', async function () {
290 avatarfile: buildAbsoluteFixturePath('avatar.png')
292 await makeUploadRequest({
294 path: path + '/me/avatar/pick',
295 token: server.accessToken,
298 expectedStatus: HttpStatusCode.OK_200
303 describe('When managing my scoped tokens', function () {
305 it('Should fail to get my scoped tokens with an non authenticated user', async function () {
306 await server.users.getMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
309 it('Should fail to get my scoped tokens with a bad token', async function () {
310 await server.users.getMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
314 it('Should succeed to get my scoped tokens', async function () {
315 await server.users.getMyScopedTokens()
318 it('Should fail to renew my scoped tokens with an non authenticated user', async function () {
319 await server.users.renewMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
322 it('Should fail to renew my scoped tokens with a bad token', async function () {
323 await server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
326 it('Should succeed to renew my scoped tokens', async function () {
327 await server.users.renewMyScopedTokens()
331 describe('When getting my information', function () {
332 it('Should fail with a non authenticated user', async function () {
333 await server.users.getMyInfo({ token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
336 it('Should success with the correct parameters', async function () {
337 await server.users.getMyInfo({ token: userToken })
341 describe('When getting my video rating', function () {
342 let command: UsersCommand
345 command = server.users
348 it('Should fail with a non authenticated user', async function () {
349 await command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
352 it('Should fail with an incorrect video uuid', async function () {
353 await command.getMyRating({ videoId: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
356 it('Should fail with an unknown video', async function () {
357 await command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
360 it('Should succeed with the correct parameters', async function () {
361 await command.getMyRating({ videoId: video.id })
362 await command.getMyRating({ videoId: video.uuid })
363 await command.getMyRating({ videoId: video.shortUUID })
367 describe('When retrieving my global ratings', function () {
368 const path = '/api/v1/accounts/user1/ratings'
370 it('Should fail with a bad start pagination', async function () {
371 await checkBadStartPagination(server.url, path, userToken)
374 it('Should fail with a bad count pagination', async function () {
375 await checkBadCountPagination(server.url, path, userToken)
378 it('Should fail with an incorrect sort', async function () {
379 await checkBadSortPagination(server.url, path, userToken)
382 it('Should fail with a unauthenticated user', async function () {
383 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
386 it('Should fail with a another user', async function () {
387 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
390 it('Should fail with a bad type', async function () {
391 await makeGetRequest({
395 query: { rating: 'toto ' },
396 expectedStatus: HttpStatusCode.BAD_REQUEST_400
400 it('Should succeed with the correct params', async function () {
401 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
405 describe('When getting my global followers', function () {
406 const path = '/api/v1/accounts/user1/followers'
408 it('Should fail with a bad start pagination', async function () {
409 await checkBadStartPagination(server.url, path, userToken)
412 it('Should fail with a bad count pagination', async function () {
413 await checkBadCountPagination(server.url, path, userToken)
416 it('Should fail with an incorrect sort', async function () {
417 await checkBadSortPagination(server.url, path, userToken)
420 it('Should fail with a unauthenticated user', async function () {
421 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
424 it('Should fail with a another user', async function () {
425 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
428 it('Should succeed with the correct params', async function () {
429 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
433 describe('When blocking/unblocking/removing user', function () {
435 it('Should fail with an incorrect id', async function () {
436 const options = { userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
438 await server.users.remove(options)
439 await server.users.banUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
440 await server.users.unbanUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
443 it('Should fail with the root user', async function () {
444 const options = { userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
446 await server.users.remove(options)
447 await server.users.banUser(options)
448 await server.users.unbanUser(options)
451 it('Should return 404 with a non existing id', async function () {
452 const options = { userId: 4545454, expectedStatus: HttpStatusCode.NOT_FOUND_404 }
454 await server.users.remove(options)
455 await server.users.banUser(options)
456 await server.users.unbanUser(options)
459 it('Should fail with a non admin user', async function () {
460 const options = { userId, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
462 await server.users.remove(options)
463 await server.users.banUser(options)
464 await server.users.unbanUser(options)
467 it('Should fail on a moderator with a moderator', async function () {
468 const options = { userId: moderatorId, token: moderatorToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
470 await server.users.remove(options)
471 await server.users.banUser(options)
472 await server.users.unbanUser(options)
475 it('Should succeed on a user with a moderator', async function () {
476 const options = { userId, token: moderatorToken }
478 await server.users.banUser(options)
479 await server.users.unbanUser(options)
483 describe('When deleting our account', function () {
485 it('Should fail with with the root account', async function () {
486 await server.users.deleteMe({ expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
490 after(async function () {
491 MockSmtpServer.Instance.kill()
493 await cleanupTests([ server ])