- Created all Tests with Mocha: User + Todo tables

This commit is contained in:
Paolo Arena
2019-02-07 00:52:48 +01:00
parent 779bd1cb63
commit 87378fe732
10 changed files with 309 additions and 150 deletions

View File

@@ -5,7 +5,9 @@ const { ObjectID } = require('mongodb');
const { app } = require('./../server');
const { Todo } = require('./../models/todo');
const { User } = require('./../models/user');
const { todos, populateTodos, users, populateUsers, userjson } = require('./seed/seed');
const { todos, populateTodos, users, populateUsers, userjson, mypwdcrypted, mypwdchiaro } = require('./seed/seed');
const tools = require('../tools/general');
// const { debug } = require('debug');
// const log = debug('server');
@@ -23,6 +25,8 @@ beforeEach(populateUsers);
beforeEach(populateTodos);
const IndexUserToCreate = 2;
const IndexTodoToCreate = 3;
// console.log('UserOne:', users[0]);
// console.log('UserTwo:', users[0]);
@@ -98,7 +102,7 @@ describe('POST /users/login', () => {
.set('x-auth', users[0].tokens[0].token)
.send({
username: users[0].username,
password: users[0].password,
password: mypwdchiaro,
idapp: users[0].idapp,
keyappid: users[0].keyappid,
lang: users[0].lang,
@@ -121,13 +125,13 @@ describe('POST /users/login', () => {
}).catch((e) => done(e));
});
});
/*
it('should reject invalid login', (done) => {
request(app)
.post('/users/login')
.send({
email: users[1].email,
password: users[1].password + '1'
username: users[0].username,
password: mypwdchiaro + '1'
})
.expect(400)
.expect((res) => {
@@ -144,42 +148,78 @@ describe('POST /users/login', () => {
}).catch((e) => done(e));
});
});
*/
});
describe('POST /todos', () => {
/*
it('should create a new Todos', (done) => {
describe('DELETE /users/me/token', () => {
it('should logout user deleting auth token', (done) => {
request(app)
.post('/todos')
.delete('/users/me/token')
.set('x-auth', users[0].tokens[0].token)
.send(users[0])
.send()
.expect(200)
.expect((res) => {
expect(res.body.text).toBe(text);
expect(res.headers['x-auth']).toNotExist();
})
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find({ text }).then((todos) => {
expect(todos.length).toBe(1);
expect(todos[0].text).toBe(text);
done();
});
});
it('should return 401 deleting an invalid token', (done) => {
request(app)
.delete('/users/me/token')
.set('x-auth', users[0].tokens[0].token + '1')
.send()
.expect(401)
.end((err, res) => {
if (err) {
return done(err);
}
done();
});
});
});
describe('POST /todos', () => {
it('should create a new Todos', (done) => {
request(app)
.post('/todos')
.set('x-auth', users[0].tokens[0].token)
.send(todos[IndexTodoToCreate])
.expect(200)
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find({ descr: todos[IndexTodoToCreate].descr }).then((arr_todos) => {
expect(arr_todos.length).toBe(1);
expect(arr_todos[0].descr).toBe(todos[IndexTodoToCreate].descr);
expect(String(arr_todos[0]._id)).toBe(String(todos[IndexTodoToCreate]._id));
expect(String(arr_todos[0].userId)).toBe(String(users[0]._id));
done();
}).catch((e) => done(e));
});
});
*/
})
/*
it('should return 404 if not authenticated', (done) => {
request(app)
.post('/todos')
.set('x-auth', users[0].tokens[0].token)
.send(todos[IndexTodoToCreate])
.expect(404)
.end((err) => {
done();
});
});
it('should not create todo with invalid body data', (done) => {
request(app)
@@ -193,7 +233,6 @@ describe('POST /todos', () => {
}
Todo.find().then((todos) => {
expect(todos.length).toBe(2);
done();
}).catch((e) => done(e));
});
@@ -203,43 +242,50 @@ describe('POST /todos', () => {
describe('GET /todos', () => {
it('should get all todos', (done) => {
request(app)
.get('/todos')
.get(`/todos/${users[0]._id }`)
.set('x-auth', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.todos.length).toBe(1);
expect(res.body.todos.length).toBe(2);
})
.end(done);
});
});
describe('GET /todos/:id', () => {
it('should return todo doc', (done) => {
it('should return todos of the User', (done) => {
request(app)
.get(`/todos/${todos[0]._id.toHexString()}`)
.get(`/todos/${todos[0].userId}`)
.set('x-auth', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.todo.text).toBe(todos[0].text);
let miores = res
let miadescr = miores.body.todos[0].descr
expect(res.body.todos.length).toBe(2);
expect(miadescr).toBe(todos[0].descr);
})
.end(done);
});
it('should not return todo doc created by other user', (done) => {
request(app)
.get(`/todos/${todos[1]._id.toHexString()}`)
.get(`/todos/${todos[2].userId}`)
.set('x-auth', users[0].tokens[0].token)
.expect(404)
.end(done);
});
it('should return 404 if todo not found', (done) => {
it('should return [] if user not found', (done) => {
var hexId = new ObjectID().toHexString();
request(app)
.get(`/todos/${hexId}`)
.get(`/todos/${users[0]._id + '111'}`)
.set('x-auth', users[0].tokens[0].token)
.expect(404)
.expect((res) => {
expect(res.body.todos).toBe(undefined);
})
.end(done);
});
@@ -254,11 +300,11 @@ describe('GET /todos/:id', () => {
describe('DELETE /todos/:id', () => {
it('should remove a todo', (done) => {
var hexId = todos[1]._id.toHexString();
var hexId = todos[0]._id.toHexString();
request(app)
.delete(`/todos/${hexId}`)
.set('x-auth', users[1].tokens[0].token)
.set('x-auth', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.todo._id).toBe(hexId);
@@ -275,31 +321,12 @@ describe('DELETE /todos/:id', () => {
});
});
it('should remove a todo', (done) => {
var hexId = todos[0]._id.toHexString();
request(app)
.delete(`/todos/${hexId}`)
.set('x-auth', users[1].tokens[0].token)
.expect(404)
.end((err, res) => {
if (err) {
return done(err);
}
Todo.findById(hexId).then((todo) => {
expect(todo).toExist();
done();
}).catch((e) => done(e));
});
});
it('should return 404 if todo not found', (done) => {
var hexId = new ObjectID().toHexString();
request(app)
.delete(`/todos/${hexId}`)
.set('x-auth', users[1].tokens[0].token)
.set('x-auth', users[0].tokens[0].token)
.expect(404)
.end(done);
});
@@ -307,7 +334,7 @@ describe('DELETE /todos/:id', () => {
it('should return 404 if object id is invalid', (done) => {
request(app)
.delete('/todos/123abc')
.set('x-auth', users[1].tokens[0].token)
.set('x-auth', users[0].tokens[0].token)
.expect(404)
.end(done);
});
@@ -316,60 +343,68 @@ describe('DELETE /todos/:id', () => {
describe('PATCH /todos/:id', () => {
it('should update the todo', (done) => {
var hexId = todos[0]._id.toHexString();
var text = 'This should be the new text';
var descr = 'This should be the new text';
request(app)
.patch(`/todos/${hexId}`)
.set('x-auth', users[0].tokens[0].token)
.send({
completed: true,
text
descr
})
.expect(200)
.expect((res) => {
expect(res.body.todo.text).toBe(text);
expect(res.body.todo.descr).toBe(descr);
expect(res.body.todo.completed).toBe(true);
expect(res.body.todo.completedAt).toBeA('number');
// expect(res.body.todo.completedAt).toBeA('number');
})
.end(done);
});
it('should not update the todo created by other user', (done) => {
var hexId = todos[0]._id.toHexString();
var text = 'This should be the new text';
var descr = 'This should be the new text';
request(app)
.patch(`/todos/${hexId}`)
.set('x-auth', users[1].tokens[0].token)
.send({
completed: true,
text
descr
})
.expect(404)
.end(done);
});
it('should clear completedAt when todo is not completed', (done) => {
var hexId = todos[1]._id.toHexString();
var text = 'This should be the new text!!';
request(app)
.patch(`/todos/${hexId}`)
.set('x-auth', users[1].tokens[0].token)
.send({
completed: false,
text
})
.expect(200)
.expect((res) => {
expect(res.body.todo.text).toBe(text);
expect(res.body.todo.completed).toBe(false);
expect(res.body.todo.completedAt).toNotExist();
})
.end(done);
});
// it('should clear completedAt when todo is not completed', (done) => {
// var hexId = todos[1]._id.toHexString();
// var text = 'This should be the new text!!';
//
// request(app)
// .patch(`/todos/${hexId}`)
// .set('x-auth', users[1].tokens[0].token)
// .send({
// completed: false,
// text
// })
// .expect(200)
// .expect((res) => {
// expect(res.body.todo.text).toBe(text);
// expect(res.body.todo.completed).toBe(false);
// expect(res.body.todo.completedAt).toNotExist();
// })
// .end(done);
// });
});
/*
});
describe('GET /users/me', () => {
it('should return user if authenticated', (done) => {
request(app)
@@ -383,10 +418,10 @@ describe('GET /users/me', () => {
.end(done);
});
it('should return 401 if not authenticated', (done) => {
it('should return 404 if not authenticated', (done) => {
request(app)
.get('/users/me')
.expect(401)
.expect(404)
.expect((res) => {
expect(res.body).toEqual({});
})