2020-01-31 09:56:52 -06:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
import 'mocha'
|
2020-07-30 02:43:12 -05:00
|
|
|
import { omit } from 'lodash'
|
2021-12-17 04:58:15 -06:00
|
|
|
import { MockSmtpServer } from '@server/tests/shared'
|
2021-10-19 08:02:43 -05:00
|
|
|
import { HttpStatusCode, UserRole } from '@shared/models'
|
2021-12-17 04:58:15 -06:00
|
|
|
import { cleanupTests, createSingleServer, makePostBodyRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
describe('Test users API validators', function () {
|
|
|
|
const path = '/api/v1/users/'
|
2021-07-16 02:47:51 -05:00
|
|
|
let server: PeerTubeServer
|
|
|
|
let serverWithRegistrationDisabled: PeerTubeServer
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
before(async function () {
|
2018-01-18 11:10:45 -06:00
|
|
|
this.timeout(30000)
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2021-10-19 08:02:43 -05:00
|
|
|
const res = await Promise.all([
|
|
|
|
createSingleServer(1, { signup: { limit: 3 } }),
|
|
|
|
createSingleServer(2)
|
|
|
|
])
|
2020-02-17 03:16:52 -06:00
|
|
|
|
2021-10-19 08:02:43 -05:00
|
|
|
server = res[0]
|
|
|
|
serverWithRegistrationDisabled = res[1]
|
2020-02-17 03:16:52 -06:00
|
|
|
|
2021-10-19 08:02:43 -05:00
|
|
|
await setAccessTokensToServers([ server ])
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2021-10-19 08:02:43 -05:00
|
|
|
await server.users.generate('moderator2', UserRole.MODERATOR)
|
2018-08-08 03:55:27 -05:00
|
|
|
})
|
|
|
|
|
2019-05-28 03:46:32 -05:00
|
|
|
describe('When registering a new user', function () {
|
2017-09-04 14:21:47 -05:00
|
|
|
const registrationPath = path + '/register'
|
2017-12-28 08:25:31 -06:00
|
|
|
const baseCorrectParams = {
|
|
|
|
username: 'user3',
|
2019-06-07 09:59:53 -05:00
|
|
|
displayName: 'super user',
|
2017-12-28 08:25:31 -06:00
|
|
|
email: 'test3@example.com',
|
|
|
|
password: 'my super password'
|
|
|
|
}
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
it('Should fail with a too small username', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, username: '' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with a too long username', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with an incorrect username', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, username: 'my username' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with a missing email', async function () {
|
2017-12-28 08:25:31 -06:00
|
|
|
const fields = omit(baseCorrectParams, 'email')
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with an invalid email', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, email: 'test_example.com' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with a too small password', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, password: 'bla' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with a too long password', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail if we register a user with the same username', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, username: 'root' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2017-12-28 08:25:31 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.CONFLICT_409
|
2017-12-28 08:25:31 -06:00
|
|
|
})
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
|
2018-06-21 04:54:22 -05:00
|
|
|
it('Should fail with a "peertube" username', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, username: 'peertube' }
|
2018-06-21 04:54:22 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.CONFLICT_409
|
2018-06-21 04:54:22 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
it('Should fail if we register a user with the same email', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, email: 'admin' + server.internalServerNumber + '@example.com' }
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2017-12-28 08:25:31 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.CONFLICT_409
|
2017-12-28 08:25:31 -06:00
|
|
|
})
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
|
2019-06-07 09:59:53 -05:00
|
|
|
it('Should fail with a bad display name', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, displayName: 'a'.repeat(150) }
|
2019-06-07 09:59:53 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
2019-05-28 03:46:32 -05:00
|
|
|
it('Should fail with a bad channel name', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, channel: { name: '[]azf', displayName: 'toto' } }
|
2019-05-28 03:46:32 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with a bad channel display name', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, channel: { name: 'toto', displayName: '' } }
|
2019-05-28 03:46:32 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
2019-09-23 01:17:42 -05:00
|
|
|
it('Should fail with a channel name that is the same as username', async function () {
|
2019-05-29 04:03:01 -05:00
|
|
|
const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, ...source }
|
2019-05-29 04:03:01 -05:00
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
2019-05-28 03:46:32 -05:00
|
|
|
it('Should fail with an existing channel', async function () {
|
2021-07-09 04:21:30 -05:00
|
|
|
const attributes = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.channels.create({ attributes })
|
2019-05-28 03:46:32 -05:00
|
|
|
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, channel: { name: 'existing_channel', displayName: 'toto' } }
|
2019-05-28 03:46:32 -05:00
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.CONFLICT_409
|
2020-12-07 07:32:36 -06:00
|
|
|
})
|
2019-05-28 03:46:32 -05:00
|
|
|
})
|
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
it('Should succeed with the correct params', async function () {
|
2021-07-13 02:43:59 -05:00
|
|
|
const fields = { ...baseCorrectParams, channel: { name: 'super_channel', displayName: 'toto' } }
|
2019-05-28 03:46:32 -05:00
|
|
|
|
2017-12-28 08:25:31 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: server.accessToken,
|
2022-07-13 04:58:01 -05:00
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.NO_CONTENT_204
|
2017-12-28 08:25:31 -06:00
|
|
|
})
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail on a server with registration disabled', async function () {
|
|
|
|
const fields = {
|
|
|
|
username: 'user4',
|
|
|
|
email: 'test4@example.com',
|
|
|
|
password: 'my super password 4'
|
|
|
|
}
|
|
|
|
|
|
|
|
await makePostBodyRequest({
|
|
|
|
url: serverWithRegistrationDisabled.url,
|
|
|
|
path: registrationPath,
|
|
|
|
token: serverWithRegistrationDisabled.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.FORBIDDEN_403
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('When registering multiple users on a server with users limit', function () {
|
2021-10-19 08:02:43 -05:00
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
it('Should fail when after 3 registrations', async function () {
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.users.register({ username: 'user42', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
2021-10-19 08:02:43 -05:00
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
|
2018-01-30 08:16:24 -06:00
|
|
|
describe('When asking a password reset', function () {
|
|
|
|
const path = '/api/v1/users/ask-reset-password'
|
|
|
|
|
|
|
|
it('Should fail with a missing email', async function () {
|
|
|
|
const fields = {}
|
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with an invalid email', async function () {
|
|
|
|
const fields = { email: 'hello' }
|
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should success with the correct params', async function () {
|
|
|
|
const fields = { email: 'admin@example.com' }
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.NO_CONTENT_204
|
2020-12-07 07:32:36 -06:00
|
|
|
})
|
2018-01-30 08:16:24 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-31 02:18:19 -05:00
|
|
|
describe('When asking for an account verification email', function () {
|
|
|
|
const path = '/api/v1/users/ask-send-verify-email'
|
|
|
|
|
|
|
|
it('Should fail with a missing email', async function () {
|
|
|
|
const fields = {}
|
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should fail with an invalid email', async function () {
|
|
|
|
const fields = { email: 'hello' }
|
|
|
|
|
|
|
|
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should succeed with the correct params', async function () {
|
|
|
|
const fields = { email: 'admin@example.com' }
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
await makePostBodyRequest({
|
|
|
|
url: server.url,
|
|
|
|
path,
|
|
|
|
token: server.accessToken,
|
|
|
|
fields,
|
2021-07-16 03:42:24 -05:00
|
|
|
expectedStatus: HttpStatusCode.NO_CONTENT_204
|
2020-12-07 07:32:36 -06:00
|
|
|
})
|
2018-08-31 02:18:19 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-04-24 08:10:37 -05:00
|
|
|
after(async function () {
|
2020-02-17 03:16:52 -06:00
|
|
|
MockSmtpServer.Instance.kill()
|
|
|
|
|
2019-04-24 08:10:37 -05:00
|
|
|
await cleanupTests([ server, serverWithRegistrationDisabled ])
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
})
|