mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
misc fixes, add tests for some tasks routes
This commit is contained in:
@@ -18,18 +18,52 @@ describe('DELETE /tasks/:id', () => {
|
|||||||
let task;
|
let task;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// generate task
|
return api.post('/tasks', {
|
||||||
// task = generatedTask;
|
text: 'test habit',
|
||||||
|
type: 'habit',
|
||||||
|
}).then((createdTask) => {
|
||||||
|
task = createdTask;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deletes a user\'s task');
|
it('deletes a user\'s task', () => {
|
||||||
|
return api.del('/tasks/' + task._id)
|
||||||
|
.then(() => {
|
||||||
|
return expect(api.get('/tasks/' + task._id)).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 404,
|
||||||
|
error: 'NotFound',
|
||||||
|
message: t('taskNotFound'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('task cannot be deleted', () => {
|
context('task cannot be deleted', () => {
|
||||||
it('cannot delete a non-existant task');
|
it('cannot delete a non-existant task', () => {
|
||||||
|
return expect(api.del('/tasks/550e8400-e29b-41d4-a716-446655440000')).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 404,
|
||||||
|
error: 'NotFound',
|
||||||
|
message: t('taskNotFound'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('cannot delete a task owned by someone else');
|
it('cannot delete a task owned by someone else', () => {
|
||||||
|
return generateUser()
|
||||||
|
.then((user2) => {
|
||||||
|
return requester(user2).post('/tasks', {
|
||||||
|
text: 'test habit',
|
||||||
|
type: 'habit',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then((task2) => {
|
||||||
|
return expect(api.del('/tasks/' + task2._id)).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 404,
|
||||||
|
error: 'NotFound',
|
||||||
|
message: t('taskNotFound'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('cannot delete active challenge tasks');
|
it('cannot delete active challenge tasks'); // TODO after challenges are implemented
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
requester,
|
requester,
|
||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../helpers/api-integration.helper';
|
} from '../../../../helpers/api-integration.helper';
|
||||||
|
import Q from 'q';
|
||||||
|
|
||||||
describe('GET /tasks', () => {
|
describe('GET /tasks', () => {
|
||||||
let user, api;
|
let user, api;
|
||||||
@@ -14,5 +15,33 @@ describe('GET /tasks', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns all user\'s tasks');
|
it('returns all user\'s tasks', () => {
|
||||||
|
let length;
|
||||||
|
return Q.all([
|
||||||
|
api.post('/tasks', {text: 'test habit', type: 'habit'}),
|
||||||
|
])
|
||||||
|
.then((createdTasks) => {
|
||||||
|
length = createdTasks.length;
|
||||||
|
return api.get('/tasks');
|
||||||
|
})
|
||||||
|
.then((tasks) => {
|
||||||
|
expect(tasks.length).to.equal(length + 1); // + 1 because 1 is a default task
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns only a type of user\'s tasks if req.query.type is specified', () => {
|
||||||
|
let habitId;
|
||||||
|
api.post('/tasks', {text: 'test habit', type: 'habit'})
|
||||||
|
.then((task) => {
|
||||||
|
habitId = task._id;
|
||||||
|
return api.get('/tasks?type=habit');
|
||||||
|
})
|
||||||
|
.then((tasks) => {
|
||||||
|
expect(tasks.length).to.equal(1);
|
||||||
|
expect(tasks[0]._id).to.equal(habitId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO complete after task scoring is done
|
||||||
|
it('returns completed todos sorted by creation date if req.query.includeCompletedTodos is specified')
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ let api = {};
|
|||||||
*
|
*
|
||||||
* @apiSuccess {Object} task The newly created task
|
* @apiSuccess {Object} task The newly created task
|
||||||
*/
|
*/
|
||||||
|
// TODO should allow to create multiple tasks at once
|
||||||
|
// TODO gives problems when creating tasks concurrently because of how mongoose treats arrays (VersionErrors - treated as 500s)
|
||||||
api.createTask = {
|
api.createTask = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/tasks',
|
url: '/tasks',
|
||||||
@@ -61,7 +63,7 @@ api.getTasks = {
|
|||||||
url: '/tasks',
|
url: '/tasks',
|
||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
handler (req, res, next) {
|
handler (req, res, next) {
|
||||||
req.checkQuery('type', res.t('invalidTaskType')).isIn(Tasks.tasksTypes);
|
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(Tasks.tasksTypes);
|
||||||
|
|
||||||
let validationErrors = req.validationErrors();
|
let validationErrors = req.validationErrors();
|
||||||
if (validationErrors) return next(validationErrors);
|
if (validationErrors) return next(validationErrors);
|
||||||
@@ -74,7 +76,7 @@ api.getTasks = {
|
|||||||
query.type = type;
|
query.type = type;
|
||||||
if (type === 'todo') query.completed = false; // Exclude completed todos
|
if (type === 'todo') query.completed = false; // Exclude completed todos
|
||||||
} else {
|
} else {
|
||||||
query.$and = [ // Exclude completed todos
|
query.$or = [ // Exclude completed todos
|
||||||
{type: 'todo', completed: false},
|
{type: 'todo', completed: false},
|
||||||
{type: {$in: ['habit', 'daily', 'reward']}},
|
{type: {$in: ['habit', 'daily', 'reward']}},
|
||||||
];
|
];
|
||||||
@@ -532,7 +534,7 @@ api.removeTagFromTask = {
|
|||||||
function _removeTaskTasksOrder (user, taskId) {
|
function _removeTaskTasksOrder (user, taskId) {
|
||||||
// Loop through all lists and when the task is found, remove it and return
|
// Loop through all lists and when the task is found, remove it and return
|
||||||
for (let i = 0; i < Tasks.tasksTypes.length; i++) {
|
for (let i = 0; i < Tasks.tasksTypes.length; i++) {
|
||||||
let list = user.tasksOrder[Tasks.tasksTypes[i]];
|
let list = user.tasksOrder[`${Tasks.tasksTypes[i]}s`];
|
||||||
let index = list.indexOf(taskId);
|
let index = list.indexOf(taskId);
|
||||||
|
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
@@ -555,7 +557,7 @@ function _removeTaskTasksOrder (user, taskId) {
|
|||||||
* @apiSuccess {object} empty An empty object
|
* @apiSuccess {object} empty An empty object
|
||||||
*/
|
*/
|
||||||
api.deleteTask = {
|
api.deleteTask = {
|
||||||
method: 'GET',
|
method: 'DELETE',
|
||||||
url: '/tasks/:taskId',
|
url: '/tasks/:taskId',
|
||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
handler (req, res, next) {
|
handler (req, res, next) {
|
||||||
|
|||||||
@@ -15,4 +15,4 @@ schema.plugin(baseModel, {
|
|||||||
noSet: ['_id', 'challenge'],
|
noSet: ['_id', 'challenge'],
|
||||||
});
|
});
|
||||||
|
|
||||||
export let model = mongoose.model('Tag', TagSchema);
|
export let model = mongoose.model('Tag', schema);
|
||||||
|
|||||||
Reference in New Issue
Block a user