mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
checklists tests
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
translate as t,
|
||||
} from '../../../../../helpers/api-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
|
||||
let user, api;
|
||||
|
||||
before(() => {
|
||||
return generateUser().then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes a checklist item', () => {
|
||||
let task;
|
||||
|
||||
return api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'Daily with checklist',
|
||||
}).then(createdTask => {
|
||||
task = createdTask;
|
||||
return api.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false});
|
||||
}).then((savedTask) => {
|
||||
return api.del(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`);
|
||||
}).then(() => {
|
||||
return api.get(`/tasks/${task._id}`);
|
||||
}).then((savedTask) => {
|
||||
expect(savedTask.checklist.length).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('does not work with habits', () => {
|
||||
let habit;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'habit',
|
||||
text: 'habit with checklist',
|
||||
}).then(createdTask => {
|
||||
habit = createdTask;
|
||||
return api.del(`/tasks/${habit._id}/checklist/${generateUUID()}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('does not work with rewards', () => {
|
||||
let reward;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'reward',
|
||||
text: 'reward with checklist',
|
||||
}).then(createdTask => {
|
||||
reward = createdTask;
|
||||
return api.del(`/tasks/${reward._id}/checklist/${generateUUID()}`);
|
||||
}).then(checklistItem => {})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on task not found', () => {
|
||||
return expect(api.del(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('taskNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on checklist item not found', () => {
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'daily with checklist',
|
||||
}).then(createdTask => {
|
||||
return api.del(`/tasks/${createdTask._id}/checklist/${generateUUID()}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('checklistItemNotFound'),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
translate as t,
|
||||
} from '../../../../../helpers/api-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('POST /tasks/:taskId/checklist/', () => {
|
||||
let user, api;
|
||||
|
||||
before(() => {
|
||||
return generateUser().then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('adds a checklist item to a task', () => {
|
||||
let task;
|
||||
|
||||
return api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'Daily with checklist',
|
||||
}).then(createdTask => {
|
||||
task = createdTask;
|
||||
return api.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', ignored: false, _id: 123});
|
||||
}).then((savedTask) => {
|
||||
expect(savedTask.checklist.length).to.equal(1);
|
||||
expect(savedTask.checklist[0].text).to.equal('Checklist Item 1');
|
||||
expect(savedTask.checklist[0].completed).to.equal(false);
|
||||
expect(savedTask.checklist[0]._id).to.be.a('string');
|
||||
expect(savedTask.checklist[0]._id).to.not.equal('123');
|
||||
expect(savedTask.checklist[0].ignored).to.be.an('undefined');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not add a checklist to habits', () => {
|
||||
let habit;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'habit',
|
||||
text: 'habit with checklist',
|
||||
}).then(createdTask => {
|
||||
habit = createdTask;
|
||||
return api.post(`/tasks/${habit._id}/checklist`, {text: 'Checklist Item 1'});
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('does not add a checklist to rewards', () => {
|
||||
let reward;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'reward',
|
||||
text: 'reward with checklist',
|
||||
}).then(createdTask => {
|
||||
reward = createdTask;
|
||||
return api.post(`/tasks/${reward._id}/checklist`, {text: 'Checklist Item 1'});
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on task not found', () => {
|
||||
return expect(api.post(`/tasks/${generateUUID()}/checklist`, {
|
||||
text: 'Checklist Item 1'
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('taskNotFound'),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
translate as t,
|
||||
} from '../../../../../helpers/api-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
|
||||
let user, api;
|
||||
|
||||
before(() => {
|
||||
return generateUser().then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('scores a checklist item', () => {
|
||||
let task;
|
||||
|
||||
return api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'Daily with checklist',
|
||||
}).then(createdTask => {
|
||||
task = createdTask;
|
||||
return api.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false});
|
||||
}).then((savedTask) => {
|
||||
return api.post(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}/score`);
|
||||
}).then((savedTask) => {
|
||||
expect(savedTask.checklist.length).to.equal(1);
|
||||
expect(savedTask.checklist[0].completed).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on habits', () => {
|
||||
let habit;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'habit',
|
||||
text: 'habit with checklist',
|
||||
}).then(createdTask => {
|
||||
habit = createdTask;
|
||||
return api.post(`/tasks/${habit._id}/checklist/${generateUUID()}/score`, {text: 'Checklist Item 1'});
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on rewards', () => {
|
||||
let reward;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'reward',
|
||||
text: 'reward with checklist',
|
||||
}).then(createdTask => {
|
||||
reward = createdTask;
|
||||
return api.post(`/tasks/${reward._id}/checklist/${generateUUID()}/score`);
|
||||
}).then(checklistItem => {})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on task not found', () => {
|
||||
return expect(api.post(`/tasks/${generateUUID()}/checklist/${generateUUID()}/score`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('taskNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on checklist item not found', () => {
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'daily with checklist',
|
||||
}).then(createdTask => {
|
||||
return api.post(`/tasks/${createdTask._id}/checklist/${generateUUID()}/score`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('checklistItemNotFound'),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
translate as t,
|
||||
} from '../../../../../helpers/api-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('PUT /tasks/:taskId/checklist/:itemId', () => {
|
||||
let user, api;
|
||||
|
||||
before(() => {
|
||||
return generateUser().then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('updates a checklist item', () => {
|
||||
let task;
|
||||
|
||||
return api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'Daily with checklist',
|
||||
}).then(createdTask => {
|
||||
task = createdTask;
|
||||
return api.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false});
|
||||
}).then((savedTask) => {
|
||||
return api.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {text: 'updated', completed: true, _id: 123});
|
||||
}).then((savedTask) => {
|
||||
expect(savedTask.checklist.length).to.equal(1);
|
||||
expect(savedTask.checklist[0].text).to.equal('updated');
|
||||
expect(savedTask.checklist[0].completed).to.equal(true);
|
||||
expect(savedTask.checklist[0]._id).to.not.equal('123');
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on habits', () => {
|
||||
let habit;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'habit',
|
||||
text: 'habit with checklist',
|
||||
}).then(createdTask => {
|
||||
habit = createdTask;
|
||||
return api.put(`/tasks/${habit._id}/checklist/${generateUUID()}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on rewards', () => {
|
||||
let reward;
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'reward',
|
||||
text: 'reward with checklist',
|
||||
}).then(createdTask => {
|
||||
reward = createdTask;
|
||||
return api.put(`/tasks/${reward._id}/checklist/${generateUUID()}`);
|
||||
}).then(checklistItem => {})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('checklistOnlyDailyTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on task not found', () => {
|
||||
return expect(api.put(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('taskNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on checklist item not found', () => {
|
||||
return expect(api.post('/tasks', {
|
||||
type: 'daily',
|
||||
text: 'daily with checklist',
|
||||
}).then(createdTask => {
|
||||
return api.put(`/tasks/${createdTask._id}/checklist/${generateUUID()}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('checklistItemNotFound'),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -368,7 +368,7 @@ api.addChecklistItem = {
|
||||
if (!task) throw new NotFound(res.t('taskNotFound'));
|
||||
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
||||
|
||||
task.checklist.push(req.body);
|
||||
task.checklist.push(Tasks.Task.sanitizeChecklist(req.body));
|
||||
return task.save();
|
||||
})
|
||||
.then((savedTask) => res.respond(200, savedTask)) // TODO what to return
|
||||
@@ -454,8 +454,7 @@ api.updateChecklistItem = {
|
||||
let item = _.find(task.checklist, {_id: req.params.itemId});
|
||||
if (!item) throw new NotFound(res.t('checklistItemNotFound'));
|
||||
|
||||
delete req.body.id; // Simple sanitization to prevent the ID to be changed
|
||||
_.merge(item, req.body);
|
||||
_.merge(item, Tasks.Task.sanitizeChecklist(req.body));
|
||||
return task.save();
|
||||
})
|
||||
.then((savedTask) => res.respond(200, savedTask)) // TODO what to return
|
||||
|
||||
@@ -57,6 +57,12 @@ TaskSchema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
|
||||
return Task.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
|
||||
};
|
||||
|
||||
// Sanitize checklist objects (disallowing _id)
|
||||
TaskSchema.statics.sanitizeChecklist = function sanitizeChecklist (checklistObj) {
|
||||
delete checklistObj._id;
|
||||
return checklistObj;
|
||||
};
|
||||
|
||||
export let Task = mongoose.model('Task', TaskSchema);
|
||||
|
||||
// habits and dailies shared fields
|
||||
|
||||
Reference in New Issue
Block a user