Skip to content

Commit

Permalink
chore: Updated tests to better reflect their descriptions (#312)
Browse files Browse the repository at this point in the history
* deleted duplicate test, changed some assertions to reflect their description

* added tests to cover more branches
  • Loading branch information
Andrew836-dev authored Jan 9, 2021
1 parent 59bb478 commit 8274dfb
Showing 1 changed file with 115 additions and 99 deletions.
214 changes: 115 additions & 99 deletions test/passport-local-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ describe('passportLocalMongoose', function() {
});
});
});

it('should fail when no replacement password given', function(done) {
const user = new DefaultUser();

user.setPassword('password1', function(err) {
if (err) {
return done(err);
}

user.changePassword('password1', '', function(err) {
expect(err).to.be.instanceof(errors.MissingPasswordError);

done();
});
});
});
});

describe('#changePassword() async', function() {
Expand Down Expand Up @@ -354,6 +370,16 @@ describe('passportLocalMongoose', function() {
const changePasswordUser = await user.changePassword('password1', 'password2');
expect(changePasswordUser).to.exist;
});

it('should fail when no replacement password given', async () => {
const user = new DefaultUser();

await user.setPassword('password1');

const result = await user.changePassword('password1', '').catch(err => err);

expect(result).to.be.instanceof(errors.MissingPasswordError);
});
});

describe('#authenticate() callback', function() {
Expand Down Expand Up @@ -448,44 +474,10 @@ describe('passportLocalMongoose', function() {
return done(err);
}

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId();
user.authenticate('password', function(err, user, error) {
expect(err).to.exist;
expect(user).to.not.exist;
expect(error).to.not.exist;
done();
});
});
});
});

it('should get an error updating the user on password match when limiting attempts', function(done) {
const UserSchema = new Schema({}, { saveErrorIfNotFound: true });
UserSchema.plugin(passportLocalMongoose, {
limitAttempts: true
});
const User = mongoose.model('LimitAttemptsUpdateWithError', UserSchema);

const user = new User({
username: 'jane'
});
user.setPassword('password', function(err) {
if (err) {
return done(err);
}

user.save(function(err) {
if (err) {
return done(err);
}

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId();
user.authenticate('password', function(err, user, error) {
expect(err).to.exist;
expect(user).to.not.exist;
expect(error).to.not.exist;
expect(err).to.not.exist;
expect(user).to.be.false;
expect(error).to.be.instanceOf(errors.AttemptTooSoonError);
done();
});
});
Expand Down Expand Up @@ -535,7 +527,9 @@ describe('passportLocalMongoose', function() {
const User = mongoose.model('LimitAttemptsMismatchWithAnError', UserSchema);

const user = new User({
username: 'wendy'
username: 'wendy',
attempts: 1,
last: Date.now()
});
user.setPassword('password', function(err) {
if (err) {
Expand All @@ -547,19 +541,32 @@ describe('passportLocalMongoose', function() {
return done(err);
}

user.hash = 'deadbeef'; // force an error on scmp with different length hex.

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId(); // force the save to fail
user.authenticate('password', function(err, user, error) {
expect(err).to.exist;
expect(user).to.not.exist;
expect(error).to.not.exist;
user.authenticate('WRONGpassword', function(err, user, error) {
if (err) {
return done(err);
}
expect(user).to.be.false;
expect(error).to.be.instanceof(errors.AttemptTooSoonError);
done();
});
});
});
});

it('should supply message if username is not registered', function(done) {
const user = new DefaultUser({
username: 'andrew'
});
user.authenticate('password', function(err, result, error) {
if (err) {
return done(err);
}

expect(result).to.be.false;
expect(error.message).to.exist;
done();
});
});
});

describe('#authenticate() async', function() {
Expand Down Expand Up @@ -619,41 +626,10 @@ describe('passportLocalMongoose', function() {
await user.setPassword('password');
await user.save();

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId();

try {
await user.authenticate('password');
} catch (e) {
return;
}

throw new Error('Expected user.authenticate to throw');
});

it('should get an error updating the user on password match when limiting attempts', async () => {
const UserSchema = new Schema({}, { saveErrorIfNotFound: true });
UserSchema.plugin(passportLocalMongoose, {
limitAttempts: true
});
const User = mongoose.model('LimitAttemptsUpdateWithErrorAsync', UserSchema);

const user = new User({
username: 'jane'
});

await user.setPassword('password');
await user.save();

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId();
try {
await user.authenticate('password');
} catch (e) {
return;
}
const { user: authenticatedUser, error } = await user.authenticate('password');

throw new Error('Expected user.authenticate to throw');
expect(authenticatedUser).to.be.false;
expect(error).to.be.instanceof(errors.AttemptTooSoonError);
});

it('should update the user on password match while limiting attempts', async () => {
Expand Down Expand Up @@ -691,18 +667,20 @@ describe('passportLocalMongoose', function() {
await user.setPassword('password');
await user.save();

user.hash = 'deadbeef'; // force an error on scmp with different length hex.
const { user: authenticatedUser, error } = await user.authenticate('WRONGpassword');

// TODO: This test does not test limit attempts at all but tests a mongoose error 'No document found for query "{ _id: 5a9672ad958eb907e4619736 }"!'
user._id = mongoose.Types.ObjectId(); // force the save to fail
expect(authenticatedUser).to.be.false;
expect(error).to.be.instanceof(errors.IncorrectPasswordError);
});

try {
await user.authenticate('password');
} catch (e) {
return;
}
it('should supply message if username is not registered', async () => {
const user = new DefaultUser({
username: 'andrew'
});
const { user: authenticatedUser, error } = await user.authenticate('password');

throw new Error('Expected user.authenticate to throw');
expect(authenticatedUser).to.be.false;
expect(error.message).to.exist;
});
});

Expand Down Expand Up @@ -742,7 +720,7 @@ describe('passportLocalMongoose', function() {
return done(err);
}

expect(result instanceof DefaultUser).to.exist;
expect(result).to.be.instanceof(DefaultUser);
expect(result.username).to.equal(user.username);

expect(result.salt).to.equal(user.salt);
Expand All @@ -754,10 +732,10 @@ describe('passportLocalMongoose', function() {
});
});

it('should authenticate existing user with case insensitive username with matching password', function(done) {
it('should authenticate existing user with usernameLowerCase enabled and with matching password', function(done) {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, { usernameLowerCase: true });
const User = mongoose.model('AuthenticateWithCaseInsensitiveUsername', UserSchema);
const User = mongoose.model('AuthenticateWithLowerCaseUsername', UserSchema);

const username = 'userName';
User.register({ username: username }, 'password', function(err) {
Expand All @@ -770,14 +748,38 @@ describe('passportLocalMongoose', function() {
return done(err);
}

expect(result instanceof User).to.exist;
expect(result).to.be.instanceof(User);
expect('username').to.equal(result.username);

done();
});
});
});

it('should authenticate existing user with case insensitive username with matching password', function(done) {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, { usernameCaseInsensitive: true });
const User = mongoose.model('AuthenticateWithCaseInsensitiveUsername', UserSchema);

const username = 'userName';
User.register({ username: username }, 'password', function(err) {
if (err) {
return done(err);
}

User.authenticate()('username', 'password', function(err, result) {
if (err) {
return done(err);
}

expect(result).to.be.instanceof(User);
expect(username).to.equal(result.username);

done();
});
});
});

it('should authenticate existing user with matching password with field overrides', function(done) {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, {
Expand All @@ -798,7 +800,7 @@ describe('passportLocalMongoose', function() {
return done(err);
}

expect(result instanceof User).to.exist;
expect(result).to.be.instanceof(User);
expect(result.email).to.equal(user.email);
expect(result.saltValue).to.equal(user.saltValue);
expect(result.hashValue).to.equal(user.hashValue);
Expand Down Expand Up @@ -980,27 +982,41 @@ describe('passportLocalMongoose', function() {
await user.save();
const { user: result } = await DefaultUser.authenticate()('user', 'password');

expect(result instanceof DefaultUser).to.exist;
expect(result).to.be.instanceof(DefaultUser);
expect(result.username).to.equal(user.username);

expect(result.salt).to.equal(user.salt);
expect(result.hash).to.equal(user.hash);
});

it('should authenticate existing user with case insensitive username with matching password', async () => {
it('should authenticate existing user with usernameLowerCase enabled and with matching password', async () => {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, { usernameLowerCase: true });
const User = mongoose.model('AuthenticateWithCaseInsensitiveUsernameAsync', UserSchema);
const User = mongoose.model('AuthenticateWithLowerCaseUsernameAsync', UserSchema);

const username = 'userName';
await User.register({ username: username }, 'password');

const { user: result } = await User.authenticate()('username', 'password');

expect(result instanceof User).to.exist;
expect(result).to.be.instanceof(User);
expect('username').to.equal(result.username);
});

it('should authenticate existing user with case insensitive username with matching password', async () => {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, { usernameCaseInsensitive: true });
const User = mongoose.model('AuthenticateWithCaseInsensitiveUsernameAsync', UserSchema);

const username = 'userName';
await User.register({ username: username }, 'password');

const { user: result } = await User.authenticate()('username', 'password');

expect(result).to.be.instanceof(User);
expect(username).to.equal(result.username);
});

it('should authenticate existing user with matching password with field overrides', async () => {
const UserSchema = new Schema();
UserSchema.plugin(passportLocalMongoose, {
Expand All @@ -1015,7 +1031,7 @@ describe('passportLocalMongoose', function() {

const { user: result } = await User.authenticate()(email, 'password');

expect(result instanceof User).to.exist;
expect(result).to.be.instanceof(User);
expect(result.email).to.equal(user.email);
expect(result.saltValue).to.equal(user.saltValue);
expect(result.hashValue).to.equal(user.hashValue);
Expand Down

0 comments on commit 8274dfb

Please sign in to comment.