mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
add tests for improvement categories
This commit is contained in:
@@ -175,4 +175,27 @@ describe('PUT /user', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('Improvement Categories', () => {
|
||||||
|
it('sets valid categories', async () => {
|
||||||
|
await user.put('/user', {
|
||||||
|
'preferences.improvementCategories': ['work', 'school'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.sync();
|
||||||
|
|
||||||
|
expect(user.preferences.improvementCategories).to.eql(['work', 'school']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discards invalid categories', async () => {
|
||||||
|
await expect(user.put('/user', {
|
||||||
|
'preferences.improvementCategories': ['work', 'procrastination', 'school'],
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 400,
|
||||||
|
text: [
|
||||||
|
'Validator failed for path `preferences.improvementCategories` with value `work,procrastination,school`',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -355,7 +355,15 @@ api.update = (req, res, next) => {
|
|||||||
|
|
||||||
user.save((err) => {
|
user.save((err) => {
|
||||||
if (!_.isEmpty(errors)) return res.json(401, {err: errors});
|
if (!_.isEmpty(errors)) return res.json(401, {err: errors});
|
||||||
if (err) return next(err);
|
if (err) {
|
||||||
|
if (err.name == 'ValidationError') {
|
||||||
|
let errorMessages = _.map(_.values(err.errors), (error) => {
|
||||||
|
return error.message;
|
||||||
|
});
|
||||||
|
return res.json(400, {err: errorMessages});
|
||||||
|
}
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
res.json(200, user);
|
res.json(200, user);
|
||||||
user = errors = null;
|
user = errors = null;
|
||||||
|
|||||||
@@ -395,7 +395,7 @@ var UserSchema = new Schema({
|
|||||||
type: Array,
|
type: Array,
|
||||||
validate: (categories) => {
|
validate: (categories) => {
|
||||||
const validCategories = ['work', 'exercise', 'healthWellness', 'school', 'teams', 'chores', 'creativity'];
|
const validCategories = ['work', 'exercise', 'healthWellness', 'school', 'teams', 'chores', 'creativity'];
|
||||||
let isValidCategory = categories.every(category => validValues.indexOf(category) !== -1);
|
let isValidCategory = categories.every(category => validCategories.indexOf(category) !== -1);
|
||||||
return isValidCategory;
|
return isValidCategory;
|
||||||
}}
|
}}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user