index all todos

This commit is contained in:
2018-06-07 14:45:56 +03:00
parent 6f34bd1cab
commit e995b6cdbf
2 changed files with 14 additions and 1 deletions

View File

@@ -13,7 +13,9 @@ router.get(
'/',
asyncHelper(async (req, res) => {
const { listId } = res.locals || req.body;
const todos = await Todo.find({ list: listId, user: req.user.id }).exec();
const todos = listId
? await Todo.find({ list: listId, user: req.user.id }).exec()
: await Todo.find({ user: req.user.id }).exec();
res.json({ success: true, data: todos.map(todo => todo.toJson()) });
}),
);

View File

@@ -49,6 +49,17 @@ describe('test todos', () => {
expect(response.body.success).toBeTruthy();
expect(response.body.data[0].text).toEqual('Todo1');
});
test('should index all todos', async () => {
const response = await request(server)
.get(`/api/todos`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', 'application/json; charset=utf-8');
expect(response.body.success).toBeTruthy();
expect(response.body.data[0].text).toEqual('Todo1');
});
test('should not index todos without authentication', async () => {
await request(server)
.get(`/api/lists/${list._id}/todos`)