mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Added initial checklist item update tests
This commit is contained in:
@@ -0,0 +1,155 @@
|
|||||||
|
import {
|
||||||
|
generateUser,
|
||||||
|
generateGroup,
|
||||||
|
generateChallenge,
|
||||||
|
translate as t,
|
||||||
|
} from '../../../../../helpers/api-integration/v3';
|
||||||
|
import { v4 as generateUUID } from 'uuid';
|
||||||
|
|
||||||
|
describe('PUT /tasks/:taskId/checklist/:itemId', () => {
|
||||||
|
let user;
|
||||||
|
let guild;
|
||||||
|
let challenge;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
user = await generateUser();
|
||||||
|
guild = await generateGroup(user);
|
||||||
|
challenge = await generateChallenge(user, guild);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails on task not found', async () => {
|
||||||
|
let task = await user.post(`/tasks/challenge/${challenge._id}`, {
|
||||||
|
type: 'todo',
|
||||||
|
text: 'Todo with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(user.put(`/tasks/${task._id}/checklist/${generateUUID()}`, {
|
||||||
|
text: 'updated',
|
||||||
|
completed: true,
|
||||||
|
_id: 123, // ignored
|
||||||
|
}))
|
||||||
|
.to.eventually.be.rejected.and.eql({
|
||||||
|
code: 404,
|
||||||
|
error: 'NotFound',
|
||||||
|
message: t('checklistItemNotFound'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns error when user is not a member of the challenge', async () => {
|
||||||
|
let task = await user.post(`/tasks/challenge/${challenge._id}`, {
|
||||||
|
type: 'todo',
|
||||||
|
text: 'Todo with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
|
||||||
|
text: 'Checklist Item 1',
|
||||||
|
completed: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
let anotherUser = await generateUser();
|
||||||
|
|
||||||
|
await expect(anotherUser.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {
|
||||||
|
text: 'updated',
|
||||||
|
completed: true,
|
||||||
|
_id: 123, // ignored
|
||||||
|
}))
|
||||||
|
.to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('onlyChalLeaderEditTasks'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates a checklist item on dailies', async () => {
|
||||||
|
let task = await user.post(`/tasks/challenge/${challenge._id}`, {
|
||||||
|
type: 'daily',
|
||||||
|
text: 'Daily with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
|
||||||
|
text: 'Checklist Item 1',
|
||||||
|
completed: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
savedTask = await user.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {
|
||||||
|
text: 'updated',
|
||||||
|
completed: true,
|
||||||
|
_id: 123, // ignored
|
||||||
|
});
|
||||||
|
|
||||||
|
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('updates a checklist item on todos', async () => {
|
||||||
|
let task = await user.post(`/tasks/challenge/${challenge._id}`, {
|
||||||
|
type: 'todo',
|
||||||
|
text: 'Todo with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
|
||||||
|
text: 'Checklist Item 1',
|
||||||
|
completed: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
savedTask = await user.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {
|
||||||
|
text: 'updated',
|
||||||
|
completed: true,
|
||||||
|
_id: 123, // ignored
|
||||||
|
});
|
||||||
|
|
||||||
|
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', async () => {
|
||||||
|
let habit = await user.post('/tasks/user', {
|
||||||
|
type: 'habit',
|
||||||
|
text: 'habit with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(user.put(`/tasks/${habit._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 400,
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: t('checklistOnlyDailyTodo'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails on rewards', async () => {
|
||||||
|
let reward = await user.post('/tasks/user', {
|
||||||
|
type: 'reward',
|
||||||
|
text: 'reward with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(user.put(`/tasks/${reward._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 400,
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: t('checklistOnlyDailyTodo'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails on task not found', async () => {
|
||||||
|
await expect(user.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', async () => {
|
||||||
|
let createdTask = await user.post('/tasks/user', {
|
||||||
|
type: 'daily',
|
||||||
|
text: 'daily with checklist',
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(user.put(`/tasks/${createdTask._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 404,
|
||||||
|
error: 'NotFound',
|
||||||
|
message: t('checklistItemNotFound'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -561,7 +561,7 @@ api.addChecklistItem = {
|
|||||||
if (!task) {
|
if (!task) {
|
||||||
throw new NotFound(res.t('taskNotFound'));
|
throw new NotFound(res.t('taskNotFound'));
|
||||||
} else if (!task.userId) { // If the task belongs to a challenge make sure the user has rights
|
} else if (!task.userId) { // If the task belongs to a challenge make sure the user has rights
|
||||||
challenge = await Challenge.find().selec({_id: task.challenge.id}).select('leader').exec();
|
challenge = await Challenge.findOne({_id: task.challenge.id}).exec();
|
||||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||||
if (challenge.leader !== user._id) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
if (challenge.leader !== user._id) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||||
} else if (task.userId !== user._id) { // If the task is owned by an user make it's the current one
|
} else if (task.userId !== user._id) { // If the task is owned by an user make it's the current one
|
||||||
@@ -652,7 +652,7 @@ api.updateChecklistItem = {
|
|||||||
if (!task) {
|
if (!task) {
|
||||||
throw new NotFound(res.t('taskNotFound'));
|
throw new NotFound(res.t('taskNotFound'));
|
||||||
} else if (!task.userId) { // If the task belongs to a challenge make sure the user has rights
|
} else if (!task.userId) { // If the task belongs to a challenge make sure the user has rights
|
||||||
challenge = await Challenge.find().selec({_id: task.challenge.id}).select('leader').exec();
|
challenge = await Challenge.findOne({_id: task.challenge.id}).exec();
|
||||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||||
if (challenge.leader !== user._id) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
if (challenge.leader !== user._id) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||||
} else if (task.userId !== user._id) { // If the task is owned by an user make it's the current one
|
} else if (task.userId !== user._id) { // If the task is owned by an user make it's the current one
|
||||||
|
|||||||
Reference in New Issue
Block a user