1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, MockSmtpServer } from '@server/tests/shared'
4 import { buildAbsoluteFixturePath } from '@shared/core-utils'
5 import { HttpStatusCode, UserRole, VideoCreateResult } from '@shared/models'
13 setAccessTokensToServers,
15 } from '@shared/server-commands'
17 describe('Test my user API validators', function () {
18 const path = '/api/v1/users/'
21 let moderatorId: number
22 let video: VideoCreateResult
23 let server: PeerTubeServer
25 let moderatorToken = ''
27 // ---------------------------------------------------------------
29 before(async function () {
33 server = await createSingleServer(1)
34 await setAccessTokensToServers([ server ])
38 const result = await server.users.generate('user1')
39 userToken = result.token
40 userId = result.userId
44 const result = await server.users.generate('moderator1', UserRole.MODERATOR)
45 moderatorToken = result.token
49 const result = await server.users.generate('moderator2', UserRole.MODERATOR)
50 moderatorId = result.userId
54 video = await server.videos.upload()
58 describe('When updating my account', function () {
60 it('Should fail with an invalid email attribute', async function () {
65 await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
68 it('Should fail with a too small password', async function () {
70 currentPassword: 'password',
74 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
77 it('Should fail with a too long password', async function () {
79 currentPassword: 'password',
80 password: 'super'.repeat(61)
83 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
86 it('Should fail without the current password', async function () {
88 currentPassword: 'password',
89 password: 'super'.repeat(61)
92 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
95 it('Should fail with an invalid current password', async function () {
97 currentPassword: 'my super password fail',
98 password: 'super'.repeat(61)
101 await makePutBodyRequest({
106 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
110 it('Should fail with an invalid NSFW policy attribute', async function () {
115 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
118 it('Should fail with an invalid autoPlayVideo attribute', async function () {
123 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
126 it('Should fail with an invalid autoPlayNextVideo attribute', async function () {
128 autoPlayNextVideo: -1
131 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
134 it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
136 videosHistoryEnabled: -1
139 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
142 it('Should fail with an non authenticated user', async function () {
144 currentPassword: 'password',
145 password: 'my super password'
148 await makePutBodyRequest({
151 token: 'super token',
153 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
157 it('Should fail with a too long description', async function () {
159 description: 'super'.repeat(201)
162 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
165 it('Should fail with an invalid videoLanguages attribute', async function () {
168 videoLanguages: 'toto'
171 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
176 for (let i = 0; i < 1000; i++) {
181 videoLanguages: languages
184 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
188 it('Should fail with an invalid theme', async function () {
189 const fields = { theme: 'invalid' }
190 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
193 it('Should fail with an unknown theme', async function () {
194 const fields = { theme: 'peertube-theme-unknown' }
195 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
198 it('Should fail with invalid no modal attributes', async function () {
200 'noInstanceConfigWarningModal',
201 'noAccountSetupWarningModal',
205 for (const key of keys) {
210 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
214 it('Should succeed to change password with the correct params', async function () {
216 currentPassword: 'password',
217 password: 'my super password',
219 autoPlayVideo: false,
220 email: 'super_email@example.com',
222 noInstanceConfigWarningModal: true,
223 noWelcomeModal: true,
224 noAccountSetupWarningModal: true
227 await makePutBodyRequest({
232 expectedStatus: HttpStatusCode.NO_CONTENT_204
236 it('Should succeed without password change with the correct params', async function () {
242 await makePutBodyRequest({
247 expectedStatus: HttpStatusCode.NO_CONTENT_204
252 describe('When updating my avatar', function () {
253 it('Should fail without an incorrect input file', async function () {
256 avatarfile: buildAbsoluteFixturePath('video_short.mp4')
258 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
261 it('Should fail with a big file', async function () {
264 avatarfile: buildAbsoluteFixturePath('avatar-big.png')
266 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
269 it('Should fail with an unauthenticated user', async function () {
272 avatarfile: buildAbsoluteFixturePath('avatar.png')
274 await makeUploadRequest({
276 path: path + '/me/avatar/pick',
279 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
283 it('Should succeed with the correct params', async function () {
286 avatarfile: buildAbsoluteFixturePath('avatar.png')
288 await makeUploadRequest({
290 path: path + '/me/avatar/pick',
291 token: server.accessToken,
294 expectedStatus: HttpStatusCode.OK_200
299 describe('When managing my scoped tokens', function () {
301 it('Should fail to get my scoped tokens with an non authenticated user', async function () {
302 await server.users.getMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
305 it('Should fail to get my scoped tokens with a bad token', async function () {
306 await server.users.getMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
310 it('Should succeed to get my scoped tokens', async function () {
311 await server.users.getMyScopedTokens()
314 it('Should fail to renew my scoped tokens with an non authenticated user', async function () {
315 await server.users.renewMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
318 it('Should fail to renew my scoped tokens with a bad token', async function () {
319 await server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
322 it('Should succeed to renew my scoped tokens', async function () {
323 await server.users.renewMyScopedTokens()
327 describe('When getting my information', function () {
328 it('Should fail with a non authenticated user', async function () {
329 await server.users.getMyInfo({ token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
332 it('Should success with the correct parameters', async function () {
333 await server.users.getMyInfo({ token: userToken })
337 describe('When getting my video rating', function () {
338 let command: UsersCommand
341 command = server.users
344 it('Should fail with a non authenticated user', async function () {
345 await command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
348 it('Should fail with an incorrect video uuid', async function () {
349 await command.getMyRating({ videoId: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
352 it('Should fail with an unknown video', async function () {
353 await command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
356 it('Should succeed with the correct parameters', async function () {
357 await command.getMyRating({ videoId: video.id })
358 await command.getMyRating({ videoId: video.uuid })
359 await command.getMyRating({ videoId: video.shortUUID })
363 describe('When retrieving my global ratings', function () {
364 const path = '/api/v1/accounts/user1/ratings'
366 it('Should fail with a bad start pagination', async function () {
367 await checkBadStartPagination(server.url, path, userToken)
370 it('Should fail with a bad count pagination', async function () {
371 await checkBadCountPagination(server.url, path, userToken)
374 it('Should fail with an incorrect sort', async function () {
375 await checkBadSortPagination(server.url, path, userToken)
378 it('Should fail with a unauthenticated user', async function () {
379 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
382 it('Should fail with a another user', async function () {
383 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
386 it('Should fail with a bad type', async function () {
387 await makeGetRequest({
391 query: { rating: 'toto ' },
392 expectedStatus: HttpStatusCode.BAD_REQUEST_400
396 it('Should succeed with the correct params', async function () {
397 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
401 describe('When getting my global followers', function () {
402 const path = '/api/v1/accounts/user1/followers'
404 it('Should fail with a bad start pagination', async function () {
405 await checkBadStartPagination(server.url, path, userToken)
408 it('Should fail with a bad count pagination', async function () {
409 await checkBadCountPagination(server.url, path, userToken)
412 it('Should fail with an incorrect sort', async function () {
413 await checkBadSortPagination(server.url, path, userToken)
416 it('Should fail with a unauthenticated user', async function () {
417 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
420 it('Should fail with a another user', async function () {
421 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
424 it('Should succeed with the correct params', async function () {
425 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
429 describe('When blocking/unblocking/removing user', function () {
431 it('Should fail with an incorrect id', async function () {
432 const options = { userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
434 await server.users.remove(options)
435 await server.users.banUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
436 await server.users.unbanUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
439 it('Should fail with the root user', async function () {
440 const options = { userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
442 await server.users.remove(options)
443 await server.users.banUser(options)
444 await server.users.unbanUser(options)
447 it('Should return 404 with a non existing id', async function () {
448 const options = { userId: 4545454, expectedStatus: HttpStatusCode.NOT_FOUND_404 }
450 await server.users.remove(options)
451 await server.users.banUser(options)
452 await server.users.unbanUser(options)
455 it('Should fail with a non admin user', async function () {
456 const options = { userId, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
458 await server.users.remove(options)
459 await server.users.banUser(options)
460 await server.users.unbanUser(options)
463 it('Should fail on a moderator with a moderator', async function () {
464 const options = { userId: moderatorId, token: moderatorToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
466 await server.users.remove(options)
467 await server.users.banUser(options)
468 await server.users.unbanUser(options)
471 it('Should succeed on a user with a moderator', async function () {
472 const options = { userId, token: moderatorToken }
474 await server.users.banUser(options)
475 await server.users.unbanUser(options)
479 describe('When deleting our account', function () {
481 it('Should fail with with the root account', async function () {
482 await server.users.deleteMe({ expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
486 after(async function () {
487 MockSmtpServer.Instance.kill()
489 await cleanupTests([ server ])