1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, MockSmtpServer } from '@server/tests/shared'
4 import { omit } from '@shared/core-utils'
5 import { HttpStatusCode, UserAdminFlag, UserRole } from '@shared/models'
15 setAccessTokensToServers
16 } from '@shared/server-commands'
18 describe('Test users admin API validators', function () {
19 const path = '/api/v1/users/'
22 let moderatorId: number
23 let server: PeerTubeServer
25 let moderatorToken = ''
28 // ---------------------------------------------------------------
30 before(async function () {
33 const emails: object[] = []
34 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
37 server = await createSingleServer(1)
39 await setAccessTokensToServers([ server ])
43 const result = await server.users.generate('user1')
44 userToken = result.token
45 userId = result.userId
49 const result = await server.users.generate('moderator1', UserRole.MODERATOR)
50 moderatorToken = result.token
54 const result = await server.users.generate('moderator2', UserRole.MODERATOR)
55 moderatorId = result.userId
59 describe('When listing users', function () {
60 it('Should fail with a bad start pagination', async function () {
61 await checkBadStartPagination(server.url, path, server.accessToken)
64 it('Should fail with a bad count pagination', async function () {
65 await checkBadCountPagination(server.url, path, server.accessToken)
68 it('Should fail with an incorrect sort', async function () {
69 await checkBadSortPagination(server.url, path, server.accessToken)
72 it('Should fail with a non authenticated user', async function () {
73 await makeGetRequest({
76 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
80 it('Should fail with a non admin user', async function () {
81 await makeGetRequest({
85 expectedStatus: HttpStatusCode.FORBIDDEN_403
90 describe('When adding a new user', function () {
91 const baseCorrectParams = {
93 email: 'test@example.com',
94 password: 'my super password',
98 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
101 it('Should fail with a too small username', async function () {
102 const fields = { ...baseCorrectParams, username: '' }
104 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
107 it('Should fail with a too long username', async function () {
108 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
110 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
113 it('Should fail with a not lowercase username', async function () {
114 const fields = { ...baseCorrectParams, username: 'Toto' }
116 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
119 it('Should fail with an incorrect username', async function () {
120 const fields = { ...baseCorrectParams, username: 'my username' }
122 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
125 it('Should fail with a missing email', async function () {
126 const fields = omit(baseCorrectParams, [ 'email' ])
128 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
131 it('Should fail with an invalid email', async function () {
132 const fields = { ...baseCorrectParams, email: 'test_example.com' }
134 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
137 it('Should fail with a too small password', async function () {
138 const fields = { ...baseCorrectParams, password: 'bla' }
140 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
143 it('Should fail with a too long password', async function () {
144 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
146 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
149 it('Should fail with empty password and no smtp configured', async function () {
150 const fields = { ...baseCorrectParams, password: '' }
152 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
155 it('Should succeed with no password on a server with smtp enabled', async function () {
158 await killallServers([ server ])
160 await server.run(ConfigCommand.getEmailOverrideConfig(emailPort))
163 ...baseCorrectParams,
166 username: 'create_password',
167 email: 'create_password@example.com'
170 await makePostBodyRequest({
173 token: server.accessToken,
175 expectedStatus: HttpStatusCode.OK_200
179 it('Should fail with invalid admin flags', async function () {
180 const fields = { ...baseCorrectParams, adminFlags: 'toto' }
182 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
185 it('Should fail with an non authenticated user', async function () {
186 await makePostBodyRequest({
189 token: 'super token',
190 fields: baseCorrectParams,
191 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
195 it('Should fail if we add a user with the same username', async function () {
196 const fields = { ...baseCorrectParams, username: 'user1' }
198 await makePostBodyRequest({
201 token: server.accessToken,
203 expectedStatus: HttpStatusCode.CONFLICT_409
207 it('Should fail if we add a user with the same email', async function () {
208 const fields = { ...baseCorrectParams, email: 'user1@example.com' }
210 await makePostBodyRequest({
213 token: server.accessToken,
215 expectedStatus: HttpStatusCode.CONFLICT_409
219 it('Should fail with an invalid videoQuota', async function () {
220 const fields = { ...baseCorrectParams, videoQuota: -5 }
222 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
225 it('Should fail with an invalid videoQuotaDaily', async function () {
226 const fields = { ...baseCorrectParams, videoQuotaDaily: -7 }
228 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
231 it('Should fail without a user role', async function () {
232 const fields = omit(baseCorrectParams, [ 'role' ])
234 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
237 it('Should fail with an invalid user role', async function () {
238 const fields = { ...baseCorrectParams, role: 88989 }
240 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
243 it('Should fail with a "peertube" username', async function () {
244 const fields = { ...baseCorrectParams, username: 'peertube' }
246 await makePostBodyRequest({
249 token: server.accessToken,
251 expectedStatus: HttpStatusCode.CONFLICT_409
255 it('Should fail to create a moderator or an admin with a moderator', async function () {
256 for (const role of [ UserRole.MODERATOR, UserRole.ADMINISTRATOR ]) {
257 const fields = { ...baseCorrectParams, role }
259 await makePostBodyRequest({
262 token: moderatorToken,
264 expectedStatus: HttpStatusCode.FORBIDDEN_403
269 it('Should succeed to create a user with a moderator', async function () {
270 const fields = { ...baseCorrectParams, username: 'a4656', email: 'a4656@example.com', role: UserRole.USER }
272 await makePostBodyRequest({
275 token: moderatorToken,
277 expectedStatus: HttpStatusCode.OK_200
281 it('Should succeed with the correct params', async function () {
282 await makePostBodyRequest({
285 token: server.accessToken,
286 fields: baseCorrectParams,
287 expectedStatus: HttpStatusCode.OK_200
291 it('Should fail with a non admin user', async function () {
292 const user = { username: 'user1' }
293 userToken = await server.login.getAccessToken(user)
297 email: 'test@example.com',
298 password: 'my super password',
301 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
305 describe('When getting a user', function () {
307 it('Should fail with an non authenticated user', async function () {
308 await makeGetRequest({
311 token: 'super token',
312 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
316 it('Should fail with a non admin user', async function () {
317 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
320 it('Should succeed with the correct params', async function () {
321 await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
325 describe('When updating a user', function () {
327 it('Should fail with an invalid email attribute', async function () {
332 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
335 it('Should fail with an invalid emailVerified attribute', async function () {
340 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
343 it('Should fail with an invalid videoQuota attribute', async function () {
348 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
351 it('Should fail with an invalid user role attribute', async function () {
356 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
359 it('Should fail with a too small password', async function () {
361 currentPassword: 'password',
365 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
368 it('Should fail with a too long password', async function () {
370 currentPassword: 'password',
371 password: 'super'.repeat(61)
374 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
377 it('Should fail with an non authenticated user', async function () {
382 await makePutBodyRequest({
385 token: 'super token',
387 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
391 it('Should fail when updating root role', async function () {
393 role: UserRole.MODERATOR
396 await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields })
399 it('Should fail with invalid admin flags', async function () {
400 const fields = { adminFlags: 'toto' }
402 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
405 it('Should fail to update an admin with a moderator', async function () {
410 await makePutBodyRequest({
412 path: path + moderatorId,
413 token: moderatorToken,
415 expectedStatus: HttpStatusCode.FORBIDDEN_403
419 it('Should succeed to update a user with a moderator', async function () {
424 await makePutBodyRequest({
427 token: moderatorToken,
429 expectedStatus: HttpStatusCode.NO_CONTENT_204
433 it('Should succeed with the correct params', async function () {
435 email: 'email@example.com',
441 await makePutBodyRequest({
444 token: server.accessToken,
446 expectedStatus: HttpStatusCode.NO_CONTENT_204
451 after(async function () {
452 MockSmtpServer.Instance.kill()
454 await cleanupTests([ server ])