adejs/tests/test-password.js

56 lines
1.5 KiB
JavaScript

const config = require('../settings/config.js');
require('app-module-path').addPath(config.BASE_DIR);
require('app-module-path').addPath(config.UTILS_DIR);
require('app-module-path').addPath(config.CONTROLLERS_DIR);
const model = require('model');
const User = require('auth/models');
const log = require('log');
model.reinitialize(() => {
log.debug('Database reinitialized');
let user = new User();
user.username = "toto"
user.email = "toto@tforgione.fr";
user.password = "tata";
user.resources = 23;
user.project_id = 42;
user.save((err, u) => {
if (err) {
log.dberror(err);
return;
}
log.debug('New user created');
// Test password
User.getById(1, (err, user) => {
if (err !== undefined) {
log.dberror("User is undefined");
console.log(err);
return;
}
if (model.PasswordField.testSync("tata", user.password)) {
log.debug("Password authentication succeed");
} else {
log.error("Password should have succeeded but failed");
}
if (!model.PasswordField.testSync("toto", user.password)) {
log.debug("Password authentication failed as it was supposed to");
} else {
log.error("Password should have failed but succeded");
}
process.exit(0);
});
});
});