Improved tests and fixed them for new users model

This commit is contained in:
Wyatt Johnson
2016-11-09 16:26:23 -07:00
parent ef22da5f29
commit c203bf16eb
6 changed files with 206 additions and 112 deletions
+43 -4
View File
@@ -8,14 +8,21 @@ const UserSchema = new mongoose.Schema({
id: {
type: String,
default: uuid.v4,
unique: true
unique: true,
required: true
},
displayName: String,
disabled: Boolean,
password: String,
profiles: [{
id: String,
provider: String
id: {
type: String,
required: true
},
provider: {
type: String,
required: true
}
}],
roles: [String]
});
@@ -163,6 +170,18 @@ UserSchema.statics.changePassword = function(id, password) {
});
};
/**
* Creates local users.
* @param {Array} users Users to create
* @return {Promise} Resolves with the users that were created
*/
UserSchema.statics.createLocalUsers = function(users) {
return Promise.all(users.map((user) => {
return User
.createLocalUser(user.email, user.password, user.displayName);
}));
};
/**
* Creates the local user with a given email, password, and name.
* @param {String} email email of the new user
@@ -171,6 +190,18 @@ UserSchema.statics.changePassword = function(id, password) {
* @param {Function} done callback
*/
UserSchema.statics.createLocalUser = function(email, password, displayName) {
if (!email) {
return Promise.reject('email is required');
}
if (!password) {
return Promise.reject('password is required');
}
if (!displayName) {
return Promise.reject('displayName is required');
}
return new Promise((resolve, reject) => {
bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => {
if (err) {
@@ -262,9 +293,17 @@ UserSchema.statics.removeRoleFromUser = function(id, role) {
});
};
/**
* Finds a user with the id.
* @param {String} id user id (uuid)
*/
UserSchema.statics.findById = function(id) {
return User.findOne({id});
};
/**
* Finds users in an array of idd.
* @param {String} idd array of user identifiers (uuid)
* @param {Array} ids array of user identifiers (uuid)
*/
UserSchema.statics.findByIdArray = function(ids) {
return User.find({