mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
Move task tests
This commit is contained in:
@@ -80,106 +80,3 @@ describe "Todos", ->
|
|||||||
# Expect todos to be 2 less than the total count
|
# Expect todos to be 2 less than the total count
|
||||||
expect(_.size(res.body.todos)).to.equal numTasks - 2
|
expect(_.size(res.body.todos)).to.equal numTasks - 2
|
||||||
done()
|
done()
|
||||||
|
|
||||||
describe "Creating, Updating, Deleting Todos", ->
|
|
||||||
todo = undefined
|
|
||||||
updateTodo = undefined
|
|
||||||
describe "Creating todos", ->
|
|
||||||
it "Creates a todo", (done) ->
|
|
||||||
request.post(baseURL + "/user/tasks").send(
|
|
||||||
type: "todo"
|
|
||||||
text: "Sample Todo"
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 200
|
|
||||||
todo = res.body
|
|
||||||
expect(todo.text).to.equal "Sample Todo"
|
|
||||||
expect(todo.id).to.be.ok
|
|
||||||
expect(todo.value).to.equal 0
|
|
||||||
done()
|
|
||||||
|
|
||||||
it "Does not create a todo with an id that already exists", (done) ->
|
|
||||||
original_todo = {
|
|
||||||
type: "todo"
|
|
||||||
text: "original todo"
|
|
||||||
id: "custom-id"
|
|
||||||
}
|
|
||||||
duplicate_id_todo = {
|
|
||||||
type: "todo"
|
|
||||||
text: "not original todo"
|
|
||||||
id: "custom-id"
|
|
||||||
}
|
|
||||||
request.post(baseURL + "/user/tasks").send(
|
|
||||||
original_todo
|
|
||||||
).end (err, res) ->
|
|
||||||
request.post(baseURL + "/user/tasks").send(
|
|
||||||
duplicate_id_todo
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 409
|
|
||||||
expect(res.body.err).to.eql('A task with that ID already exists.')
|
|
||||||
done()
|
|
||||||
|
|
||||||
describe "Updating todos", ->
|
|
||||||
it "Does not update id of todo", (done) ->
|
|
||||||
request.put(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
id: "a-new-id"
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 200
|
|
||||||
updateTodo = res.body
|
|
||||||
expect(updateTodo.id).to.equal todo.id
|
|
||||||
done()
|
|
||||||
|
|
||||||
it "Does not update type of todo", (done) ->
|
|
||||||
request.put(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
type: "habit"
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 200
|
|
||||||
updateTodo = res.body
|
|
||||||
expect(updateTodo.type).to.equal todo.type
|
|
||||||
done()
|
|
||||||
|
|
||||||
it "Does update text, attribute, priority, value, notes", (done) ->
|
|
||||||
request.put(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
text: "Changed Title"
|
|
||||||
attribute: "int"
|
|
||||||
priority: 1.5
|
|
||||||
value: 5
|
|
||||||
notes: "Some notes"
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 200
|
|
||||||
todo = res.body
|
|
||||||
expect(todo.text).to.equal "Changed Title"
|
|
||||||
expect(todo.attribute).to.equal "int"
|
|
||||||
expect(todo.priority).to.equal 1.5
|
|
||||||
expect(todo.value).to.equal 5
|
|
||||||
expect(todo.notes).to.equal "Some notes"
|
|
||||||
done()
|
|
||||||
|
|
||||||
describe "Deleting todos", ->
|
|
||||||
it "Does delete todo", (done) ->
|
|
||||||
request.del(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 200
|
|
||||||
body = res.body
|
|
||||||
expect(body).to.be.empty
|
|
||||||
done()
|
|
||||||
|
|
||||||
it "Does not delete already deleted todo", (done) ->
|
|
||||||
request.del(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 404
|
|
||||||
body = res.body
|
|
||||||
expect(body.err).to.equal "Task not found."
|
|
||||||
done()
|
|
||||||
|
|
||||||
it "Does not update text, attribute, priority, value, notes if task is already deleted", (done) ->
|
|
||||||
request.put(baseURL + "/user/tasks/" + todo.id).send(
|
|
||||||
text: "New Title"
|
|
||||||
attribute: "str"
|
|
||||||
priority: 1
|
|
||||||
value: 4
|
|
||||||
notes: "Other notes"
|
|
||||||
).end (err, res) ->
|
|
||||||
expectCode res, 404
|
|
||||||
body = res.body
|
|
||||||
expect(body.err).to.equal "Task not found."
|
|
||||||
done()
|
|
||||||
|
|||||||
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