From 6b51c0477ff940f445ffa973cffd1bc0bfde03f2 Mon Sep 17 00:00:00 2001 From: Barbara Liau Date: Tue, 18 Apr 2017 16:27:05 -0600 Subject: [PATCH 1/3] Add getters to toObjet, and add toJSON --- lib/models/credit.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/models/credit.js b/lib/models/credit.js index db60ca35..b8ab294c 100644 --- a/lib/models/credit.js +++ b/lib/models/credit.js @@ -72,6 +72,15 @@ const Credit = new mongoose.Schema({ }); Credit.set('toObject', { + getters: true, + transform: function(doc, ret) { + delete ret.__v; + delete ret._id; + } +}); + +Credit.set('toJSON', { + getters: true, transform: function(doc, ret) { delete ret.__v; delete ret._id; From 9f77a6c34c910b9e51898621ab5ffc58ad9339da Mon Sep 17 00:00:00 2001 From: Barbara Liau Date: Tue, 18 Apr 2017 16:27:31 -0600 Subject: [PATCH 2/3] Add tests to check for getters --- test/credit.unit.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/credit.unit.js b/test/credit.unit.js index 178c3b8d..03ba2fcb 100644 --- a/test/credit.unit.js +++ b/test/credit.unit.js @@ -554,6 +554,71 @@ describe('Storage/models/Credit', function() { }); }); + it('should call getters', function(done) { + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.NEW_SIGNUP, + promo_amount: PROMO_AMOUNT.NEW_SIGNUP, + promo_expires: PROMO_EXPIRES.NEW_SIGNUP + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + expect(credit.toObject().promo_amount).to.equal(488); + done(); + }); + }); + + }); + + describe('#toJSON', function() { + + it('should remove expected fields', function (done) { + const amount = 1000; + + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.STORJ_EVENT, + promo_amount: amount, + promo_expires: PROMO_EXPIRES.DEFAULT + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + + const creditKeys = Object.keys(credit.toJSON()); + expect(creditKeys).to.not.contain('__v', '_id'); + done(); + }); + }); + + it('should call getters', function (done) { + const amount = 1000; + + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.STORJ_EVENT, + promo_amount: amount, + promo_expires: PROMO_EXPIRES.DEFAULT + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + expect(credit.promo_amount).to.equal(amount); + done(); + }); + + }); + }); }); From b048d5307a691dc34f2318500723fceb9d487064 Mon Sep 17 00:00:00 2001 From: Barbara Liau Date: Tue, 18 Apr 2017 16:27:31 -0600 Subject: [PATCH 3/3] Add tests to check for getters --- test/credit.unit.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/credit.unit.js b/test/credit.unit.js index 178c3b8d..03ba2fcb 100644 --- a/test/credit.unit.js +++ b/test/credit.unit.js @@ -554,6 +554,71 @@ describe('Storage/models/Credit', function() { }); }); + it('should call getters', function(done) { + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.NEW_SIGNUP, + promo_amount: PROMO_AMOUNT.NEW_SIGNUP, + promo_expires: PROMO_EXPIRES.NEW_SIGNUP + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + expect(credit.toObject().promo_amount).to.equal(488); + done(); + }); + }); + + }); + + describe('#toJSON', function() { + + it('should remove expected fields', function (done) { + const amount = 1000; + + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.STORJ_EVENT, + promo_amount: amount, + promo_expires: PROMO_EXPIRES.DEFAULT + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + + const creditKeys = Object.keys(credit.toJSON()); + expect(creditKeys).to.not.contain('__v', '_id'); + done(); + }); + }); + + it('should call getters', function (done) { + const amount = 1000; + + const newCredit = new Credit({ + user: 'user123@domain.tld', + type: CREDIT_TYPES.MANUAL, + promo_code: PROMO_CODE.STORJ_EVENT, + promo_amount: amount, + promo_expires: PROMO_EXPIRES.DEFAULT + }); + + newCredit.save(function(err, credit) { + if (err) { + return done(err); + } + expect(credit.promo_amount).to.equal(amount); + done(); + }); + + }); + }); });