mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
fix user model, sanitize some fields on task creation, add some tests and comments
This commit is contained in:
@@ -17,7 +17,7 @@ describe('POST /tasks', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('checks "type" is present and a valid value', () => {
|
context('checks "req.body.type"', () => {
|
||||||
it('returns an error if req.body.type is absent', () => {
|
it('returns an error if req.body.type is absent', () => {
|
||||||
expect(api.post('/tasks', {
|
expect(api.post('/tasks', {
|
||||||
notType: 'habit',
|
notType: 'habit',
|
||||||
@@ -27,5 +27,26 @@ describe('POST /tasks', () => {
|
|||||||
message: t('invalidReqParams'),
|
message: t('invalidReqParams'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns an error if req.body.type is not valid', () => {
|
||||||
|
expect(api.post('/tasks', {
|
||||||
|
type: 'habitF',
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 400,
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: t('invalidReqParams'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('checks "task.userId"', () => {
|
||||||
|
it('sets "task.userId" to valid value', () => {
|
||||||
|
return api.post('/tasks', {
|
||||||
|
text: 'test habit',
|
||||||
|
type: 'habit',
|
||||||
|
}).then((task) => {
|
||||||
|
expect(task.userId).to.equal(user._id);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -31,16 +31,16 @@ api.createTask = {
|
|||||||
let user = res.locals.user;
|
let user = res.locals.user;
|
||||||
let taskType = req.body.type;
|
let taskType = req.body.type;
|
||||||
|
|
||||||
let newTask = new Tasks[taskType](Tasks.Task.sanitize(req.body));
|
let newTask = new Tasks[taskType](Tasks.Task.sanitizeCreate(req.body));
|
||||||
newTask.userId = user._id;
|
newTask.userId = user._id;
|
||||||
|
|
||||||
user.tasksOrder[taskType].unshift(newTask._id);
|
user.tasksOrder[taskType + 's'].unshift(newTask._id);
|
||||||
|
|
||||||
Q.all([
|
Q.all([
|
||||||
newTask.save(),
|
newTask.save(),
|
||||||
user.save(),
|
user.save(),
|
||||||
])
|
])
|
||||||
.then(([task]) => res.respond(201, task))
|
.then((results) => res.respond(201, results[0]))
|
||||||
.catch(next);
|
.catch(next);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -478,7 +478,7 @@ api.deleteTask = {
|
|||||||
|
|
||||||
let validationErrors = req.validationErrors();
|
let validationErrors = req.validationErrors();
|
||||||
if (validationErrors) return next(validationErrors);
|
if (validationErrors) return next(validationErrors);
|
||||||
|
|
||||||
Tasks.Task.findOne({
|
Tasks.Task.findOne({
|
||||||
_id: req.params.taskId,
|
_id: req.params.taskId,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export let TaskSchema = new Schema({
|
|||||||
text: {type: String, required: true},
|
text: {type: String, required: true},
|
||||||
notes: {type: String, default: ''},
|
notes: {type: String, default: ''},
|
||||||
tags: {type: Schema.Types.Mixed, default: {}}, // TODO dictionary? { "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true }, validate
|
tags: {type: Schema.Types.Mixed, default: {}}, // TODO dictionary? { "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true }, validate
|
||||||
value: {type: Number, default: 0}, // redness
|
value: {type: Number, default: 0}, // redness or cost for rewards
|
||||||
priority: {type: Number, default: 1, required: true},
|
priority: {type: Number, default: 1, required: true},
|
||||||
attribute: {type: String, default: 'str', enum: ['str', 'con', 'int', 'per']},
|
attribute: {type: String, default: 'str', enum: ['str', 'con', 'int', 'per']},
|
||||||
userId: {type: String, ref: 'User'}, // When null it belongs to a challenge
|
userId: {type: String, ref: 'User'}, // When null it belongs to a challenge
|
||||||
@@ -40,8 +40,14 @@ TaskSchema.plugin(baseModel, {
|
|||||||
timestamps: true,
|
timestamps: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// A list of additional fields that cannot be set on creation (but can be set on updare)
|
||||||
|
let noCreate = ['completed'];
|
||||||
|
TaskSchema.statics.sanitizeCreate = function sanitizeCreate (createObj) {
|
||||||
|
return Task.sanitize(createObj, noCreate); // eslint-disable-line no-use-before-define
|
||||||
|
};
|
||||||
|
|
||||||
// A list of additional fields that cannot be updated (but can be set on creation)
|
// A list of additional fields that cannot be updated (but can be set on creation)
|
||||||
let noUpdate = ['_id', 'type'];
|
let noUpdate = ['_id', 'type']; // TODO should prevent changes to checlist.*.id
|
||||||
TaskSchema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
|
TaskSchema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
|
||||||
return Task.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
|
return Task.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -628,6 +628,8 @@ schema.pre('save', true, function preSaveUser (next, done) {
|
|||||||
_populateDefaultsForNewUser(this)
|
_populateDefaultsForNewUser(this)
|
||||||
.then(() => done())
|
.then(() => done())
|
||||||
.catch(done);
|
.catch(done);
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user