- Created first Test with Mocha

This commit is contained in:
Paolo Arena
2019-02-06 18:48:32 +01:00
parent db2a460594
commit 779bd1cb63
13 changed files with 705 additions and 114 deletions

94
server/tests/seed/seed.js Normal file
View File

@@ -0,0 +1,94 @@
const { ObjectID } = require('mongodb');
const jwt = require('jsonwebtoken');
const { Todo } = require('./../../models/todo');
const { User } = require('./../../models/user');
var bcrypt = require('bcrypt');
const userOneId = new ObjectID();
const userTwoId = 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 mypwdcrypted = '$2b$12$mteST.isuWO0SNsfeZ0aCe.Dz3qwPh5SU8t9rc5SaPTkb3j0ywGv6'
const users = [ {
_id: userTwoId,
keyappid: process.env.KEY_APP_ID,
lang: 'it',
idapp: '1',
email: 'paoloa.rena77@gmail.com',
username: 'paoloar77B',
password: mypwdcrypted,
tokens: [{
access: 'auth',
token: jwt.sign({ _id: userOneId, access: 'auth' }, process.env.SIGNCODE).toString()
}]
},
{
_id: userThreeId,
keyappid: process.env.KEY_APP_ID,
lang: 'it',
idapp: '1',
email: 'pa@com',
password: mypwdcrypted,
username: 'paoloar77C',
tokens: [{
access: 'auth',
token: jwt.sign({ _id: userTwoId, access: 'auth' }, process.env.SIGNCODE).toString()
}]
}, {
keyappid: process.env.KEY_APP_ID,
lang: 'it',
idapp: '1',
email: 'pao.loarena77@gmail.com',
password: mypwdcrypted,
username: 'paoloar77A'
}];
const userjson = JSON.stringify(users[0]);
// console.log('userjson=', userjson)
const todos = [{
_id: new ObjectID(),
text: 'First test todo',
_creator: userOneId
}, {
_id: new ObjectID(),
text: 'Second test todo',
completed: true,
completedAt: 333,
_creator: userTwoId
}];
const populateTodos = (done) => {
const lista = [ users[0]._id, users[1]._id, users[2]._id];
Todo.deleteMany({ userId: {$in: lista } })
.then(() => {
return Todo.insertMany(todos);
}).then(() => done())
};
const populateUsers = (done) => {
const lista = [ users[0].username, users[1].username, users[2].username];
// const lista = [ "aa"]
User.deleteMany({ username: {$in: lista } })
.then(() => {
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 };