diff --git a/common/locales/en/api-v3.json b/common/locales/en/api-v3.json index 77930e4583..a97308e064 100644 --- a/common/locales/en/api-v3.json +++ b/common/locales/en/api-v3.json @@ -49,5 +49,6 @@ "winnerNotFound": "Winner with id \"<%= userId %>\" not found or not part of the challenge.", "noCompletedTodosChallenge": "\"includeComepletedTodos\" is not supported when fetching a challenge tasks.", "userTasksNoChallengeId": "When \"tasksOwner\" is \"user\" \"challengeId\" can't be passed.", - "onlyChalLeaderEditTasks": "Tasks belonging to a challenge can only be edited by the leader." + "onlyChalLeaderEditTasks": "Tasks belonging to a challenge can only be edited by the leader.", + "invalidTasksOwner": "\"tasksOwner\" must be \"user\" or \"challenge\"." } diff --git a/test/api/v3/integration/tasks/DELETE-tasks_id.test.js b/test/api/v3/integration/tasks/DELETE-tasks_id.test.js index 1d3e59ef15..503e484e66 100644 --- a/test/api/v3/integration/tasks/DELETE-tasks_id.test.js +++ b/test/api/v3/integration/tasks/DELETE-tasks_id.test.js @@ -16,7 +16,7 @@ describe('DELETE /tasks/:id', () => { let task; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }).then((createdTask) => { @@ -48,7 +48,7 @@ describe('DELETE /tasks/:id', () => { it('cannot delete a task owned by someone else', () => { return generateUser() .then((anotherUser) => { - return anotherUser.post('/tasks', { + return anotherUser.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }); diff --git a/test/api/v3/integration/tasks/GET-tasks.test.js b/test/api/v3/integration/tasks/GET-tasks.test.js index 3f1c50448d..bf77442ea4 100644 --- a/test/api/v3/integration/tasks/GET-tasks.test.js +++ b/test/api/v3/integration/tasks/GET-tasks.test.js @@ -16,14 +16,14 @@ describe('GET /tasks', () => { }); it('returns all user\'s tasks', async () => { - let createdTasks = await user.post('/tasks', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]); - let tasks = await user.get('/tasks/user'); + let createdTasks = await user.post('/tasks?tasksOwner=user', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]); + let tasks = await user.get('/tasks?tasksOwner=user'); expect(tasks.length).to.equal(createdTasks.length + 1); // + 1 because 1 is a default task }); it('returns only a type of user\'s tasks if req.query.type is specified', async () => { - let createdTasks = await user.post('/tasks', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]); - let tasks = await user.get('/tasks/user?type=habit'); + let createdTasks = await user.post('/tasks?tasksOwner=user', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]); + let tasks = await user.get('/tasks?tasksOwner=user&type=habit'); expect(tasks.length).to.equal(1); expect(tasks[0]._id).to.equal(createdTasks[0]._id); }); diff --git a/test/api/v3/integration/tasks/GET-tasks_id.test.js b/test/api/v3/integration/tasks/GET-tasks_id.test.js index a4ae455d6a..b88d8a95a6 100644 --- a/test/api/v3/integration/tasks/GET-tasks_id.test.js +++ b/test/api/v3/integration/tasks/GET-tasks_id.test.js @@ -17,7 +17,7 @@ describe('GET /tasks/:id', () => { let task; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }).then((createdTask) => { @@ -54,7 +54,7 @@ describe('GET /tasks/:id', () => { .then((user2) => { anotherUser = user2; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }); diff --git a/test/api/v3/integration/tasks/POST-tasks.test.js b/test/api/v3/integration/tasks/POST-tasks.test.js index 245cea7d28..a002119634 100644 --- a/test/api/v3/integration/tasks/POST-tasks.test.js +++ b/test/api/v3/integration/tasks/POST-tasks.test.js @@ -14,7 +14,7 @@ describe('POST /tasks', () => { context('validates params', () => { it('returns an error if req.body.type is absent', async () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { notType: 'habit', })).to.eventually.be.rejected.and.eql({ code: 400, @@ -24,7 +24,7 @@ describe('POST /tasks', () => { }); it('returns an error if req.body.type is not valid', async () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habitF', })).to.eventually.be.rejected.and.eql({ code: 400, @@ -34,7 +34,7 @@ describe('POST /tasks', () => { }); it('returns an error if one object inside an array is invalid', async () => { - return expect(user.post('/tasks', [ + return expect(user.post('/tasks?tasksOwner=user', [ {type: 'habitF'}, {type: 'habit'}, ])).to.eventually.be.rejected.and.eql({ @@ -45,7 +45,7 @@ describe('POST /tasks', () => { }); it('returns an error if req.body.text is absent', async () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', })).to.eventually.be.rejected.and.eql({ code: 400, @@ -56,7 +56,7 @@ describe('POST /tasks', () => { it('does not update user.tasksOrder.{taskType} when the task is not saved because invalid', async () => { let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', })).to.eventually.be.rejected.and.eql({ // this block is necessary code: 400, @@ -71,7 +71,7 @@ describe('POST /tasks', () => { it('does not update user.tasksOrder.{taskType} when a task inside an array is not saved because invalid', async () => { let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits; - return expect(user.post('/tasks', [ + return expect(user.post('/tasks?tasksOwner=user', [ {type: 'habit'}, // Missing text {type: 'habit', text: 'valid'}, // Valid ])).to.eventually.be.rejected.and.eql({ // this block is necessary @@ -86,8 +86,8 @@ describe('POST /tasks', () => { }); it('does not save any task sent in an array when 1 is invalid', async () => { - let originalTasks = await user.get('/tasks'); - return expect(user.post('/tasks', [ + let originalTasks = await user.get('/tasks?tasksOwner=user'); + return expect(user.post('/tasks?tasksOwner=user', [ {type: 'habit'}, // Missing text {type: 'habit', text: 'valid'}, // Valid ])).to.eventually.be.rejected.and.eql({ // this block is necessary @@ -95,14 +95,14 @@ describe('POST /tasks', () => { error: 'BadRequest', message: 'habit validation failed', }).then(async () => { - let updatedTasks = await user.get('/tasks'); + let updatedTasks = await user.get('/tasks?tasksOwner=user'); expect(updatedTasks).to.eql(originalTasks); }); }); it('automatically sets "task.userId" to user\'s uuid', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }); @@ -113,7 +113,7 @@ describe('POST /tasks', () => { it(`ignores setting userId, history, createdAt, updatedAt, challenge, completed, streak, dateCompleted fields`, async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', userId: 123, @@ -137,7 +137,7 @@ describe('POST /tasks', () => { }); it('ignores invalid fields', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', notValid: true, @@ -149,7 +149,7 @@ describe('POST /tasks', () => { context('habits', () => { it('creates a habit', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', up: false, @@ -167,7 +167,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.habits when a new habit is created', async () => { let originalHabitsOrderLen = (await user.get('/user')).tasksOrder.habits.length; - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'an habit', }); @@ -179,7 +179,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.habits when multiple habits are created', async () => { let originalHabitsOrderLen = (await user.get('/user')).tasksOrder.habits.length; - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ type: 'habit', text: 'an habit', }, { @@ -194,7 +194,7 @@ describe('POST /tasks', () => { }); it('creates multiple habits', async () => { - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ text: 'test habit', type: 'habit', up: false, @@ -224,7 +224,7 @@ describe('POST /tasks', () => { }); it('defaults to setting up and down to true', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', notes: 1976, @@ -235,7 +235,7 @@ describe('POST /tasks', () => { }); it('cannot create checklists', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', checklist: [ @@ -249,7 +249,7 @@ describe('POST /tasks', () => { context('todos', () => { it('creates a todo', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test todo', type: 'todo', notes: 1976, @@ -262,7 +262,7 @@ describe('POST /tasks', () => { }); it('creates multiple todos', async () => { - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ text: 'test todo', type: 'todo', notes: 1976, @@ -285,7 +285,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.todos when a new todo is created', async () => { let originalTodosOrderLen = (await user.get('/user')).tasksOrder.todos.length; - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { type: 'todo', text: 'a todo', }); @@ -297,7 +297,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.todos when multiple todos are created', async () => { let originalTodosOrderLen = (await user.get('/user')).tasksOrder.todos.length; - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ type: 'todo', text: 'a todo', }, { @@ -312,7 +312,7 @@ describe('POST /tasks', () => { }); it('can create checklists', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test todo', type: 'todo', checklist: [ @@ -333,7 +333,7 @@ describe('POST /tasks', () => { it('creates a daily', async () => { let now = new Date(); - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', notes: 1976, @@ -352,7 +352,7 @@ describe('POST /tasks', () => { }); it('creates multiple dailys', async () => { - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ text: 'test daily', type: 'daily', notes: 1976, @@ -375,7 +375,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.dailys when a new daily is created', async () => { let originalDailysOrderLen = (await user.get('/user')).tasksOrder.dailys.length; - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'a daily', }); @@ -387,7 +387,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.dailys when multiple dailys are created', async () => { let originalDailysOrderLen = (await user.get('/user')).tasksOrder.dailys.length; - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ type: 'daily', text: 'a daily', }, { @@ -402,7 +402,7 @@ describe('POST /tasks', () => { }); it('defaults to a weekly frequency, with every day set', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', }); @@ -421,7 +421,7 @@ describe('POST /tasks', () => { }); it('allows repeat field to be configured', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', repeat: { @@ -445,7 +445,7 @@ describe('POST /tasks', () => { it('defaults startDate to today', async () => { let today = (new Date()).getDay(); - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', }); @@ -454,7 +454,7 @@ describe('POST /tasks', () => { }); it('can create checklists', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', checklist: [ @@ -473,7 +473,7 @@ describe('POST /tasks', () => { context('rewards', () => { it('creates a reward', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', notes: 1976, @@ -488,7 +488,7 @@ describe('POST /tasks', () => { }); it('creates multiple rewards', async () => { - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ text: 'test reward', type: 'reward', notes: 1976, @@ -515,7 +515,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.rewards when a new reward is created', async () => { let originalRewardsOrderLen = (await user.get('/user')).tasksOrder.rewards.length; - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { type: 'reward', text: 'a reward', }); @@ -527,7 +527,7 @@ describe('POST /tasks', () => { it('updates user.tasksOrder.dreward when multiple rewards are created', async () => { let originalRewardsOrderLen = (await user.get('/user')).tasksOrder.rewards.length; - let [task, task2] = await user.post('/tasks', [{ + let [task, task2] = await user.post('/tasks?tasksOwner=user', [{ type: 'reward', text: 'a reward', }, { @@ -542,7 +542,7 @@ describe('POST /tasks', () => { }); it('defaults to a 0 value', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', }); @@ -551,7 +551,7 @@ describe('POST /tasks', () => { }); it('requires value to be coerced into a number', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', value: '10', @@ -561,7 +561,7 @@ describe('POST /tasks', () => { }); it('cannot create checklists', async () => { - let task = await user.post('/tasks', { + let task = await user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', checklist: [ diff --git a/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js b/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js index 2978d93657..1c552613b4 100644 --- a/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js +++ b/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js @@ -37,7 +37,7 @@ describe('POST /tasks/:id/score/:direction', () => { let todo; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test todo', type: 'todo', }).then((task) => { @@ -149,7 +149,7 @@ describe('POST /tasks/:id/score/:direction', () => { let daily; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', }).then((task) => { @@ -226,26 +226,26 @@ describe('POST /tasks/:id/score/:direction', () => { let habit, minusHabit, plusHabit, neitherHabit; // eslint-disable-line no-unused-vars beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }).then((task) => { habit = task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test min habit', type: 'habit', up: false, }); }).then((task) => { minusHabit = task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test plus habit', type: 'habit', down: false, }); }).then((task) => { plusHabit = task; - user.post('/tasks', { + user.post('/tasks?tasksOwner=user', { text: 'test neither habit', type: 'habit', up: false, @@ -297,7 +297,7 @@ describe('POST /tasks/:id/score/:direction', () => { let reward; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', value: 5, diff --git a/test/api/v3/integration/tasks/PUT-tasks_id.test.js b/test/api/v3/integration/tasks/PUT-tasks_id.test.js index 3092d3bd84..408ef92e99 100644 --- a/test/api/v3/integration/tasks/PUT-tasks_id.test.js +++ b/test/api/v3/integration/tasks/PUT-tasks_id.test.js @@ -16,7 +16,7 @@ describe('PUT /tasks/:id', () => { let task; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', }).then((createdTask) => { @@ -65,7 +65,7 @@ describe('PUT /tasks/:id', () => { let habit; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test habit', type: 'habit', notes: 1976, @@ -93,7 +93,7 @@ describe('PUT /tasks/:id', () => { let todo; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test todo', type: 'todo', notes: 1976, @@ -150,7 +150,7 @@ describe('PUT /tasks/:id', () => { let daily; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test daily', type: 'daily', notes: 1976, @@ -254,7 +254,7 @@ describe('PUT /tasks/:id', () => { let reward; beforeEach(() => { - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { text: 'test reward', type: 'reward', notes: 1976, diff --git a/test/api/v3/integration/tasks/checklists/DELETE-tasks_taskId_checklist_itemId.test.js b/test/api/v3/integration/tasks/checklists/DELETE-tasks_taskId_checklist_itemId.test.js index d013878a74..738255996a 100644 --- a/test/api/v3/integration/tasks/checklists/DELETE-tasks_taskId_checklist_itemId.test.js +++ b/test/api/v3/integration/tasks/checklists/DELETE-tasks_taskId_checklist_itemId.test.js @@ -16,7 +16,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => { it('deletes a checklist item', () => { let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'Daily with checklist', }).then(createdTask => { @@ -33,7 +33,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => { it('does not work with habits', () => { let habit; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'habit with checklist', }).then(createdTask => { @@ -47,7 +47,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => { }); it('does not work with rewards', async () => { - let reward = await user.post('/tasks', { + let reward = await user.post('/tasks?tasksOwner=user', { type: 'reward', text: 'reward with checklist', }); @@ -68,7 +68,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => { }); it('fails on checklist item not found', () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'daily with checklist', }).then(createdTask => { diff --git a/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist.test.js b/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist.test.js index e9b695effd..1ad4f7f58c 100644 --- a/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist.test.js +++ b/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist.test.js @@ -16,7 +16,7 @@ describe('POST /tasks/:taskId/checklist/', () => { it('adds a checklist item to a task', () => { let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'Daily with checklist', }).then(createdTask => { @@ -36,7 +36,7 @@ describe('POST /tasks/:taskId/checklist/', () => { it('does not add a checklist to habits', () => { let habit; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'habit with checklist', }).then(createdTask => { @@ -51,7 +51,7 @@ describe('POST /tasks/:taskId/checklist/', () => { it('does not add a checklist to rewards', () => { let reward; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'reward', text: 'reward with checklist', }).then(createdTask => { diff --git a/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist_itemId_score.test.js b/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist_itemId_score.test.js index 667ef41446..862b67b638 100644 --- a/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist_itemId_score.test.js +++ b/test/api/v3/integration/tasks/checklists/POST-tasks_taskId_checklist_itemId_score.test.js @@ -16,7 +16,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => { it('scores a checklist item', () => { let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'Daily with checklist', }).then(createdTask => { @@ -32,7 +32,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => { it('fails on habits', () => { let habit; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'habit with checklist', }).then(createdTask => { @@ -46,7 +46,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => { }); it('fails on rewards', async () => { - let reward = await user.post('/tasks', { + let reward = await user.post('/tasks?tasksOwner=user', { type: 'reward', text: 'reward with checklist', }); @@ -67,7 +67,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => { }); it('fails on checklist item not found', () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'daily with checklist', }).then(createdTask => { diff --git a/test/api/v3/integration/tasks/checklists/PUT-tasks_taskId_checklist_itemId.test.js b/test/api/v3/integration/tasks/checklists/PUT-tasks_taskId_checklist_itemId.test.js index 988574bbf8..ae6babf778 100644 --- a/test/api/v3/integration/tasks/checklists/PUT-tasks_taskId_checklist_itemId.test.js +++ b/test/api/v3/integration/tasks/checklists/PUT-tasks_taskId_checklist_itemId.test.js @@ -16,7 +16,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => { it('updates a checklist item', () => { let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'Daily with checklist', }).then(createdTask => { @@ -33,7 +33,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => { }); it('fails on habits', async () => { - let habit = await user.post('/tasks', { + let habit = await user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'habit with checklist', }); @@ -46,7 +46,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => { }); it('fails on rewards', async () => { - let reward = await user.post('/tasks', { + let reward = await user.post('/tasks?tasksOwner=user', { type: 'reward', text: 'reward with checklist', }); @@ -67,7 +67,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => { }); it('fails on checklist item not found', () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'daily', text: 'daily with checklist', }).then(createdTask => { diff --git a/test/api/v3/integration/tasks/tags/DELETE-tasks_taskId_tags_tagId.test.js b/test/api/v3/integration/tasks/tags/DELETE-tasks_taskId_tags_tagId.test.js index c01b71fa2c..27d57bb2f0 100644 --- a/test/api/v3/integration/tasks/tags/DELETE-tasks_taskId_tags_tagId.test.js +++ b/test/api/v3/integration/tasks/tags/DELETE-tasks_taskId_tags_tagId.test.js @@ -17,7 +17,7 @@ describe('DELETE /tasks/:taskId/tags/:tagId', () => { let tag; let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'Task with tag', }).then(createdTask => { @@ -35,7 +35,7 @@ describe('DELETE /tasks/:taskId/tags/:tagId', () => { }); it('only deletes existing tags', () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'Task with tag', }).then(createdTask => { diff --git a/test/api/v3/integration/tasks/tags/POST-tasks_taskId_tags_tagId.test.js b/test/api/v3/integration/tasks/tags/POST-tasks_taskId_tags_tagId.test.js index 6e4c2ba510..9887a5bedb 100644 --- a/test/api/v3/integration/tasks/tags/POST-tasks_taskId_tags_tagId.test.js +++ b/test/api/v3/integration/tasks/tags/POST-tasks_taskId_tags_tagId.test.js @@ -17,7 +17,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => { let tag; let task; - return user.post('/tasks', { + return user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'Task with tag', }).then(createdTask => { @@ -35,7 +35,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => { let tag; let task; - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'Task with tag', }).then(createdTask => { @@ -54,7 +54,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => { }); it('does not add a non existing tag to a task', () => { - return expect(user.post('/tasks', { + return expect(user.post('/tasks?tasksOwner=user', { type: 'habit', text: 'Task with tag', }).then((task) => { diff --git a/website/src/controllers/api-v3/tasks.js b/website/src/controllers/api-v3/tasks.js index 40425c27a3..6ea130e843 100644 --- a/website/src/controllers/api-v3/tasks.js +++ b/website/src/controllers/api-v3/tasks.js @@ -22,20 +22,26 @@ let api = {}; * @apiName CreateTask * @apiGroup Task * - * @apiParam {string="user","challenge"} tasksOwner Define if tasks will belong to the auhenticated user or to a challenge (specifying the "challengeId" parameter). - * @apiParam {UUID} challengeId Optional. If "tasksOwner" is "challenge" then specify the challenge id. + * @apiParam {string="user","challenge"} tasksOwner Query parameter to define if tasks will belong to the auhenticated user or to a challenge (specifying the "challengeId" parameter). + * @apiParam {UUID} challengeId Optional. Query parameter. If "tasksOwner" is "challenge" then specify the challenge id. * * @apiSuccess {Object|Array} task The newly created task(s) */ api.createTask = { method: 'POST', - url: '/tasks/:tasksOwner/:challengeId?', + url: '/tasks', middlewares: [authWithHeaders(), cron], async handler (req, res) { + req.checkQuery('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']); + req.checkQuery('challengeId', res.t('challengeIdRequired')).optional().isUUID(); + + let validationErrors = req.validationErrors(); + if (validationErrors) throw validationErrors; + let tasksData = Array.isArray(req.body) ? req.body : [req.body]; let user = res.locals.user; - let tasksOwner = req.params.tasksOwner; - let challengeId = req.params.challengeId; + let tasksOwner = req.query.tasksOwner; + let challengeId = req.query.challengeId; let challenge; if (tasksOwner === 'user' && challengeId) throw new BadRequest(res.t('userTasksNoChallengeId')); @@ -79,8 +85,13 @@ api.createTask = { toSave.unshift((challenge || user).save()); let tasks = await Q.all(toSave); - tasks.splice(0, 1); // remove the user/challenge - res.respond(201, tasks); + + if (tasks.length === 2) { + res.respond(201, tasks[1]); + } else { + tasks.splice(0, 1); // remove the user/challenge + res.respond(201, tasks); + } // If adding tasks to a challenge -> sync users if (challenge) challenge.addTasks(tasks); // TODO catch/log @@ -93,8 +104,8 @@ api.createTask = { * @apiName GetTasks * @apiGroup Task * - * @apiParam {string="user","challenge"} tasksOwner Url parameter to return tasks belonging to a challenge (specifying the "challengeId" parameter) or to the autheticated user. - * @apiParam {UUID} challengeId Optional. If "tasksOwner" is "challenge" then specify the challenge id. + * @apiParam {string="user","challenge"} tasksOwner Query parameter to return tasks belonging to a challenge (specifying the "challengeId" parameter) or to the autheticated user. + * @apiParam {UUID} challengeId Optional query parameter. If "tasksOwner" is "challenge" then required to specify the challenge id. * @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". Only valid whe "tasksOwner" is "user". * @@ -102,20 +113,19 @@ api.createTask = { */ api.getTasks = { method: 'GET', - url: '/tasks/:tasksOwner/:challengeId?', + url: '/tasks', middlewares: [authWithHeaders(), cron], async handler (req, res) { - req.checkParams('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']); - req.checkParams('challengeId', res.t('challengeIdRequired')).optional().isUUID(); - + req.checkQuery('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']); + req.checkQuery('challengeId', res.t('challengeIdRequired')).optional().isUUID(); req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(Tasks.tasksTypes); let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; let user = res.locals.user; - let tasksOwner = req.params.tasksOwner; - let challengeId = req.params.challengeId; + let tasksOwner = req.query.tasksOwner; + let challengeId = req.query.challengeId; let challenge; if (tasksOwner === 'user' && challengeId) throw new BadRequest(res.t('userTasksNoChallengeId'));