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