diff --git a/src/controllers/auth/login.ts b/src/controllers/auth/login.ts index 630f7b81..163e5d48 100644 --- a/src/controllers/auth/login.ts +++ b/src/controllers/auth/login.ts @@ -7,7 +7,7 @@ import { validateBody } from '../../middlewares/validation'; import { filterUser } from '../../utils/filters'; import { forbidden, success, unauthenticated } from '../../utils/responses'; import { generateToken } from '../../utils/users'; -import { Error } from '../../types'; +import { Error as ResponseError } from '../../types'; import { fetchUser } from '../../operations/user'; import * as validators from '../../utils/validators'; @@ -16,7 +16,7 @@ export default [ ...isNotAuthenticated, validateBody( Joi.object({ - login: Joi.string().required(), + login: Joi.string().required().error(new Error(ResponseError.EmptyLogin)), password: validators.password.required(), }), ), @@ -33,22 +33,22 @@ export default [ } else if (!validators.username.validate(login).error) { field = 'username'; } else { - return unauthenticated(response, Error.InvalidCredentials); + return unauthenticated(response, ResponseError.InvalidCredentials); } const user = await fetchUser(login, field); // Checks if the user exists if (!user) { - return unauthenticated(response, Error.InvalidCredentials); + return unauthenticated(response, ResponseError.InvalidCredentials); } if (user.registerToken) { - return forbidden(response, Error.EmailNotConfirmed); + return forbidden(response, ResponseError.EmailNotConfirmed); } if (user.type === UserType.attendant) { - return forbidden(response, Error.LoginAsAttendant); + return forbidden(response, ResponseError.LoginAsAttendant); } // Compares the hash from the password given @@ -56,7 +56,7 @@ export default [ // If the password is not valid, rejects the request if (!isPasswordValid) { - return unauthenticated(response, Error.InvalidCredentials); + return unauthenticated(response, ResponseError.InvalidCredentials); } const token = generateToken(user); diff --git a/src/types.ts b/src/types.ts index 2b8fc474..e7f7b87e 100755 --- a/src/types.ts +++ b/src/types.ts @@ -179,6 +179,7 @@ export const enum Error { NoQRCode = "Le QR code n'existe pas", InvalidCart = 'Le contenu de la commande est invalide', + EmptyLogin = "Le nom d'utilisateur ne peut pas ĂȘtre vide", // 401 // The user credentials were refused or not provided diff --git a/tests/auth/login.test.ts b/tests/auth/login.test.ts index 462a9d75..9e1a9331 100644 --- a/tests/auth/login.test.ts +++ b/tests/auth/login.test.ts @@ -75,6 +75,15 @@ describe('POST /auth/login', () => { .expect(401, { error: Error.InvalidCredentials }); }); + it('should return an error as the login is empty', async () => { + await request(app) + .post('/auth/login') + .send({ + password: user.password, + }) + .expect(400, { error: Error.EmptyLogin }); + }); + // This case should never happen it('should error because the user is an attendant', async () => { const visitorEmail = 'bonjour@lol.fr';