From 77a5501f6413aff2f2a626b929dfda486fa9a3e6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 6 Sep 2017 16:35:40 +0200 Subject: Fix tests and user quota --- server/middlewares/validators/users.ts | 61 ++++++++++++++++++++------------- server/middlewares/validators/videos.ts | 9 +++++ 2 files changed, 47 insertions(+), 23 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index ebb343535..aec6324bf 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -6,7 +6,7 @@ import * as validator from 'validator' import { database as db } from '../../initializers/database' import { checkErrors } from './utils' import { isSignupAllowed, logger } from '../../helpers' -import { VideoInstance } from '../../models' +import { UserInstance, VideoInstance } from '../../models' function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('username', 'Should have a valid username').isUserUsernameValid() @@ -17,16 +17,19 @@ function usersAddValidator (req: express.Request, res: express.Response, next: e logger.debug('Checking usersAdd parameters', { parameters: req.body }) checkErrors(req, res, () => { - db.User.loadByUsernameOrEmail(req.body.username, req.body.email) - .then(user => { - if (user) return res.status(409).send('User already exists.') + checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next) + }) +} - next() - }) - .catch(err => { - logger.error('Error in usersAdd request validator.', err) - return res.sendStatus(500) - }) +function usersRegisterValidator (req: express.Request, res: express.Response, next: express.NextFunction) { + req.checkBody('username', 'Should have a valid username').isUserUsernameValid() + req.checkBody('password', 'Should have a valid password').isUserPasswordValid() + req.checkBody('email', 'Should have a valid email').isEmail() + + logger.debug('Checking usersRegister parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next) }) } @@ -36,18 +39,16 @@ function usersRemoveValidator (req: express.Request, res: express.Response, next logger.debug('Checking usersRemove parameters', { parameters: req.params }) checkErrors(req, res, () => { - db.User.loadById(req.params.id) - .then(user => { - if (!user) return res.status(404).send('User not found') + checkUserExists(req.params.id, res, (err, user) => { + if (err) { + logger.error('Error in usersRemoveValidator.', err) + return res.sendStatus(500) + } - if (user.username === 'root') return res.status(400).send('Cannot remove the root user') + if (user.username === 'root') return res.status(400).send('Cannot remove the root user') - next() - }) - .catch(err => { - logger.error('Error in usersRemove request validator.', err) - return res.sendStatus(500) - }) + next() + }) }) } @@ -69,7 +70,7 @@ function usersUpdateMeValidator (req: express.Request, res: express.Response, ne req.checkBody('email', 'Should have a valid email attribute').optional().isEmail() req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() - logger.debug('Checking usersUpdate parameters', { parameters: req.body }) + logger.debug('Checking usersUpdateMe parameters', { parameters: req.body }) checkErrors(req, res, next) } @@ -123,6 +124,7 @@ function ensureUserRegistrationAllowed (req: express.Request, res: express.Respo export { usersAddValidator, + usersRegisterValidator, usersRemoveValidator, usersUpdateValidator, usersUpdateMeValidator, @@ -133,16 +135,29 @@ export { // --------------------------------------------------------------------------- -function checkUserExists (id: number, res: express.Response, callback: () => void) { +function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) { db.User.loadById(id) .then(user => { if (!user) return res.status(404).send('User not found') res.locals.user = user - callback() + callback(null, user) }) .catch(err => { logger.error('Error in user request validator.', err) return res.sendStatus(500) }) } + +function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) { + db.User.loadByUsernameOrEmail(username, email) + .then(user => { + if (user) return res.status(409).send('User already exists.') + + callback() + }) + .catch(err => { + logger.error('Error in usersAdd request validator.', err) + return res.sendStatus(500) + }) +} diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index ba8c2d834..249da668d 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -36,6 +36,12 @@ function videosAddValidator (req: express.Request, res: express.Response, next: } return db.Video.getDurationFromFile(videoFile.path) + .catch(err => { + logger.error('Invalid input file in videosAddValidator.', err) + res.status(400).send('Invalid input file.') + + return undefined + }) }) .then(duration => { // Previous test failed, abort @@ -51,7 +57,10 @@ function videosAddValidator (req: express.Request, res: express.Response, next: .catch(err => { logger.error('Error in video add validator', err) res.sendStatus(500) + + return undefined }) + }) } -- cgit v1.2.3