Skip to content

Commit c93d1e0

Browse files
Merge pull request #1207 from PierreBrisorgueil/refactorImproveCoverage
refactor(global): improve coverage ♻️
2 parents ac6ba7b + 047968b commit c93d1e0

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

lib/helpers/errors.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
* Module dependencies
3-
*/
4-
const path = require('path');
5-
6-
const config = require(path.resolve('./config'));
7-
81
/**
92
* @desc Function to parse error message
103
* @param {Object} err
@@ -26,7 +19,7 @@ const getUniqueMessage = (err) => {
2619
}
2720
const fieldName = err.errmsg.substring(begin, err.errmsg.lastIndexOf('_1'));
2821
output = `${fieldName.charAt(0).toUpperCase() + fieldName.slice(1)} already exists`;
29-
} catch (ex) {
22+
} catch (err) {
3023
output = 'Unique field already exists';
3124
}
3225

@@ -41,19 +34,9 @@ const getUniqueMessage = (err) => {
4134
const getMessageFromCode = (err) => {
4235
let output;
4336
switch (err.code) {
44-
case 11000:
4537
case 11001:
4638
output = getUniqueMessage(err);
4739
break;
48-
case 'UNSUPPORTED_MEDIA_TYPE':
49-
output = 'Unsupported filetype.';
50-
break;
51-
case 'LIMIT_FILE_SIZE':
52-
output = `Image file too large. Maximum size allowed is ${(config.uploads.profile.avatar.limits.fileSize / (1024 * 1024)).toFixed(2)} Mb files.`;
53-
break;
54-
case 'LIMIT_UNEXPECTED_FILE':
55-
output = 'Missing `newProfilePicture` field.';
56-
break;
5740
default: {
5841
if (err.message) output = err.message;
5942
else output = 'Something went wrong.';
@@ -63,28 +46,24 @@ const getMessageFromCode = (err) => {
6346
};
6447

6548
/**
66-
* @desc Function to map an array of errors
49+
* @desc Function to map an array/object of errors
6750
* @param {Object} err
6851
* @return {String} message
6952
*/
7053
const getMessageFromErrors = (err) => {
71-
if (err instanceof Array) return err.errors;
7254
let output = '';
7355
if (err.errors instanceof Array) {
74-
err.errors.map((error) => {
56+
err.errors.forEach((error) => {
7557
if (error.message) {
7658
output += `${error.message} `;
7759
}
78-
return null;
7960
});
8061
} else if (err.errors instanceof Object) {
8162
Object.keys(err.errors).forEach((key) => {
8263
if (err.errors[key].message) {
8364
output += `${err.errors[key].message} `;
8465
}
8566
});
86-
} else {
87-
output = err.message;
8867
}
8968
return output;
9069
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const logger = require(path.resolve('./lib/services/logger'));
1010
const mongooseService = require(path.resolve('./lib/services/mongoose'));
1111
const multerService = require(path.resolve('./lib/services/multer'));
1212
const seed = require(path.resolve('./lib/services/seed'));
13+
const errors = require(path.resolve('./lib/helpers/errors'));
1314

1415
/**
1516
* Unit tests
@@ -396,6 +397,36 @@ describe('Configuration Tests:', () => {
396397
});
397398
});
398399

400+
describe('Errors', () => {
401+
test('should return errors message properly', async () => {
402+
try {
403+
const fromCode = errors.getMessage({ code: 11001, errmsg: 'test' });
404+
expect(fromCode).toBe('Test already exists.');
405+
406+
const fromCode2 = errors.getMessage({ code: 11001, errmsg: 'test.$.test' });
407+
expect(fromCode2).toBe('Test.$ already exists.');
408+
409+
const fromCodeUnknow = errors.getMessage({ code: 'unknow' });
410+
expect(fromCodeUnknow).toBe('Something went wrong.');
411+
412+
const fromErrorsArray = errors.getMessage({ errors: [{ message: 'error1' }, { message: 'error2' }] });
413+
expect(fromErrorsArray).toBe('error1 error2 .');
414+
415+
const fromErrorsObject = errors.getMessage({ errors: { one: { message: 'error1' }, two: { message: 'error2' } } });
416+
expect(fromErrorsObject).toBe('error1 error2 .');
417+
418+
const fromMessage = errors.getMessage({ message: 'error1' });
419+
expect(fromMessage).toBe('error1.');
420+
421+
const fromEmpty = errors.getMessage({ });
422+
expect(fromEmpty).toBe('error while retrieving the error :o : {}.');
423+
} catch (err) {
424+
console.log(err);
425+
expect(err).toBeFalsy();
426+
}
427+
});
428+
});
429+
399430
// Mongoose disconnect
400431
afterAll(() => mongooseService.disconnect()
401432
.catch((e) => {

modules/uploads/controllers/uploads.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ exports.uploadByImageName = async (req, res, next, uploadImageName) => {
129129
req.sharpOption = opts[2] || null;
130130
next();
131131
} catch (err) {
132-
console.log('err', err);
133132
next(err);
134133
}
135134
};

modules/users/repositories/user.repository.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ exports.get = (user) => {
5050
},
5151
}).exec();
5252
}
53-
return null;
5453
};
5554

5655
/**
@@ -75,7 +74,6 @@ exports.update = (user) => new User(user).save();
7574
exports.delete = async (user) => {
7675
if (user.id && mongoose.Types.ObjectId.isValid(user.id)) return User.deleteOne({ _id: user.id }).exec();
7776
if (user.email) return User.deleteOne({ email: user.email }).exec();
78-
return null;
7977
};
8078

8179
/**

modules/users/tests/user.crud.tests.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ describe('User CRUD Tests :', () => {
781781
}
782782
});
783783

784-
test('should be able to update Terms sign date', async () => {
784+
test('should be able to update Terms sign date if logged in', async () => {
785785
try {
786786
const result = await agent.get('/api/users/terms')
787787
.expect(200);
@@ -1029,6 +1029,16 @@ describe('User CRUD Tests :', () => {
10291029
});
10301030

10311031
describe('Logout', () => {
1032+
test('should not be able to update Terms sign date if not logged in', async () => {
1033+
try {
1034+
await agent.get('/api/users/terms')
1035+
.expect(401);
1036+
} catch (err) {
1037+
expect(err).toBeFalsy();
1038+
console.log(err);
1039+
}
1040+
});
1041+
10321042
test('should not be able to change user own password if not signed in', async () => {
10331043
try {
10341044
await agent.post('/api/users/password')

0 commit comments

Comments
 (0)