1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
5 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, MockSmtpServer } from '@server/tests/shared'
6 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 ])
162 hostname: 'localhost',
166 await server.run(config)
169 ...baseCorrectParams,
172 username: 'create_password',
173 email: 'create_password@example.com'
176 await makePostBodyRequest({
179 token: server.accessToken,
181 expectedStatus: HttpStatusCode.OK_200
185 it('Should fail with invalid admin flags', async function () {
186 const fields = { ...baseCorrectParams, adminFlags: 'toto' }
188 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
191 it('Should fail with an non authenticated user', async function () {
192 await makePostBodyRequest({
195 token: 'super token',
196 fields: baseCorrectParams,
197 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
201 it('Should fail if we add a user with the same username', async function () {
202 const fields = { ...baseCorrectParams, username: 'user1' }
204 await makePostBodyRequest({
207 token: server.accessToken,
209 expectedStatus: HttpStatusCode.CONFLICT_409
213 it('Should fail if we add a user with the same email', async function () {
214 const fields = { ...baseCorrectParams, email: 'user1@example.com' }
216 await makePostBodyRequest({
219 token: server.accessToken,
221 expectedStatus: HttpStatusCode.CONFLICT_409
225 it('Should fail without a videoQuota', async function () {
226 const fields = omit(baseCorrectParams, 'videoQuota')
228 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
231 it('Should fail without a videoQuotaDaily', async function () {
232 const fields = omit(baseCorrectParams, 'videoQuotaDaily')
234 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
237 it('Should fail with an invalid videoQuota', async function () {
238 const fields = { ...baseCorrectParams, videoQuota: -5 }
240 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
243 it('Should fail with an invalid videoQuotaDaily', async function () {
244 const fields = { ...baseCorrectParams, videoQuotaDaily: -7 }
246 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
249 it('Should fail without a user role', async function () {
250 const fields = omit(baseCorrectParams, 'role')
252 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
255 it('Should fail with an invalid user role', async function () {
256 const fields = { ...baseCorrectParams, role: 88989 }
258 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
261 it('Should fail with a "peertube" username', async function () {
262 const fields = { ...baseCorrectParams, username: 'peertube' }
264 await makePostBodyRequest({
267 token: server.accessToken,
269 expectedStatus: HttpStatusCode.CONFLICT_409
273 it('Should fail to create a moderator or an admin with a moderator', async function () {
274 for (const role of [ UserRole.MODERATOR, UserRole.ADMINISTRATOR ]) {
275 const fields = { ...baseCorrectParams, role }
277 await makePostBodyRequest({
280 token: moderatorToken,
282 expectedStatus: HttpStatusCode.FORBIDDEN_403
287 it('Should succeed to create a user with a moderator', async function () {
288 const fields = { ...baseCorrectParams, username: 'a4656', email: 'a4656@example.com', role: UserRole.USER }
290 await makePostBodyRequest({
293 token: moderatorToken,
295 expectedStatus: HttpStatusCode.OK_200
299 it('Should succeed with the correct params', async function () {
300 await makePostBodyRequest({
303 token: server.accessToken,
304 fields: baseCorrectParams,
305 expectedStatus: HttpStatusCode.OK_200
309 it('Should fail with a non admin user', async function () {
310 const user = { username: 'user1' }
311 userToken = await server.login.getAccessToken(user)
315 email: 'test@example.com',
316 password: 'my super password',
319 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
323 describe('When getting a user', function () {
325 it('Should fail with an non authenticated user', async function () {
326 await makeGetRequest({
329 token: 'super token',
330 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
334 it('Should fail with a non admin user', async function () {
335 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
338 it('Should succeed with the correct params', async function () {
339 await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
343 describe('When updating a user', function () {
345 it('Should fail with an invalid email attribute', async function () {
350 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
353 it('Should fail with an invalid emailVerified attribute', async function () {
358 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
361 it('Should fail with an invalid videoQuota attribute', async function () {
366 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
369 it('Should fail with an invalid user role attribute', async function () {
374 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
377 it('Should fail with a too small password', async function () {
379 currentPassword: 'password',
383 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
386 it('Should fail with a too long password', async function () {
388 currentPassword: 'password',
389 password: 'super'.repeat(61)
392 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
395 it('Should fail with an non authenticated user', async function () {
400 await makePutBodyRequest({
403 token: 'super token',
405 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
409 it('Should fail when updating root role', async function () {
411 role: UserRole.MODERATOR
414 await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields })
417 it('Should fail with invalid admin flags', async function () {
418 const fields = { adminFlags: 'toto' }
420 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
423 it('Should fail to update an admin with a moderator', async function () {
428 await makePutBodyRequest({
430 path: path + moderatorId,
431 token: moderatorToken,
433 expectedStatus: HttpStatusCode.FORBIDDEN_403
437 it('Should succeed to update a user with a moderator', async function () {
442 await makePutBodyRequest({
445 token: moderatorToken,
447 expectedStatus: HttpStatusCode.NO_CONTENT_204
451 it('Should succeed with the correct params', async function () {
453 email: 'email@example.com',
459 await makePutBodyRequest({
462 token: server.accessToken,
464 expectedStatus: HttpStatusCode.NO_CONTENT_204
469 after(async function () {
470 MockSmtpServer.Instance.kill()
472 await cleanupTests([ server ])