import { Validators } from '@angular/forms'
export const USER_USERNAME = {
- VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(20) ],
+ VALIDATORS: [
+ Validators.required,
+ Validators.minLength(3),
+ Validators.maxLength(20),
+ Validators.pattern(/^[a-z0-9._]+$/)
+ ],
MESSAGES: {
'required': 'Username is required.',
'minlength': 'Username must be at least 3 characters long.',
- 'maxlength': 'Username cannot be more than 20 characters long.'
+ 'maxlength': 'Username cannot be more than 20 characters long.',
+ 'pattern': 'Username should be only lowercase alphanumeric characters.'
}
}
export const USER_EMAIL = {
function isUserUsernameValid (value: string) {
const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
- return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
+ return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
}
function isUserDisplayNSFWValid (value: any) {
import { UserInstance, VideoInstance } from '../../models'
const usersAddValidator = [
- body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
+ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'),
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
.then(user => {
if (user) {
return res.status(409)
- .send({ error: 'User already exists.' })
+ .send({ error: 'User with this username of email already exists.' })
.end()
}
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
})
+ it('Should fail with a not lowercase username', async function () {
+ const fields = {
+ username: 'Toto',
+ email: 'test@example.com',
+ password: 'my_super_password',
+ videoQuota: 42000000,
+ role: UserRole.USER
+ }
+
+ await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
+ })
+
it('Should fail with an incorrect username', async function () {
const fields = {
username: 'my username',