PeerTube/server/tests/api/check-params/users.ts

258 lines
8.7 KiB
TypeScript
Raw Normal View History

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