Usernames are case insensitive now

pull/126/head
Chocobozzz 2017-11-04 18:32:38 +01:00
parent f735448395
commit 563d032e97
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 23 additions and 5 deletions

View File

@ -1,11 +1,17 @@
import { Validators } from '@angular/forms' import { Validators } from '@angular/forms'
export const USER_USERNAME = { 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: { MESSAGES: {
'required': 'Username is required.', 'required': 'Username is required.',
'minlength': 'Username must be at least 3 characters long.', '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 = { export const USER_EMAIL = {

View File

@ -18,7 +18,7 @@ function isUserVideoQuotaValid (value: string) {
function isUserUsernameValid (value: string) { function isUserUsernameValid (value: string) {
const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min 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) { function isUserDisplayNSFWValid (value: any) {

View File

@ -19,7 +19,7 @@ import {
import { UserInstance, VideoInstance } from '../../models' import { UserInstance, VideoInstance } from '../../models'
const usersAddValidator = [ 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('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'), body('email').isEmail().withMessage('Should have a valid email'),
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
@ -196,7 +196,7 @@ function checkUserDoesNotAlreadyExist (username: string, email: string, res: exp
.then(user => { .then(user => {
if (user) { if (user) {
return res.status(409) return res.status(409)
.send({ error: 'User already exists.' }) .send({ error: 'User with this username of email already exists.' })
.end() .end()
} }

View File

@ -112,6 +112,18 @@ describe('Test users API validators', function () {
await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 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 () { it('Should fail with an incorrect username', async function () {
const fields = { const fields = {
username: 'my username', username: 'my username',