1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
11 setAccessTokensToServers
12 } from '@shared/extra-utils'
13 import { HttpStatusCode, UserRole } from '@shared/models'
15 describe('Test users API validators', function () {
16 const path = '/api/v1/users/'
17 let server: PeerTubeServer
18 let serverWithRegistrationDisabled: PeerTubeServer
20 // ---------------------------------------------------------------
22 before(async function () {
25 const res = await Promise.all([
26 createSingleServer(1, { signup: { limit: 3 } }),
31 serverWithRegistrationDisabled = res[1]
33 await setAccessTokensToServers([ server ])
35 await server.users.generate('moderator2', UserRole.MODERATOR)
38 describe('When registering a new user', function () {
39 const registrationPath = path + '/register'
40 const baseCorrectParams = {
42 displayName: 'super user',
43 email: 'test3@example.com',
44 password: 'my super password'
47 it('Should fail with a too small username', async function () {
48 const fields = { ...baseCorrectParams, username: '' }
50 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
53 it('Should fail with a too long username', async function () {
54 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
56 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
59 it('Should fail with an incorrect username', async function () {
60 const fields = { ...baseCorrectParams, username: 'my username' }
62 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
65 it('Should fail with a missing email', async function () {
66 const fields = omit(baseCorrectParams, 'email')
68 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
71 it('Should fail with an invalid email', async function () {
72 const fields = { ...baseCorrectParams, email: 'test_example.com' }
74 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
77 it('Should fail with a too small password', async function () {
78 const fields = { ...baseCorrectParams, password: 'bla' }
80 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
83 it('Should fail with a too long password', async function () {
84 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
86 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
89 it('Should fail if we register a user with the same username', async function () {
90 const fields = { ...baseCorrectParams, username: 'root' }
92 await makePostBodyRequest({
94 path: registrationPath,
95 token: server.accessToken,
97 expectedStatus: HttpStatusCode.CONFLICT_409
101 it('Should fail with a "peertube" username', async function () {
102 const fields = { ...baseCorrectParams, username: 'peertube' }
104 await makePostBodyRequest({
106 path: registrationPath,
107 token: server.accessToken,
109 expectedStatus: HttpStatusCode.CONFLICT_409
113 it('Should fail if we register a user with the same email', async function () {
114 const fields = { ...baseCorrectParams, email: 'admin' + server.internalServerNumber + '@example.com' }
116 await makePostBodyRequest({
118 path: registrationPath,
119 token: server.accessToken,
121 expectedStatus: HttpStatusCode.CONFLICT_409
125 it('Should fail with a bad display name', async function () {
126 const fields = { ...baseCorrectParams, displayName: 'a'.repeat(150) }
128 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
131 it('Should fail with a bad channel name', async function () {
132 const fields = { ...baseCorrectParams, channel: { name: '[]azf', displayName: 'toto' } }
134 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
137 it('Should fail with a bad channel display name', async function () {
138 const fields = { ...baseCorrectParams, channel: { name: 'toto', displayName: '' } }
140 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
143 it('Should fail with a channel name that is the same as username', async function () {
144 const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
145 const fields = { ...baseCorrectParams, ...source }
147 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
150 it('Should fail with an existing channel', async function () {
151 const attributes = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
152 await server.channels.create({ attributes })
154 const fields = { ...baseCorrectParams, channel: { name: 'existing_channel', displayName: 'toto' } }
156 await makePostBodyRequest({
158 path: registrationPath,
159 token: server.accessToken,
161 expectedStatus: HttpStatusCode.CONFLICT_409
165 it('Should succeed with the correct params', async function () {
166 const fields = { ...baseCorrectParams, channel: { name: 'super_channel', displayName: 'toto' } }
168 await makePostBodyRequest({
170 path: registrationPath,
171 token: server.accessToken,
173 expectedStatus: HttpStatusCode.NO_CONTENT_204
177 it('Should fail on a server with registration disabled', async function () {
180 email: 'test4@example.com',
181 password: 'my super password 4'
184 await makePostBodyRequest({
185 url: serverWithRegistrationDisabled.url,
186 path: registrationPath,
187 token: serverWithRegistrationDisabled.accessToken,
189 expectedStatus: HttpStatusCode.FORBIDDEN_403
194 describe('When registering multiple users on a server with users limit', function () {
196 it('Should fail when after 3 registrations', async function () {
197 await server.users.register({ username: 'user42', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
202 describe('When asking a password reset', function () {
203 const path = '/api/v1/users/ask-reset-password'
205 it('Should fail with a missing email', async function () {
208 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
211 it('Should fail with an invalid email', async function () {
212 const fields = { email: 'hello' }
214 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
217 it('Should success with the correct params', async function () {
218 const fields = { email: 'admin@example.com' }
220 await makePostBodyRequest({
223 token: server.accessToken,
225 expectedStatus: HttpStatusCode.NO_CONTENT_204
230 describe('When asking for an account verification email', function () {
231 const path = '/api/v1/users/ask-send-verify-email'
233 it('Should fail with a missing email', async function () {
236 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
239 it('Should fail with an invalid email', async function () {
240 const fields = { email: 'hello' }
242 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
245 it('Should succeed with the correct params', async function () {
246 const fields = { email: 'admin@example.com' }
248 await makePostBodyRequest({
251 token: server.accessToken,
253 expectedStatus: HttpStatusCode.NO_CONTENT_204
258 after(async function () {
259 MockSmtpServer.Instance.kill()
261 await cleanupTests([ server, serverWithRegistrationDisabled ])