1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
6 buildAbsoluteFixturePath,
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
19 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { HttpStatusCode, UserAdminFlag, UserRole, VideoCreateResult } from '@shared/models'
24 describe('Test users API validators', function () {
25 const path = '/api/v1/users/'
28 let moderatorId: number
29 let video: VideoCreateResult
30 let server: PeerTubeServer
31 let serverWithRegistrationDisabled: PeerTubeServer
33 let moderatorToken = ''
35 let overrideConfig: Object
37 // ---------------------------------------------------------------
39 before(async function () {
42 const emails: object[] = []
43 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
45 overrideConfig = { signup: { limit: 8 } }
48 const res = await Promise.all([
49 createSingleServer(1, overrideConfig),
54 serverWithRegistrationDisabled = res[1]
56 await setAccessTokensToServers([ server ])
60 const user = { username: 'user1' }
61 await server.users.create({ ...user })
62 userToken = await server.login.getAccessToken(user)
66 const moderator = { username: 'moderator1' }
67 await server.users.create({ ...moderator, role: UserRole.MODERATOR })
68 moderatorToken = await server.login.getAccessToken(moderator)
72 const moderator = { username: 'moderator2' }
73 await server.users.create({ ...moderator, role: UserRole.MODERATOR })
77 video = await server.videos.upload()
81 const { data } = await server.users.list()
82 userId = data.find(u => u.username === 'user1').id
83 rootId = data.find(u => u.username === 'root').id
84 moderatorId = data.find(u => u.username === 'moderator2').id
88 describe('When listing users', function () {
89 it('Should fail with a bad start pagination', async function () {
90 await checkBadStartPagination(server.url, path, server.accessToken)
93 it('Should fail with a bad count pagination', async function () {
94 await checkBadCountPagination(server.url, path, server.accessToken)
97 it('Should fail with an incorrect sort', async function () {
98 await checkBadSortPagination(server.url, path, server.accessToken)
101 it('Should fail with a non authenticated user', async function () {
102 await makeGetRequest({
105 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
109 it('Should fail with a non admin user', async function () {
110 await makeGetRequest({
114 expectedStatus: HttpStatusCode.FORBIDDEN_403
119 describe('When adding a new user', function () {
120 const baseCorrectParams = {
122 email: 'test@example.com',
123 password: 'my super password',
127 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
130 it('Should fail with a too small username', async function () {
131 const fields = { ...baseCorrectParams, username: '' }
133 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
136 it('Should fail with a too long username', async function () {
137 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
139 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
142 it('Should fail with a not lowercase username', async function () {
143 const fields = { ...baseCorrectParams, username: 'Toto' }
145 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
148 it('Should fail with an incorrect username', async function () {
149 const fields = { ...baseCorrectParams, username: 'my username' }
151 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
154 it('Should fail with a missing email', async function () {
155 const fields = omit(baseCorrectParams, 'email')
157 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
160 it('Should fail with an invalid email', async function () {
161 const fields = { ...baseCorrectParams, email: 'test_example.com' }
163 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
166 it('Should fail with a too small password', async function () {
167 const fields = { ...baseCorrectParams, password: 'bla' }
169 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
172 it('Should fail with a too long password', async function () {
173 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
175 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
178 it('Should fail with empty password and no smtp configured', async function () {
179 const fields = { ...baseCorrectParams, password: '' }
181 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
184 it('Should succeed with no password on a server with smtp enabled', async function () {
187 await killallServers([ server ])
193 hostname: 'localhost',
197 await server.run(config)
200 ...baseCorrectParams,
203 username: 'create_password',
204 email: 'create_password@example.com'
207 await makePostBodyRequest({
210 token: server.accessToken,
212 expectedStatus: HttpStatusCode.OK_200
216 it('Should fail with invalid admin flags', async function () {
217 const fields = { ...baseCorrectParams, adminFlags: 'toto' }
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should fail with an non authenticated user', async function () {
223 await makePostBodyRequest({
226 token: 'super token',
227 fields: baseCorrectParams,
228 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
232 it('Should fail if we add a user with the same username', async function () {
233 const fields = { ...baseCorrectParams, username: 'user1' }
235 await makePostBodyRequest({
238 token: server.accessToken,
240 expectedStatus: HttpStatusCode.CONFLICT_409
244 it('Should fail if we add a user with the same email', async function () {
245 const fields = { ...baseCorrectParams, email: 'user1@example.com' }
247 await makePostBodyRequest({
250 token: server.accessToken,
252 expectedStatus: HttpStatusCode.CONFLICT_409
256 it('Should fail without a videoQuota', async function () {
257 const fields = omit(baseCorrectParams, 'videoQuota')
259 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
262 it('Should fail without a videoQuotaDaily', async function () {
263 const fields = omit(baseCorrectParams, 'videoQuotaDaily')
265 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
268 it('Should fail with an invalid videoQuota', async function () {
269 const fields = { ...baseCorrectParams, videoQuota: -5 }
271 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
274 it('Should fail with an invalid videoQuotaDaily', async function () {
275 const fields = { ...baseCorrectParams, videoQuotaDaily: -7 }
277 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
280 it('Should fail without a user role', async function () {
281 const fields = omit(baseCorrectParams, 'role')
283 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
286 it('Should fail with an invalid user role', async function () {
287 const fields = { ...baseCorrectParams, role: 88989 }
289 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
292 it('Should fail with a "peertube" username', async function () {
293 const fields = { ...baseCorrectParams, username: 'peertube' }
295 await makePostBodyRequest({
298 token: server.accessToken,
300 expectedStatus: HttpStatusCode.CONFLICT_409
304 it('Should fail to create a moderator or an admin with a moderator', async function () {
305 for (const role of [ UserRole.MODERATOR, UserRole.ADMINISTRATOR ]) {
306 const fields = { ...baseCorrectParams, role }
308 await makePostBodyRequest({
311 token: moderatorToken,
313 expectedStatus: HttpStatusCode.FORBIDDEN_403
318 it('Should succeed to create a user with a moderator', async function () {
319 const fields = { ...baseCorrectParams, username: 'a4656', email: 'a4656@example.com', role: UserRole.USER }
321 await makePostBodyRequest({
324 token: moderatorToken,
326 expectedStatus: HttpStatusCode.OK_200
330 it('Should succeed with the correct params', async function () {
331 await makePostBodyRequest({
334 token: server.accessToken,
335 fields: baseCorrectParams,
336 expectedStatus: HttpStatusCode.OK_200
340 it('Should fail with a non admin user', async function () {
341 const user = { username: 'user1' }
342 userToken = await server.login.getAccessToken(user)
346 email: 'test@example.com',
347 password: 'my super password',
350 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
354 describe('When updating my account', function () {
356 it('Should fail with an invalid email attribute', async function () {
361 await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
364 it('Should fail with a too small password', async function () {
366 currentPassword: 'password',
370 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
373 it('Should fail with a too long password', async function () {
375 currentPassword: 'password',
376 password: 'super'.repeat(61)
379 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
382 it('Should fail without the current password', async function () {
384 currentPassword: 'password',
385 password: 'super'.repeat(61)
388 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
391 it('Should fail with an invalid current password', async function () {
393 currentPassword: 'my super password fail',
394 password: 'super'.repeat(61)
397 await makePutBodyRequest({
402 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
406 it('Should fail with an invalid NSFW policy attribute', async function () {
411 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
414 it('Should fail with an invalid autoPlayVideo attribute', async function () {
419 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
422 it('Should fail with an invalid autoPlayNextVideo attribute', async function () {
424 autoPlayNextVideo: -1
427 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
430 it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
432 videosHistoryEnabled: -1
435 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
438 it('Should fail with an non authenticated user', async function () {
440 currentPassword: 'password',
441 password: 'my super password'
444 await makePutBodyRequest({
447 token: 'super token',
449 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
453 it('Should fail with a too long description', async function () {
455 description: 'super'.repeat(201)
458 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
461 it('Should fail with an invalid videoLanguages attribute', async function () {
464 videoLanguages: 'toto'
467 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
472 for (let i = 0; i < 1000; i++) {
477 videoLanguages: languages
480 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
484 it('Should fail with an invalid theme', async function () {
485 const fields = { theme: 'invalid' }
486 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
489 it('Should fail with an unknown theme', async function () {
490 const fields = { theme: 'peertube-theme-unknown' }
491 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
494 it('Should fail with invalid no modal attributes', async function () {
496 'noInstanceConfigWarningModal',
497 'noAccountSetupWarningModal',
501 for (const key of keys) {
506 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
510 it('Should succeed to change password with the correct params', async function () {
512 currentPassword: 'password',
513 password: 'my super password',
515 autoPlayVideo: false,
516 email: 'super_email@example.com',
518 noInstanceConfigWarningModal: true,
519 noWelcomeModal: true,
520 noAccountSetupWarningModal: true
523 await makePutBodyRequest({
528 expectedStatus: HttpStatusCode.NO_CONTENT_204
532 it('Should succeed without password change with the correct params', async function () {
538 await makePutBodyRequest({
543 expectedStatus: HttpStatusCode.NO_CONTENT_204
548 describe('When updating my avatar', function () {
549 it('Should fail without an incorrect input file', async function () {
552 avatarfile: buildAbsoluteFixturePath('video_short.mp4')
554 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
557 it('Should fail with a big file', async function () {
560 avatarfile: buildAbsoluteFixturePath('avatar-big.png')
562 await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
565 it('Should fail with an unauthenticated user', async function () {
568 avatarfile: buildAbsoluteFixturePath('avatar.png')
570 await makeUploadRequest({
572 path: path + '/me/avatar/pick',
575 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
579 it('Should succeed with the correct params', async function () {
582 avatarfile: buildAbsoluteFixturePath('avatar.png')
584 await makeUploadRequest({
586 path: path + '/me/avatar/pick',
587 token: server.accessToken,
590 expectedStatus: HttpStatusCode.OK_200
595 describe('When managing my scoped tokens', function () {
597 it('Should fail to get my scoped tokens with an non authenticated user', async function () {
598 await server.users.getMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
601 it('Should fail to get my scoped tokens with a bad token', async function () {
602 await server.users.getMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
606 it('Should succeed to get my scoped tokens', async function () {
607 await server.users.getMyScopedTokens()
610 it('Should fail to renew my scoped tokens with an non authenticated user', async function () {
611 await server.users.renewMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
614 it('Should fail to renew my scoped tokens with a bad token', async function () {
615 await server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
618 it('Should succeed to renew my scoped tokens', async function () {
619 await server.users.renewMyScopedTokens()
623 describe('When getting a user', function () {
625 it('Should fail with an non authenticated user', async function () {
626 await makeGetRequest({
629 token: 'super token',
630 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
634 it('Should fail with a non admin user', async function () {
635 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
638 it('Should succeed with the correct params', async function () {
639 await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
643 describe('When updating a user', function () {
645 it('Should fail with an invalid email attribute', async function () {
650 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
653 it('Should fail with an invalid emailVerified attribute', async function () {
658 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
661 it('Should fail with an invalid videoQuota attribute', async function () {
666 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
669 it('Should fail with an invalid user role attribute', async function () {
674 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
677 it('Should fail with a too small password', async function () {
679 currentPassword: 'password',
683 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
686 it('Should fail with a too long password', async function () {
688 currentPassword: 'password',
689 password: 'super'.repeat(61)
692 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
695 it('Should fail with an non authenticated user', async function () {
700 await makePutBodyRequest({
703 token: 'super token',
705 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
709 it('Should fail when updating root role', async function () {
711 role: UserRole.MODERATOR
714 await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields })
717 it('Should fail with invalid admin flags', async function () {
718 const fields = { adminFlags: 'toto' }
720 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
723 it('Should fail to update an admin with a moderator', async function () {
728 await makePutBodyRequest({
730 path: path + moderatorId,
731 token: moderatorToken,
733 expectedStatus: HttpStatusCode.FORBIDDEN_403
737 it('Should succeed to update a user with a moderator', async function () {
742 await makePutBodyRequest({
745 token: moderatorToken,
747 expectedStatus: HttpStatusCode.NO_CONTENT_204
751 it('Should succeed with the correct params', async function () {
753 email: 'email@example.com',
759 await makePutBodyRequest({
762 token: server.accessToken,
764 expectedStatus: HttpStatusCode.NO_CONTENT_204
769 describe('When getting my information', function () {
770 it('Should fail with a non authenticated user', async function () {
771 await server.users.getMyInfo({ token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
774 it('Should success with the correct parameters', async function () {
775 await server.users.getMyInfo({ token: userToken })
779 describe('When getting my video rating', function () {
780 let command: UsersCommand
783 command = server.users
786 it('Should fail with a non authenticated user', async function () {
787 await command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
790 it('Should fail with an incorrect video uuid', async function () {
791 await command.getMyRating({ videoId: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
794 it('Should fail with an unknown video', async function () {
795 await command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
798 it('Should succeed with the correct parameters', async function () {
799 await command.getMyRating({ videoId: video.id })
800 await command.getMyRating({ videoId: video.uuid })
801 await command.getMyRating({ videoId: video.shortUUID })
805 describe('When retrieving my global ratings', function () {
806 const path = '/api/v1/accounts/user1/ratings'
808 it('Should fail with a bad start pagination', async function () {
809 await checkBadStartPagination(server.url, path, userToken)
812 it('Should fail with a bad count pagination', async function () {
813 await checkBadCountPagination(server.url, path, userToken)
816 it('Should fail with an incorrect sort', async function () {
817 await checkBadSortPagination(server.url, path, userToken)
820 it('Should fail with a unauthenticated user', async function () {
821 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
824 it('Should fail with a another user', async function () {
825 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
828 it('Should fail with a bad type', async function () {
829 await makeGetRequest({
833 query: { rating: 'toto ' },
834 expectedStatus: HttpStatusCode.BAD_REQUEST_400
838 it('Should succeed with the correct params', async function () {
839 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
843 describe('When getting my global followers', function () {
844 const path = '/api/v1/accounts/user1/followers'
846 it('Should fail with a bad start pagination', async function () {
847 await checkBadStartPagination(server.url, path, userToken)
850 it('Should fail with a bad count pagination', async function () {
851 await checkBadCountPagination(server.url, path, userToken)
854 it('Should fail with an incorrect sort', async function () {
855 await checkBadSortPagination(server.url, path, userToken)
858 it('Should fail with a unauthenticated user', async function () {
859 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
862 it('Should fail with a another user', async function () {
863 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
866 it('Should succeed with the correct params', async function () {
867 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
871 describe('When blocking/unblocking/removing user', function () {
873 it('Should fail with an incorrect id', async function () {
874 const options = { userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
876 await server.users.remove(options)
877 await server.users.banUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
878 await server.users.unbanUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
881 it('Should fail with the root user', async function () {
882 const options = { userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
884 await server.users.remove(options)
885 await server.users.banUser(options)
886 await server.users.unbanUser(options)
889 it('Should return 404 with a non existing id', async function () {
890 const options = { userId: 4545454, expectedStatus: HttpStatusCode.NOT_FOUND_404 }
892 await server.users.remove(options)
893 await server.users.banUser(options)
894 await server.users.unbanUser(options)
897 it('Should fail with a non admin user', async function () {
898 const options = { userId, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
900 await server.users.remove(options)
901 await server.users.banUser(options)
902 await server.users.unbanUser(options)
905 it('Should fail on a moderator with a moderator', async function () {
906 const options = { userId: moderatorId, token: moderatorToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
908 await server.users.remove(options)
909 await server.users.banUser(options)
910 await server.users.unbanUser(options)
913 it('Should succeed on a user with a moderator', async function () {
914 const options = { userId, token: moderatorToken }
916 await server.users.banUser(options)
917 await server.users.unbanUser(options)
921 describe('When deleting our account', function () {
922 it('Should fail with with the root account', async function () {
923 await server.users.deleteMe({ expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
927 describe('When registering a new user', function () {
928 const registrationPath = path + '/register'
929 const baseCorrectParams = {
931 displayName: 'super user',
932 email: 'test3@example.com',
933 password: 'my super password'
936 it('Should fail with a too small username', async function () {
937 const fields = { ...baseCorrectParams, username: '' }
939 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
942 it('Should fail with a too long username', async function () {
943 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
945 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
948 it('Should fail with an incorrect username', async function () {
949 const fields = { ...baseCorrectParams, username: 'my username' }
951 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
954 it('Should fail with a missing email', async function () {
955 const fields = omit(baseCorrectParams, 'email')
957 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
960 it('Should fail with an invalid email', async function () {
961 const fields = { ...baseCorrectParams, email: 'test_example.com' }
963 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
966 it('Should fail with a too small password', async function () {
967 const fields = { ...baseCorrectParams, password: 'bla' }
969 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
972 it('Should fail with a too long password', async function () {
973 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
975 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
978 it('Should fail if we register a user with the same username', async function () {
979 const fields = { ...baseCorrectParams, username: 'root' }
981 await makePostBodyRequest({
983 path: registrationPath,
984 token: server.accessToken,
986 expectedStatus: HttpStatusCode.CONFLICT_409
990 it('Should fail with a "peertube" username', async function () {
991 const fields = { ...baseCorrectParams, username: 'peertube' }
993 await makePostBodyRequest({
995 path: registrationPath,
996 token: server.accessToken,
998 expectedStatus: HttpStatusCode.CONFLICT_409
1002 it('Should fail if we register a user with the same email', async function () {
1003 const fields = { ...baseCorrectParams, email: 'admin' + server.internalServerNumber + '@example.com' }
1005 await makePostBodyRequest({
1007 path: registrationPath,
1008 token: server.accessToken,
1010 expectedStatus: HttpStatusCode.CONFLICT_409
1014 it('Should fail with a bad display name', async function () {
1015 const fields = { ...baseCorrectParams, displayName: 'a'.repeat(150) }
1017 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1020 it('Should fail with a bad channel name', async function () {
1021 const fields = { ...baseCorrectParams, channel: { name: '[]azf', displayName: 'toto' } }
1023 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1026 it('Should fail with a bad channel display name', async function () {
1027 const fields = { ...baseCorrectParams, channel: { name: 'toto', displayName: '' } }
1029 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1032 it('Should fail with a channel name that is the same as username', async function () {
1033 const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
1034 const fields = { ...baseCorrectParams, ...source }
1036 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1039 it('Should fail with an existing channel', async function () {
1040 const attributes = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
1041 await server.channels.create({ attributes })
1043 const fields = { ...baseCorrectParams, channel: { name: 'existing_channel', displayName: 'toto' } }
1045 await makePostBodyRequest({
1047 path: registrationPath,
1048 token: server.accessToken,
1050 expectedStatus: HttpStatusCode.CONFLICT_409
1054 it('Should succeed with the correct params', async function () {
1055 const fields = { ...baseCorrectParams, channel: { name: 'super_channel', displayName: 'toto' } }
1057 await makePostBodyRequest({
1059 path: registrationPath,
1060 token: server.accessToken,
1062 expectedStatus: HttpStatusCode.NO_CONTENT_204
1066 it('Should fail on a server with registration disabled', async function () {
1069 email: 'test4@example.com',
1070 password: 'my super password 4'
1073 await makePostBodyRequest({
1074 url: serverWithRegistrationDisabled.url,
1075 path: registrationPath,
1076 token: serverWithRegistrationDisabled.accessToken,
1078 expectedStatus: HttpStatusCode.FORBIDDEN_403
1083 describe('When registering multiple users on a server with users limit', function () {
1084 it('Should fail when after 3 registrations', async function () {
1085 await server.users.register({ username: 'user42', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
1089 describe('When asking a password reset', function () {
1090 const path = '/api/v1/users/ask-reset-password'
1092 it('Should fail with a missing email', async function () {
1095 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
1098 it('Should fail with an invalid email', async function () {
1099 const fields = { email: 'hello' }
1101 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
1104 it('Should success with the correct params', async function () {
1105 const fields = { email: 'admin@example.com' }
1107 await makePostBodyRequest({
1110 token: server.accessToken,
1112 expectedStatus: HttpStatusCode.NO_CONTENT_204
1117 describe('When asking for an account verification email', function () {
1118 const path = '/api/v1/users/ask-send-verify-email'
1120 it('Should fail with a missing email', async function () {
1123 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
1126 it('Should fail with an invalid email', async function () {
1127 const fields = { email: 'hello' }
1129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
1132 it('Should succeed with the correct params', async function () {
1133 const fields = { email: 'admin@example.com' }
1135 await makePostBodyRequest({
1138 token: server.accessToken,
1140 expectedStatus: HttpStatusCode.NO_CONTENT_204
1145 after(async function () {
1146 MockSmtpServer.Instance.kill()
1148 await cleanupTests([ server, serverWithRegistrationDisabled ])