diff --git a/lib/helpers/errors.js b/lib/helpers/errors.js index e7ca4e476..ecfbf28e1 100644 --- a/lib/helpers/errors.js +++ b/lib/helpers/errors.js @@ -1,10 +1,3 @@ -/** - * Module dependencies - */ -const path = require('path'); - -const config = require(path.resolve('./config')); - /** * @desc Function to parse error message * @param {Object} err @@ -26,7 +19,7 @@ const getUniqueMessage = (err) => { } const fieldName = err.errmsg.substring(begin, err.errmsg.lastIndexOf('_1')); output = `${fieldName.charAt(0).toUpperCase() + fieldName.slice(1)} already exists`; - } catch (ex) { + } catch (err) { output = 'Unique field already exists'; } @@ -41,19 +34,9 @@ const getUniqueMessage = (err) => { const getMessageFromCode = (err) => { let output; switch (err.code) { - case 11000: case 11001: output = getUniqueMessage(err); break; - case 'UNSUPPORTED_MEDIA_TYPE': - output = 'Unsupported filetype.'; - break; - case 'LIMIT_FILE_SIZE': - output = `Image file too large. Maximum size allowed is ${(config.uploads.profile.avatar.limits.fileSize / (1024 * 1024)).toFixed(2)} Mb files.`; - break; - case 'LIMIT_UNEXPECTED_FILE': - output = 'Missing `newProfilePicture` field.'; - break; default: { if (err.message) output = err.message; else output = 'Something went wrong.'; @@ -63,19 +46,17 @@ const getMessageFromCode = (err) => { }; /** - * @desc Function to map an array of errors + * @desc Function to map an array/object of errors * @param {Object} err * @return {String} message */ const getMessageFromErrors = (err) => { - if (err instanceof Array) return err.errors; let output = ''; if (err.errors instanceof Array) { - err.errors.map((error) => { + err.errors.forEach((error) => { if (error.message) { output += `${error.message} `; } - return null; }); } else if (err.errors instanceof Object) { Object.keys(err.errors).forEach((key) => { @@ -83,8 +64,6 @@ const getMessageFromErrors = (err) => { output += `${err.errors[key].message} `; } }); - } else { - output = err.message; } return output; }; diff --git a/modules/core/tests/core.config.tests.js b/modules/core/tests/core.lib.tests.js similarity index 90% rename from modules/core/tests/core.config.tests.js rename to modules/core/tests/core.lib.tests.js index 3e14a4ac1..f544ee530 100644 --- a/modules/core/tests/core.config.tests.js +++ b/modules/core/tests/core.lib.tests.js @@ -10,6 +10,7 @@ const logger = require(path.resolve('./lib/services/logger')); const mongooseService = require(path.resolve('./lib/services/mongoose')); const multerService = require(path.resolve('./lib/services/multer')); const seed = require(path.resolve('./lib/services/seed')); +const errors = require(path.resolve('./lib/helpers/errors')); /** * Unit tests @@ -396,6 +397,36 @@ describe('Configuration Tests:', () => { }); }); + describe('Errors', () => { + test('should return errors message properly', async () => { + try { + const fromCode = errors.getMessage({ code: 11001, errmsg: 'test' }); + expect(fromCode).toBe('Test already exists.'); + + const fromCode2 = errors.getMessage({ code: 11001, errmsg: 'test.$.test' }); + expect(fromCode2).toBe('Test.$ already exists.'); + + const fromCodeUnknow = errors.getMessage({ code: 'unknow' }); + expect(fromCodeUnknow).toBe('Something went wrong.'); + + const fromErrorsArray = errors.getMessage({ errors: [{ message: 'error1' }, { message: 'error2' }] }); + expect(fromErrorsArray).toBe('error1 error2 .'); + + const fromErrorsObject = errors.getMessage({ errors: { one: { message: 'error1' }, two: { message: 'error2' } } }); + expect(fromErrorsObject).toBe('error1 error2 .'); + + const fromMessage = errors.getMessage({ message: 'error1' }); + expect(fromMessage).toBe('error1.'); + + const fromEmpty = errors.getMessage({ }); + expect(fromEmpty).toBe('error while retrieving the error :o : {}.'); + } catch (err) { + console.log(err); + expect(err).toBeFalsy(); + } + }); + }); + // Mongoose disconnect afterAll(() => mongooseService.disconnect() .catch((e) => { diff --git a/modules/uploads/controllers/uploads.controller.js b/modules/uploads/controllers/uploads.controller.js index be6502802..d3223b95b 100644 --- a/modules/uploads/controllers/uploads.controller.js +++ b/modules/uploads/controllers/uploads.controller.js @@ -129,7 +129,6 @@ exports.uploadByImageName = async (req, res, next, uploadImageName) => { req.sharpOption = opts[2] || null; next(); } catch (err) { - console.log('err', err); next(err); } }; diff --git a/modules/users/repositories/user.repository.js b/modules/users/repositories/user.repository.js index 288d7cb32..84d108153 100644 --- a/modules/users/repositories/user.repository.js +++ b/modules/users/repositories/user.repository.js @@ -50,7 +50,6 @@ exports.get = (user) => { }, }).exec(); } - return null; }; /** @@ -75,7 +74,6 @@ exports.update = (user) => new User(user).save(); exports.delete = async (user) => { if (user.id && mongoose.Types.ObjectId.isValid(user.id)) return User.deleteOne({ _id: user.id }).exec(); if (user.email) return User.deleteOne({ email: user.email }).exec(); - return null; }; /** diff --git a/modules/users/tests/user.crud.tests.js b/modules/users/tests/user.crud.tests.js index 686b4bdd9..9e5912eb4 100644 --- a/modules/users/tests/user.crud.tests.js +++ b/modules/users/tests/user.crud.tests.js @@ -781,7 +781,7 @@ describe('User CRUD Tests :', () => { } }); - test('should be able to update Terms sign date', async () => { + test('should be able to update Terms sign date if logged in', async () => { try { const result = await agent.get('/api/users/terms') .expect(200); @@ -1029,6 +1029,16 @@ describe('User CRUD Tests :', () => { }); describe('Logout', () => { + test('should not be able to update Terms sign date if not logged in', async () => { + try { + await agent.get('/api/users/terms') + .expect(401); + } catch (err) { + expect(err).toBeFalsy(); + console.log(err); + } + }); + test('should not be able to change user own password if not signed in', async () => { try { await agent.post('/api/users/password')