diff --git a/core/router/admin.coffee b/core/router/admin.coffee index 39a2fb3..7e58c63 100644 --- a/core/router/admin.coffee +++ b/core/router/admin.coffee @@ -1,6 +1,5 @@ {express, async, _} = app.libs {requireAdminAuthenticate} = app.middleware -{plaggable} = app {Account, Ticket, Financials, CouponCode} = app.models module.exports = exports = express.Router() @@ -56,30 +55,28 @@ exports.get '/ticket', (req, res) -> res.render 'ticket/list', result exports.post '/confirm_payment', (req, res) -> - Account.findOne req.body.account_id, (err, account) -> + Account.findById req.body.account_id, (err, account) -> unless account return res.error 'account_not_exist' - amount = parseFloat req.body.amount - - if _.isNaN amount + unless _.isFinite req.body.amount return res.error 'invalid_amount' - account.incBalance amount, 'deposit', + account.incBalance req.body.amount, 'deposit', type: req.body.type order_id: req.body.order_id , -> res.json {} exports.post '/delete_account', (req, res) -> - Account.findOne req.body.account_id, (err, account) -> + Account.findById req.body.account_id, (err, account) -> unless account return res.error 'account_not_exist' unless _.isEmpty account.billing.plans return res.error 'already_in_plan' - unless account.attribute.balance <= 0 + unless account.billing.balance <= 0 return res.error 'balance_not_empty' Account.findByIdAndRemove account._id, -> @@ -88,5 +85,5 @@ exports.post '/delete_account', (req, res) -> exports.post '/generate_coupon_code', (req, res) -> coupon_code = _.pick req.body, 'expired', 'available_times', 'type', 'meta' - CouponCode.createCodes coupon_code, req.body.count, (err, coupon_codes) -> + CouponCode.createCodes coupon_code, req.body.count, (err, coupon_codes...) -> res.json coupon_codes diff --git a/core/test/model/CouponCode.test.coffee b/core/test/model/CouponCode.test.coffee index 51507ce..edce46c 100644 --- a/core/test/model/CouponCode.test.coffee +++ b/core/test/model/CouponCode.test.coffee @@ -39,7 +39,7 @@ describe 'model/CouponCode', -> coupon1.type.should.be.equal 'amount' coupon1.meta.amount.should.be.equal 4 - coupon1.code.should.not.equal coupon2 + coupon1.code.should.not.equal coupon2.code for coupon in coupons created_objects.couponcodes.push coupon._id diff --git a/core/test/router/admin.test.coffee b/core/test/router/admin.test.coffee index 5625330..1a80038 100644 --- a/core/test/router/admin.test.coffee +++ b/core/test/router/admin.test.coffee @@ -4,6 +4,7 @@ describe 'router/admin', -> agent = null csrf_token = null + account_id = null before -> {utils} = app @@ -37,6 +38,16 @@ describe 'router/admin', -> res.body.token.should.be.exist done err + it 'should create a account for test', (done) -> + Account.register + username: "account#{utils.randomString(10).toLowerCase()}" + email: "#{utils.randomString 20}@gmail.com" + password: utils.randomString 20 + , (err, account) -> + created_objects.accounts.push account._id + account_id = account._id + done err + it 'GET / when no permission', (done) -> namespace.accountRouter.agent .get '/admin' @@ -53,8 +64,77 @@ describe 'router/admin', -> .expect 200 .end done - it 'POST confirm_payment' + it 'POST confirm_payment', (done) -> + agent.post '/admin/confirm_payment' + .send + csrf_token: csrf_token + account_id: account_id + amount: 10 + order_id: 'ID' + .expect 200 + .end done - it 'POST delete_account' + it 'POST confirm_payment with account_id not exist', (done) -> + agent.post '/admin/confirm_payment' + .send + csrf_token: csrf_token + account_id: '14534f8a3d9064cb116c315d' + amount: 10 + order_id: 'ID' + .expect 400 + .end (err, res) -> + res.body.error.should.be.equal 'account_not_exist' + done err - it 'POST generate_coupon_code' + it 'POST confirm_payment with invalid amount', (done) -> + agent.post '/admin/confirm_payment' + .send + csrf_token: csrf_token + account_id: account_id + amount: '1x' + .expect 400 + .end (err, res) -> + res.body.error.should.be.equal 'invalid_amount' + done err + + it 'POST delete_account', (done) -> + Account.findByIdAndUpdate account_id, + $set: + 'billing.balance': 0 + , -> + agent.post '/admin/delete_account' + .send + csrf_token: csrf_token + account_id: account_id + .expect 200 + .end (err) -> + Account.findById account_id, (mongo_err, account) -> + expect(mongo_err).to.not.exist + expect(account).to.not.exist + done err + + it 'POST generate_coupon_code', (done) -> + agent.post '/admin/generate_coupon_code' + .send + csrf_token: csrf_token + count: 2 + available_times: 3 + type: 'amount' + meta: + category: 'test' + amount: 4 + .expect 200 + .end (err, res) -> + res.body.should.have.length 2 + [coupon1, coupon2] = res.body + + coupon1.available_times.should.be.equal 3 + coupon1.type.should.be.equal 'amount' + coupon1.meta.amount.should.be.equal 4 + + coupon1.code.should.not.equal coupon2.code + + created_objects.couponcodes.push ObjectId coupon1._id + created_objects.couponcodes.push ObjectId coupon2._id + + done err