test of CouponCode.validateCode

This commit is contained in:
jysperm
2014-10-29 09:37:18 +08:00
parent 6e0af400d2
commit 403ba07ed4
3 changed files with 24 additions and 6 deletions

View File

@@ -38,7 +38,7 @@ CouponCode.plugin mongooseUniqueValidator,
exports.coupons_meta = coupons_meta =
amount:
validate: (account, coupon, callback) ->
@constructor.findOne
coupon.constructor.findOne
type: 'amount'
$or: [
'meta.category': coupon.meta.category
@@ -77,9 +77,10 @@ CouponCode.statics.createCodes = (template, count, callback) ->
CouponCode.methods.getMessage = (req, callback) ->
coupons_meta[@type].message req, @, callback
# @param callback(is_validated)
CouponCode.methods.validateCode = (account, callback) ->
if @available_times <= 0
return callback true
return callback()
coupons_meta[@type].validate account, @, callback
@@ -91,7 +92,7 @@ CouponCode.methods.applyCode = (account, callback) ->
$inc:
available_times: -1
$push:
log:
apply_log:
account_id: account._id
created_at: new Date()
, (err) =>

View File

@@ -22,6 +22,7 @@ describe 'model/CouponCode', ->
available_times: 3
type: 'amount'
meta:
category: 'test'
amount: 4
, 5, (err, coupons...) ->
expect(err).to.not.exist
@@ -56,6 +57,11 @@ describe 'model/CouponCode', ->
CouponCode.findById coupon1._id, (err, coupon1) ->
coupon1.available_times.should.be.equal 2
matched_account_id = _.find coupon1.apply_log, (item) ->
return item.account_id.toString() == account.id
matched_account_id.should.be.exist
original_account = account
Account.findById account._id, (err, account) ->
(account.billing.balance - original_account.billing.balance).should.be.equal 4
@@ -63,8 +69,18 @@ describe 'model/CouponCode', ->
done()
describe 'validateCode', ->
it 'should success'
it 'should success', (done) ->
coupon2.validateCode {_id: new ObjectId}, (is_validated) ->
is_validated.should.be.ok
done()
it 'should fail when used coupon'
it 'should fail when used coupon', (done) ->
coupon1.validateCode account, (is_validated) ->
expect(is_validated).to.not.ok
done()
it 'should fail when available_times <= 0'
it 'should fail when available_times <= 0', (done) ->
coupon2.available_times = 0
coupon2.validateCode account, (is_validated) ->
expect(is_validated).to.not.ok
done()

View File

@@ -12,6 +12,7 @@ global.async = require 'async'
global.deepmerge = require 'deepmerge'
global.chai = require 'chai'
global.supertest = require 'supertest'
global.ObjectId = (require 'mongoose').Schema.Types.ObjectId
global.expect = chai.expect