- Created all Tests with Mocha: User + Todo tables
This commit is contained in:
@@ -6,21 +6,26 @@ const { User } = require('./../../models/user');
|
||||
|
||||
var bcrypt = require('bcrypt');
|
||||
|
||||
const tools = require('../../tools/general');
|
||||
|
||||
let myuserIdOne = '';
|
||||
|
||||
const userOneId = new ObjectID();
|
||||
const userTwoId = new ObjectID();
|
||||
const userThreeId = new ObjectID();
|
||||
// const userThreeId = new ObjectID();
|
||||
|
||||
// let mypwdchiaro = 'mypassword@1A';
|
||||
// let mypwdcrypt = bcrypt.hash(mypwdchiaro, bcrypt.genSaltSync(12))
|
||||
// .then((hashedPassword) => {
|
||||
// console.log('pwd=',hashedPassword);
|
||||
// });
|
||||
// String(mypwdcrypt)
|
||||
|
||||
|
||||
let mypwdchiaro = 'mypassword@1A';
|
||||
let mypwdcrypted = '$2b$12$mteST.isuWO0SNsfeZ0aCe.Dz3qwPh5SU8t9rc5SaPTkb3j0ywGv6'
|
||||
|
||||
const users = [ {
|
||||
_id: userTwoId,
|
||||
const users = [{
|
||||
_id: userOneId,
|
||||
keyappid: process.env.KEY_APP_ID,
|
||||
lang: 'it',
|
||||
idapp: '1',
|
||||
@@ -33,7 +38,7 @@ const users = [ {
|
||||
}]
|
||||
},
|
||||
{
|
||||
_id: userThreeId,
|
||||
_id: userTwoId,
|
||||
keyappid: process.env.KEY_APP_ID,
|
||||
lang: 'it',
|
||||
idapp: '1',
|
||||
@@ -45,7 +50,7 @@ const users = [ {
|
||||
token: jwt.sign({ _id: userTwoId, access: 'auth' }, process.env.SIGNCODE).toString()
|
||||
}]
|
||||
}, {
|
||||
keyappid: process.env.KEY_APP_ID,
|
||||
keyappid: process.env.KEY_APP_ID, // RECORD CHE VERRA' UTILIZZATO PER UNA NUOVA REGISTRAZIONE
|
||||
lang: 'it',
|
||||
idapp: '1',
|
||||
email: 'pao.loarena77@gmail.com',
|
||||
@@ -60,35 +65,106 @@ const userjson = JSON.stringify(users[0]);
|
||||
|
||||
const todos = [{
|
||||
_id: new ObjectID(),
|
||||
text: 'First test todo',
|
||||
_creator: userOneId
|
||||
category: "personal",
|
||||
completed: false,
|
||||
completed_at: 0,
|
||||
created_at: 6,
|
||||
descr: "Primo Task Esempio",
|
||||
enableExpiring: false,
|
||||
expiring_at: 16,
|
||||
id_next: "10000000",
|
||||
id_prev: "0",
|
||||
modified: false,
|
||||
modify_at: 6,
|
||||
pos: 1,
|
||||
priority: 1,
|
||||
progress: 0,
|
||||
userId: users[0]._id
|
||||
}, {
|
||||
_id: new ObjectID(),
|
||||
text: 'Second test todo',
|
||||
completed: true,
|
||||
completedAt: 333,
|
||||
_creator: userTwoId
|
||||
category: "personal",
|
||||
completed: false,
|
||||
completed_at: 0,
|
||||
created_at: 6,
|
||||
descr: "Secondo Task Esempio",
|
||||
enableExpiring: false,
|
||||
expiring_at: 16,
|
||||
id_next: "10000000",
|
||||
id_prev: "1",
|
||||
modified: false,
|
||||
modify_at: 6,
|
||||
pos: 2,
|
||||
priority: 1,
|
||||
progress: 0,
|
||||
userId: users[0]._id
|
||||
}, {
|
||||
_id: new ObjectID(),
|
||||
category: "personal",
|
||||
completed: false,
|
||||
completed_at: 0,
|
||||
created_at: 6,
|
||||
descr: "Terzo Task Esempio",
|
||||
enableExpiring: false,
|
||||
expiring_at: 16,
|
||||
id_next: "10000000",
|
||||
id_prev: "1",
|
||||
modified: false,
|
||||
modify_at: 6,
|
||||
pos: 3,
|
||||
priority: 1,
|
||||
progress: 0,
|
||||
userId: users[1]._id
|
||||
}, { // RECORD CHE VERRA' UTILIZZATO PER AGGIUNGERE UN NUOVO TASK
|
||||
_id: new ObjectID(),
|
||||
category: "personal",
|
||||
completed: false,
|
||||
completed_at: 0,
|
||||
created_at: 6,
|
||||
descr: "Nuovo Quarto Task Esempio da Inserire",
|
||||
enableExpiring: false,
|
||||
expiring_at: 16,
|
||||
id_next: "10000000",
|
||||
id_prev: "2",
|
||||
modified: false,
|
||||
modify_at: 6,
|
||||
pos: 4,
|
||||
priority: 1,
|
||||
progress: 0,
|
||||
userId: users[0]._id
|
||||
}];
|
||||
|
||||
const populateTodos = (done) => {
|
||||
const lista = [ users[0]._id, users[1]._id, users[2]._id];
|
||||
Todo.deleteMany({ userId: {$in: lista } })
|
||||
|
||||
Todo.deleteMany({})
|
||||
.then(() => {
|
||||
return Todo.insertMany(todos);
|
||||
}).then(() => done())
|
||||
|
||||
var TodoOne = new Todo(todos[0]).save();
|
||||
var TodoTwo = new Todo(todos[1]).save();
|
||||
|
||||
return Promise.all([TodoOne, TodoTwo])
|
||||
}).then(() => {
|
||||
done()
|
||||
|
||||
// tools.mylogshow('todos[0]', todos[0]._id);
|
||||
// tools.mylogshow('todos[1]', todos[1]._id);
|
||||
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
const populateUsers = (done) => {
|
||||
const lista = [ users[0].username, users[1].username, users[2].username];
|
||||
// const lista = [ "aa"]
|
||||
User.deleteMany({ username: {$in: lista } })
|
||||
|
||||
User.deleteMany({})
|
||||
.then(() => {
|
||||
|
||||
// console.log('users[0]', users[0])
|
||||
var userOne = new User(users[0]).save();
|
||||
var userTwo = new User(users[1]).save();
|
||||
|
||||
return Promise.all([userOne, userTwo])
|
||||
}).then(() => done());
|
||||
|
||||
};
|
||||
|
||||
module.exports = { todos, populateTodos, users, populateUsers, userjson, mypwdcrypted };
|
||||
module.exports = { todos, populateTodos, users, populateUsers, userjson, mypwdcrypted, mypwdchiaro };
|
||||
|
||||
@@ -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({});
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user