From fd1afdf6e66be5e618490fe59f284efd4c42d355 Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Wed, 6 Apr 2016 13:14:56 -0400 Subject: [PATCH] Test coverages for hashed password * Test coverages for hashed password for replaceAttributes * Test coverages for hashed password for updateAttribute --- test/manipulation.test.js | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 1dc2d358..3dd574df 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -37,6 +37,15 @@ describe('manipulation', function () { before(function setupStubUserModel(done) { StubUser = db.createModel('StubUser', { password: String }, { forceId: true }); StubUser.setter.password = function(plain) { + var hashed = false; + if (!plain) return; + var pos = plain.indexOf('-'); + if (pos !== -1) { + var head = plain.substr(0, pos); + var tail = plain.substr(pos + 1, plain.length); + hashed = head.toUpperCase() === tail; + } + if (hashed) return; this.$password = plain + '-' + plain.toUpperCase(); }; db.automigrate('StubUser', done); @@ -464,6 +473,22 @@ describe('manipulation', function () { done(); }); + it('should have updated password hashed with updateAttribute', + function(done) { + StubUser.create({ password: 'foo' }, function(err, created) { + if (err) return done(err); + created.updateAttribute('password', 'test', function(err, created) { + if (err) return done(err); + created.password.should.equal('test-TEST'); + StubUser.findById(created.id, function(err, found) { + if (err) return done(err); + found.password.should.equal('test-TEST'); + done(); + }); + }); + }); + }); + it('should update one attribute', function(done) { person.updateAttribute('name', 'Paul Graham', function(err, p) { if (err) return done(err); @@ -943,10 +968,27 @@ describe('manipulation', function () { }); }); - it('works without options(promise variant)', function(done) { - Post.findById(postInstance.id) - .then(function(p){ - p.replaceAttributes({title: 'b'}) + it('should have updated password hashed with replaceAttributes', + function(done) { + StubUser.create({ password: 'foo' }, function(err, created) { + if (err) return done(err); + created.replaceAttributes({ password: 'test' }, + function(err, created) { + if (err) return done(err); + created.password.should.equal('test-TEST'); + StubUser.findById(created.id, function(err, found) { + if (err) return done(err); + found.password.should.equal('test-TEST'); + done(); + }); + }); + }); + }); + + it('works without options(promise variant)', function(done) { + Post.findById(postInstance.id) + .then(function(p) { + p.replaceAttributes({ title: 'b' }) .then(function(p) { should.exist(p); p.should.be.instanceOf(Post);