Skip to content

Commit

Permalink
finished .transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 9, 2014
1 parent 1b57fc0 commit 477c3e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ Account.transfer = function(obj, cb){
obj.amount *= 1;
var total = obj.amount + 25;
Account.collection.findOne({_id:obj.fromId}, {fields:{balance:1, pin:1}}, function(err, a){
console.log(a);
//console.log(a);
if(obj.pin === a.pin && a.balance >= total){
a.balance -= total;
Transfer.save(obj, function(err, t){
console.log(t);
//console.log(t);
Account.collection.update({_id:a._id}, {$set:{balance:a.balance}, $push:{transferIds:t._id}}, function(){
Account.collection.update({_id:obj.toId}, {$inc:{balance:obj.amount}, $push:{transferIds:t._id}}, function(){
if(cb){cb();}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,31 @@ describe('Account', function(){
});
});
});
it('should not transfer funds from one account to another (wrong PIN)', function(done){
Account.transfer({from:'Bob', to:'Jim', pin:'1264', fromId:bobId, toId:jimId, amount:'250'}, function(){
Account.findById(bobId, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.transferIds).to.have.length(4);
Account.findById(jimId, function(a2){
expect(a2.balance).to.be.closeTo(100, 0.1);
expect(a2.transferIds).to.have.length(4);
done();
});
});
});
});
it('should not transfer funds from one account to another (Not enough money)', function(done){
Account.transfer({from:'Bob', to:'Jim', pin:'1234', fromId:bobId, toId:jimId, amount:'1000'}, function(){
Account.findById(bobId, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.transferIds).to.have.length(4);
Account.findById(jimId, function(a2){
expect(a2.balance).to.be.closeTo(100, 0.1);
expect(a2.transferIds).to.have.length(4);
done();
});
});
});
});
});
});

0 comments on commit 477c3e3

Please sign in to comment.