Skip to content

Instantly share code, notes, and snippets.

@FrancescaK
Created October 4, 2012 10:32
Show Gist options
  • Select an option

  • Save FrancescaK/3832812 to your computer and use it in GitHub Desktop.

Select an option

Save FrancescaK/3832812 to your computer and use it in GitHub Desktop.
testing passwords
var mongoose = require(mongoose),
User = require(./user-model);
var connStr = mongodb://localhost:27017/mongoose-bcrypt-test;
mongoose.connect(connStr, function(err) {
if (err) throw err;
console.log(Successfully connected to MongoDB);
});
// create a user a new user
var testUser = new User({
username: jmar777,
password: Password;
});
// save user to database
testUser.save(function(err) {
if (err) throw err;
// fetch user and test password verification
User.findOne({ username: 'jmar777' }, function(err, user) {
if (err) throw err;
// test a matching password
user.comparePassword('Password123', function(err, isMatch) {
if (err) throw err;
console.log('Password123:', isMatch); // -> Password123: true
});
// test a failing password
user.comparePassword('123Password', function(err, isMatch) {
if (err) throw err;
console.log('123Password:', isMatch); // -> 123Password: false
});
});
@erip
Copy link
Copy Markdown

erip commented Jun 16, 2016

There's a bug in this. Your test user has a password Password, but you test with Password123 and claim it should be a match. 😄

@lord5et
Copy link
Copy Markdown

lord5et commented Feb 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment