mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Move task tests
This commit is contained in:
44
test/api/user/tasks/DELETE-tasks_id.test.js
Normal file
44
test/api/user/tasks/DELETE-tasks_id.test.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
} from '../../../helpers/api.helper';
|
||||
|
||||
describe('DELETE /user/tasks/:id', () => {
|
||||
let api, user, task;
|
||||
|
||||
beforeEach(() => {
|
||||
return generateUser().then((_user) => {
|
||||
user = _user;
|
||||
task = user.todos[0];
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes a task', () => {
|
||||
return expect(api.del(`/user/tasks/${task.id}`)
|
||||
.then((res) => {
|
||||
return api.get(`/user/tasks/${task.id}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'No task found.',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error if the task does not exist', () => {
|
||||
return expect(api.del('/user/tasks/task-that-does-not-exist'))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'Task not found.',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not delete another user\'s task', () => {
|
||||
return expect(generateUser().then((otherUser) => {
|
||||
let otherUsersTask = otherUser.todos[0];
|
||||
return api.del(`/user/tasks/${otherUsersTask.id}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'Task not found.',
|
||||
});
|
||||
});
|
||||
});
|
||||
27
test/api/user/tasks/GET-tasks.test.js
Normal file
27
test/api/user/tasks/GET-tasks.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
} from '../../../helpers/api.helper';
|
||||
|
||||
describe('GET /user/tasks/', () => {
|
||||
let api, user;
|
||||
|
||||
beforeEach(() => {
|
||||
return generateUser().then((_user) => {
|
||||
user = _user;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('gets all tasks', () => {
|
||||
return api.get(`/user/tasks/`).then((tasks) => {
|
||||
expect(tasks).to.be.an('array');
|
||||
expect(tasks.length).to.be.greaterThan(4);
|
||||
|
||||
let task = tasks[0];
|
||||
expect(task.id).to.exist;
|
||||
expect(task.type).to.exist;
|
||||
expect(task.text).to.exist;
|
||||
});
|
||||
});
|
||||
});
|
||||
44
test/api/user/tasks/GET-tasks_id.test.js
Normal file
44
test/api/user/tasks/GET-tasks_id.test.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
} from '../../../helpers/api.helper';
|
||||
|
||||
describe('GET /user/tasks/:id', () => {
|
||||
let api, user, task;
|
||||
|
||||
beforeEach(() => {
|
||||
return generateUser().then((_user) => {
|
||||
user = _user;
|
||||
task = user.todos[0];
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('gets a task', () => {
|
||||
return api.get(`/user/tasks/${task.id}`).then((foundTask) => {
|
||||
expect(foundTask.id).to.eql(task.id);
|
||||
expect(foundTask.text).to.eql(task.text);
|
||||
expect(foundTask.notes).to.eql(task.notes);
|
||||
expect(foundTask.value).to.eql(task.value);
|
||||
expect(foundTask.type).to.eql(task.type);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error if the task does not exist', () => {
|
||||
return expect(api.get('/user/tasks/task-that-does-not-exist'))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'No task found.',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not get another user\'s task', () => {
|
||||
return expect(generateUser().then((otherUser) => {
|
||||
let otherUsersTask = otherUser.todos[0];
|
||||
return api.get(`/user/tasks/${otherUsersTask.id}`);
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'No task found.',
|
||||
});
|
||||
});
|
||||
});
|
||||
70
test/api/user/tasks/POST-tasks.test.js
Normal file
70
test/api/user/tasks/POST-tasks.test.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
} from '../../../helpers/api.helper';
|
||||
|
||||
describe('POST /user/tasks', () => {
|
||||
|
||||
let api, user;
|
||||
|
||||
beforeEach(() => {
|
||||
return generateUser().then((_user) => {
|
||||
user = _user;
|
||||
api = requester(user);
|
||||
});
|
||||
});
|
||||
|
||||
it('creates a task', () => {
|
||||
return api.post('/user/tasks').then((task) => {
|
||||
expect(task.id).to.exist;
|
||||
});
|
||||
});
|
||||
|
||||
it('creates a habit by default', () => {
|
||||
return expect(api.post('/user/tasks'))
|
||||
.to.eventually.have.property('type', 'habit');
|
||||
});
|
||||
|
||||
it('creates a task with specified values', () => {
|
||||
return api.post('/user/tasks', {
|
||||
type: 'daily',
|
||||
text: 'My task',
|
||||
notes: 'My notes',
|
||||
frequency: 'daily',
|
||||
}).then((task) => {
|
||||
expect(task.type).to.eql('daily');
|
||||
expect(task.text).to.eql('My task');
|
||||
expect(task.notes).to.eql('My notes');
|
||||
expect(task.frequency).to.eql('daily');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not create a task with an id that already exists', () => {
|
||||
let todo = user.todos[0];
|
||||
|
||||
return expect(api.post('/user/tasks', {
|
||||
id: todo.id,
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 409,
|
||||
text: 'A task with that ID already exists.',
|
||||
});
|
||||
});
|
||||
|
||||
xit('TODO: no error is thrown - throws a 500 validation error if invalid type is posted', () => {
|
||||
return expect(api.post('/user/tasks', {
|
||||
type: 'not-valid',
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 500,
|
||||
text: 'Cannot call method \'indexOf\' of undefined',
|
||||
});
|
||||
});
|
||||
|
||||
xit('TODO: no error is thrown - throws a 500 validation error if invalid data is posted', () => {
|
||||
return expect(api.post('/user/tasks', {
|
||||
frequency: 'not-valid',
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 500,
|
||||
text: 'Task validation failed',
|
||||
});
|
||||
});
|
||||
});
|
||||
70
test/api/user/tasks/PUT-tasks_id.test.js
Normal file
70
test/api/user/tasks/PUT-tasks_id.test.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
generateUser,
|
||||
requester,
|
||||
} from '../../../helpers/api.helper';
|
||||
|
||||
describe('PUT /user/tasks/:id', () => {
|
||||
let api, user, task;
|
||||
|
||||
beforeEach(() => {
|
||||
return generateUser().then((_user) => {
|
||||
user = _user;
|
||||
api = requester(user);
|
||||
task = user.todos[0];
|
||||
});
|
||||
});
|
||||
|
||||
it('does not update the id of the task', () => {
|
||||
return api.put(`/user/tasks/${task.id}`, {
|
||||
id: 'some-thing',
|
||||
}).then((updatedTask) => {
|
||||
expect(updatedTask.id).to.eql(task.id);
|
||||
expect(updatedTask.id).to.not.eql('some-thing');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not update the type of the task', () => {
|
||||
return api.put(`/user/tasks/${task.id}`, {
|
||||
type: 'habit',
|
||||
}).then((updatedTask) => {
|
||||
expect(updatedTask.type).to.eql(task.type);
|
||||
expect(updatedTask.type).to.not.eql('habit');
|
||||
});
|
||||
});
|
||||
|
||||
it('updates text, attribute, priority, value and notes', () => {
|
||||
return api.put(`/user/tasks/${task.id}`, {
|
||||
text: 'new text',
|
||||
notes: 'new notes',
|
||||
value: 10000,
|
||||
priority: .5,
|
||||
attribute: 'str',
|
||||
}).then((updatedTask) => {
|
||||
expect(updatedTask.text).to.eql('new text');
|
||||
expect(updatedTask.notes).to.eql('new notes');
|
||||
expect(updatedTask.value).to.eql(10000);
|
||||
expect(updatedTask.priority).to.eql(.5);
|
||||
expect(updatedTask.attribute).to.eql('str');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error if the task does not exist', () => {
|
||||
return expect(api.put('/user/tasks/task-id-that-does-not-exist'))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'Task not found.',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not update another user\'s task', () => {
|
||||
return expect(generateUser().then((otherUser) => {
|
||||
let otherUsersTask = otherUser.todos[0];
|
||||
return api.put(`/user/tasks/${otherUsersTask._id}`, {
|
||||
name: 'some name',
|
||||
});
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
text: 'Task not found.',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user