mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
add missing test file and update parameters for GET tasks
This commit is contained in:
@@ -17,12 +17,12 @@ describe('GET /tasks/user', () => {
|
||||
|
||||
it('returns only a type of user\'s tasks if req.query.type is specified', async () => {
|
||||
let createdTasks = await user.post('/tasks/user', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]);
|
||||
let tasks = await user.get('/tasks/user?type=habit');
|
||||
let tasks = await user.get('/tasks/user?type=habits');
|
||||
expect(tasks.length).to.equal(1);
|
||||
expect(tasks[0]._id).to.equal(createdTasks[0]._id);
|
||||
});
|
||||
|
||||
it('returns completed todos sorted by completion date if req.query.includeCompletedTodos is specified', async () => {
|
||||
it('returns completed todos sorted by completion date if req.query.type === "completeTodos"', async () => {
|
||||
let todo1 = await user.post('/tasks/user', {text: 'todo to complete 1', type: 'todo'});
|
||||
let todo2 = await user.post('/tasks/user', {text: 'todo to complete 2', type: 'todo'});
|
||||
|
||||
@@ -35,8 +35,8 @@ describe('GET /tasks/user', () => {
|
||||
|
||||
expect(user.tasksOrder.todos.length).to.equal(initialTodoCount - 2);
|
||||
|
||||
let allTodos = await user.get('/tasks/user?type=todo&includeCompletedTodos=true');
|
||||
expect(allTodos.length).to.equal(initialTodoCount);
|
||||
expect(allTodos[allTodos.length - 1].text).to.equal('todo to complete 1'); // last is the todo that was completed later
|
||||
let completedTodos = await user.get('/tasks/user?type=completedTodos');
|
||||
expect(completedTodos.length).to.equal(2);
|
||||
expect(completedTodos[completedTodos.length - 1].text).to.equal('todo to complete 1'); // last is the todo that was completed later
|
||||
});
|
||||
});
|
||||
|
||||
@@ -24,7 +24,7 @@ describe('POST /tasks/clearCompletedTodos', () => {
|
||||
type: 'todo',
|
||||
});
|
||||
|
||||
let tasks = await user.get('/tasks/user?type=todo');
|
||||
let tasks = await user.get('/tasks/user?type=todos');
|
||||
expect(tasks.length).to.equal(initialTodoCount + 6);
|
||||
|
||||
for (let task of tasks) {
|
||||
@@ -34,8 +34,10 @@ describe('POST /tasks/clearCompletedTodos', () => {
|
||||
}
|
||||
|
||||
await user.post('/tasks/clearCompletedTodos');
|
||||
let tasksUpdated = await user.get('/tasks/user?type=todo&includeCompletedTodos=true');
|
||||
expect(tasksUpdated.length).to.equal(initialTodoCount + 4); // + 6 - 3 completed (but one is from challenge)
|
||||
expect(tasksUpdated[tasksUpdated.length - 1].text).to.equal('todo 6');
|
||||
let completedTodos = await user.get('/tasks/user?type=completedTodos');
|
||||
let todos = await user.get('/tasks/user?type=todos');
|
||||
let allTodos = todos.concat(completedTodos);
|
||||
expect(allTodos.length).to.equal(initialTodoCount + 4); // + 6 - 3 completed (but one is from challenge)
|
||||
expect(allTodos[allTodos.length - 1].text).to.equal('todo 6');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('POST /tasks/:taskId/move/to/:position', () => {
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('requires a valid taskId', async () => {
|
||||
await expect(user.post('/tasks/123/move/to/1')).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
});
|
||||
|
||||
it('requires a numeric position parameter', async () => {
|
||||
await expect(user.post(`/tasks/${generateUUID()}/move/to/notANumber`)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
});
|
||||
|
||||
it('taskId must match a valid task', async () => {
|
||||
await expect(user.post(`/tasks/${generateUUID()}/move/to/1`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('taskNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('can move task to new position', async () => {
|
||||
let tasks = await user.post('/tasks/user', [
|
||||
{type: 'habit', text: 'habit 1'},
|
||||
{type: 'habit', text: 'habit 2'},
|
||||
{type: 'daily', text: 'daily 1'},
|
||||
{type: 'habit', text: 'habit 3'},
|
||||
{type: 'habit', text: 'habit 4'},
|
||||
{type: 'todo', text: 'todo 1'},
|
||||
{type: 'habit', text: 'habit 5'},
|
||||
]);
|
||||
|
||||
let taskToMove = tasks[1];
|
||||
expect(taskToMove.text).to.equal('habit 2');
|
||||
let newOrder = await user.post(`/tasks/${tasks[1]._id}/move/to/3`);
|
||||
expect(newOrder[3]).to.equal(taskToMove._id);
|
||||
expect(newOrder.length).to.equal(5);
|
||||
});
|
||||
|
||||
it('can\'t move completed todo', async () => {
|
||||
let task = await user.post('/tasks/user', {type: 'todo', text: 'todo 1'});
|
||||
await user.post(`/tasks/${task._id}/score/up`);
|
||||
|
||||
await expect(user.post(`/tasks/${task._id}/move/to/1`)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('cantMoveCompletedTodo'),
|
||||
});
|
||||
});
|
||||
|
||||
it('can push to bottom', async () => {
|
||||
let tasks = await user.post('/tasks/user', [
|
||||
{type: 'habit', text: 'habit 1'},
|
||||
{type: 'habit', text: 'habit 2'},
|
||||
{type: 'daily', text: 'daily 1'},
|
||||
{type: 'habit', text: 'habit 3'},
|
||||
{type: 'habit', text: 'habit 4'},
|
||||
{type: 'todo', text: 'todo 1'},
|
||||
{type: 'habit', text: 'habit 5'},
|
||||
]);
|
||||
|
||||
let taskToMove = tasks[1];
|
||||
expect(taskToMove.text).to.equal('habit 2');
|
||||
let newOrder = await user.post(`/tasks/${tasks[1]._id}/move/to/-1`);
|
||||
expect(newOrder[4]).to.equal(taskToMove._id);
|
||||
expect(newOrder.length).to.equal(5);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { sendTaskWebhook } from '../../libs/api-v3/webhook';
|
||||
import { removeFromArray } from '../../libs/api-v3/collectionManipulators';
|
||||
import * as Tasks from '../../models/task';
|
||||
import { model as Challenge } from '../../models/challenge';
|
||||
import { model as Group } from '../../models/group';
|
||||
import {
|
||||
NotFound,
|
||||
NotAuthorized,
|
||||
@@ -117,8 +118,19 @@ async function _getTasks (req, res, user, challenge) {
|
||||
let type = req.query.type;
|
||||
|
||||
if (type) {
|
||||
query.type = type;
|
||||
if (type === 'todo') query.completed = false; // Exclude completed todos
|
||||
if (type === 'todos') {
|
||||
query.completed = false; // Exclude completed todos
|
||||
} else if (type === 'completedTodos') {
|
||||
query = Tasks.Task.find({
|
||||
userId: user._id,
|
||||
type: 'todo',
|
||||
completed: true,
|
||||
}).limit(30).sort({ // TODO add ability to pick more than 30 completed todos
|
||||
dateCompleted: 1,
|
||||
});
|
||||
} else {
|
||||
query.type = type.slice(0, -1); // removing the final "s"
|
||||
}
|
||||
} else {
|
||||
query.$or = [ // Exclude completed todos
|
||||
{type: 'todo', completed: false},
|
||||
@@ -126,28 +138,9 @@ async function _getTasks (req, res, user, challenge) {
|
||||
];
|
||||
}
|
||||
|
||||
if (req.query.includeCompletedTodos === 'true' && (!type || type === 'todo')) {
|
||||
if (challenge) throw new BadRequest(res.t('noCompletedTodosChallenge')); // no completed todos for challenges
|
||||
|
||||
let queryCompleted = Tasks.Task.find({
|
||||
userId: user._id,
|
||||
type: 'todo',
|
||||
completed: true,
|
||||
}).limit(30).sort({ // TODO add ability to pick more than 30 completed todos
|
||||
dateCompleted: 1,
|
||||
});
|
||||
|
||||
let results = await Q.all([
|
||||
queryCompleted.exec(),
|
||||
Tasks.Task.find(query).exec(),
|
||||
]);
|
||||
|
||||
res.respond(200, results[1].concat(results[0]));
|
||||
} else {
|
||||
let tasks = await Tasks.Task.find(query).exec();
|
||||
res.respond(200, tasks);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /tasks/user Get an user's tasks
|
||||
@@ -155,8 +148,7 @@ async function _getTasks (req, res, user, challenge) {
|
||||
* @apiName GetUserTasks
|
||||
* @apiGroup Task
|
||||
*
|
||||
* @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks
|
||||
* @apiParam {boolean} includeCompletedTodos Optional query parameter to include completed todos when "type" is "todo".
|
||||
* @apiParam {string="habits","dailys","todos","rewards","completeTodos"} type Optional query parameter to return just a type of tasks. By default all types will be returned except completed todos that requested separately.
|
||||
*
|
||||
* @apiSuccess {Array} tasks An array of task objects
|
||||
*/
|
||||
@@ -165,7 +157,9 @@ api.getUserTasks = {
|
||||
url: '/tasks/user',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
async handler (req, res) {
|
||||
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(Tasks.tasksTypes);
|
||||
let types = Tasks.tasksTypes.map(type => `${type}s`);
|
||||
types.push('completedTodos');
|
||||
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(types);
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
@@ -181,7 +175,7 @@ api.getUserTasks = {
|
||||
* @apiGroup Task
|
||||
*
|
||||
* @apiParam {UUID} challengeId The id of the challenge from which to retrieve the tasks.
|
||||
* @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks
|
||||
* @apiParam {string="habits","dailys","todos","rewards"} type Optional query parameter to return just a type of tasks
|
||||
*
|
||||
* @apiSuccess {Array} tasks An array of task objects
|
||||
*/
|
||||
@@ -191,7 +185,8 @@ api.getChallengeTasks = {
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
async handler (req, res) {
|
||||
req.checkParams('challengeId', res.t('challengeIdRequired')).notEmpty().isUUID();
|
||||
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(Tasks.tasksTypes);
|
||||
let types = Tasks.tasksTypes.map(type => `${type}s`);
|
||||
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(types);
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
@@ -200,11 +195,9 @@ api.getChallengeTasks = {
|
||||
let challengeId = req.params.challengeId;
|
||||
|
||||
let challenge = await Challenge.findOne({_id: challengeId}).select('leader').exec();
|
||||
|
||||
// If the challenge does not exist, or if it exists but user is not a member, not the leader and not an admin -> throw error
|
||||
if (!challenge || (user.challenges.indexOf(challengeId) === -1 && challenge.leader !== user._id && !user.contributor.admin)) { // eslint-disable-line no-extra-parens
|
||||
throw new NotFound(res.t('challengeNotFound'));
|
||||
}
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
let group = await Group.getGroup({user, groupId: challenge.groupId, fields: '_id type privacy', optionalMembership: true});
|
||||
if (!group || !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound'));
|
||||
|
||||
await _getTasks(req, res, res.locals.user, challenge);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user