2015-03-19 18:02:45 -04:00
|
|
|
require '../common'
|
2015-09-25 13:03:44 -04:00
|
|
|
config = require '../../../server_config'
|
|
|
|
moment = require 'moment'
|
|
|
|
{findStripeSubscription} = require '../../../server/lib/utils'
|
2015-10-06 15:23:47 -04:00
|
|
|
async = require 'async'
|
2015-03-19 18:02:45 -04:00
|
|
|
|
|
|
|
describe '/db/prepaid', ->
|
|
|
|
prepaidURL = getURL('/db/prepaid')
|
|
|
|
prepaidCreateURL = getURL('/db/prepaid/-/create')
|
|
|
|
|
2015-09-25 13:03:44 -04:00
|
|
|
headers = {'X-Change-Plan': 'true'}
|
|
|
|
|
|
|
|
joeData = null
|
|
|
|
stripe = require('stripe')(config.stripe.secretKey)
|
|
|
|
joeCode = null
|
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
verifyCoursePrepaid = (user, prepaid, done) ->
|
|
|
|
expect(prepaid.creator).toEqual(user.id)
|
|
|
|
expect(prepaid.type).toEqual('course')
|
|
|
|
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
|
|
|
|
expect(prepaid.code).toMatch(/^\w{8}$/)
|
|
|
|
done()
|
|
|
|
|
|
|
|
verifySubscriptionPrepaid = (user, prepaid, done) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(prepaid.creator).toEqual(user.id)
|
|
|
|
expect(prepaid.type).toEqual('subscription')
|
2015-08-12 18:51:16 -04:00
|
|
|
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(prepaid.code).toMatch(/^\w{8}$/)
|
|
|
|
expect(prepaid.properties?.couponID).toEqual('free')
|
|
|
|
done()
|
|
|
|
|
2015-11-02 18:23:08 -05:00
|
|
|
it 'Clear database', (done) ->
|
|
|
|
clearModels [Course, CourseInstance, Payment, Prepaid, User], (err) ->
|
|
|
|
throw err if err
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'POST /db/prepaid/<id>/redeemers', ->
|
|
|
|
|
|
|
|
it 'adds a given user to the redeemers property', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: 1,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 0
|
2015-11-20 16:17:33 -05:00
|
|
|
type: 'course'
|
2015-11-02 18:23:08 -05:00
|
|
|
})
|
|
|
|
prepaid.save (err, prepaid) ->
|
|
|
|
otherUser = new User()
|
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid.id}/redeemers")
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
2015-11-13 15:51:09 -05:00
|
|
|
expect(body.redeemers?.length).toBe(1)
|
2015-11-02 18:23:08 -05:00
|
|
|
expect(res.statusCode).toBe(200)
|
2015-11-13 16:13:08 -05:00
|
|
|
return done() unless res.statusCode is 200
|
2015-11-05 15:53:25 -05:00
|
|
|
prepaid = Prepaid.findById body._id, (err, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(prepaid.get('redeemers').length).toBe(1)
|
|
|
|
User.findById otherUser.id, (err, user) ->
|
|
|
|
expect(user.get('coursePrepaidID').equals(prepaid.get('_id'))).toBe(true)
|
|
|
|
done()
|
2015-11-02 18:23:08 -05:00
|
|
|
|
|
|
|
it 'does not allow more redeemers than maxRedeemers', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: 0,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 1
|
2015-11-20 16:17:33 -05:00
|
|
|
type: 'course'
|
2015-11-02 18:23:08 -05:00
|
|
|
})
|
|
|
|
prepaid.save (err, prepaid) ->
|
|
|
|
otherUser = new User()
|
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid.id}/redeemers")
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'only allows the owner of the prepaid to add redeemers', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: 1000,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 2
|
2015-11-20 16:17:33 -05:00
|
|
|
type: 'course'
|
2015-11-02 18:23:08 -05:00
|
|
|
})
|
|
|
|
prepaid.save (err, prepaid) ->
|
|
|
|
loginNewUser (user2) ->
|
|
|
|
otherUser = new User()
|
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid.id}/redeemers")
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'is idempotent across prepaids collection', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
2015-11-20 16:17:33 -05:00
|
|
|
otherUser = new User({
|
|
|
|
'coursePrepaidID': new ObjectId()
|
|
|
|
})
|
2015-11-02 18:23:08 -05:00
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
prepaid1 = new Prepaid({
|
|
|
|
redeemers: [{userID: otherUser.get('_id')}],
|
|
|
|
code: 3
|
2015-11-20 16:17:33 -05:00
|
|
|
type: 'course'
|
2015-11-02 18:23:08 -05:00
|
|
|
})
|
|
|
|
prepaid1.save (err, prepaid1) ->
|
|
|
|
prepaid2 = new Prepaid({
|
|
|
|
maxRedeemers: 10,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 4
|
2015-11-20 16:17:33 -05:00
|
|
|
type: 'course'
|
2015-11-02 18:23:08 -05:00
|
|
|
})
|
|
|
|
prepaid2.save (err, prepaid2) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid2.id}/redeemers")
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(res.statusCode).toBe(200)
|
2015-11-14 19:22:16 -05:00
|
|
|
return done() unless res.statusCode is 200
|
2015-11-02 18:23:08 -05:00
|
|
|
expect(body.redeemers.length).toBe(0)
|
|
|
|
done()
|
2015-11-20 16:17:33 -05:00
|
|
|
|
|
|
|
it 'is idempotent to itself for a user other than the creator', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: 2,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 0
|
|
|
|
type: 'course'
|
|
|
|
})
|
|
|
|
prepaid.save (err, prepaid) ->
|
|
|
|
otherUser = new User()
|
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid.id}/redeemers")
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(body.redeemers?.length).toBe(1)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(body.redeemers?.length).toBe(1)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
prepaid = Prepaid.findById body._id, (err, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(prepaid.get('redeemers').length).toBe(1)
|
|
|
|
User.findById otherUser.id, (err, user) ->
|
|
|
|
expect(user.get('coursePrepaidID').equals(prepaid.get('_id'))).toBe(true)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'is idempotent to itself for the creator', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: 2,
|
|
|
|
redeemers: [],
|
|
|
|
creator: user1.get('_id')
|
|
|
|
code: 0
|
|
|
|
type: 'course'
|
|
|
|
})
|
|
|
|
prepaid.save (err, prepaid) ->
|
|
|
|
otherUser = new User()
|
|
|
|
otherUser.save (err, otherUser) ->
|
|
|
|
url = getURL("/db/prepaid/#{prepaid.id}/redeemers")
|
|
|
|
redeemer = { userID: user1.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(body.redeemers?.length).toBe(1)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(body.redeemers?.length).toBe(1)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
prepaid = Prepaid.findById body._id, (err, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(prepaid.get('redeemers').length).toBe(1)
|
|
|
|
User.findById user1.id, (err, user) ->
|
|
|
|
expect(user.get('coursePrepaidID').equals(prepaid.get('_id'))).toBe(true)
|
|
|
|
redeemer = { userID: otherUser.id }
|
|
|
|
request.post {uri: url, json: redeemer }, (err, res, body) ->
|
|
|
|
expect(body.redeemers?.length).toBe(2)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
done()
|
2015-11-02 18:23:08 -05:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Clear database', (done) ->
|
|
|
|
clearModels [Course, CourseInstance, Payment, Prepaid, User], (err) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
throw err if err
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Anonymous creates prepaid code', (done) ->
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'subscription', 1, 0, (err, res, body) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(401)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Non-admin creates prepaid code', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
expect(user1.isAdmin()).toEqual(false)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'subscription', 4, 0, (err, res, body) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Admin creates prepaid code with type subscription', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'subscription', 1, 0, (err, res, body) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(200)
|
2015-10-01 18:23:20 -04:00
|
|
|
verifySubscriptionPrepaid user1, body, done
|
2015-03-19 18:02:45 -04:00
|
|
|
|
2015-09-25 13:03:44 -04:00
|
|
|
it 'Admin creates prepaid code with type terminal_subscription', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
|
|
createPrepaid 'terminal_subscription', 2, 3, (err, res, body) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
expect(body.creator).toEqual(user1.id)
|
|
|
|
expect(body.type).toEqual('terminal_subscription')
|
|
|
|
expect(body.maxRedeemers).toEqual(2)
|
|
|
|
expect(body.properties?.months).toEqual(3)
|
|
|
|
expect(body.code).toMatch(/^\w{8}$/)
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
2015-03-19 18:02:45 -04:00
|
|
|
it 'Admin creates prepaid code with invalid type', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'bulldozer', 1, 0, (err, res, body) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Admin creates prepaid code with no type specified', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid null, 1, 0, (err, res, body) ->
|
2015-08-12 18:51:16 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Admin creates prepaid code with invalid maxRedeemers', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'subscription', 0, 0, (err, res, body) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Non-admin requests /db/prepaid', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
expect(user1.isAdmin()).toEqual(false)
|
|
|
|
request.get {uri: prepaidURL}, (err, res, body) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Admin requests /db/prepaid', (done) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
user1.set('permissions', ['admin'])
|
|
|
|
user1.save (err, user1) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(user1.isAdmin()).toEqual(true)
|
2015-09-25 13:03:44 -04:00
|
|
|
createPrepaid 'subscription', 1, 0, (err, res, prepaid) ->
|
2015-03-19 18:02:45 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
request.get {uri: prepaidURL}, (err, res, body) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
prepaids = JSON.parse(body)
|
|
|
|
found = false
|
|
|
|
for p in prepaids
|
|
|
|
if p._id is prepaid._id
|
|
|
|
found = true
|
2015-10-01 18:23:20 -04:00
|
|
|
verifySubscriptionPrepaid user1, p, done
|
2015-03-19 18:02:45 -04:00
|
|
|
break
|
|
|
|
expect(found).toEqual(true)
|
|
|
|
done() unless found
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
describe 'Purchase course', ->
|
2015-11-05 17:20:24 -05:00
|
|
|
it 'Standard user purchases a prepaid for 0 seats', (done) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
2015-11-05 17:20:24 -05:00
|
|
|
purchasePrepaid 'course', {}, 0, token.id, (err, res, prepaid) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(err).toBeNull()
|
2015-11-05 17:20:24 -05:00
|
|
|
expect(res.statusCode).toBe(422)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Standard user purchases a prepaid for 1 seat', (done) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
2015-11-05 17:20:24 -05:00
|
|
|
purchasePrepaid 'course', {}, 1, token.id, (err, res, prepaid) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(err).toBeNull()
|
2015-11-05 17:20:24 -05:00
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
verifyCoursePrepaid(user1, prepaid, done)
|
2015-10-01 18:23:20 -04:00
|
|
|
|
2015-11-05 17:20:24 -05:00
|
|
|
it 'Standard user purchases a prepaid for 3 seats', (done) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
2015-11-05 17:20:24 -05:00
|
|
|
purchasePrepaid 'course', {}, 3, token.id, (err, res, prepaid) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(err).toBeNull()
|
2015-11-05 17:20:24 -05:00
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
verifyCoursePrepaid(user1, prepaid, done)
|
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
describe 'Purchase terminal_subscription', ->
|
|
|
|
it 'Anonymous submits a prepaid purchase', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
logoutUser () ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 3, 3, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(401)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if type isnt terminal_subscription', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'subscription', months: 3, 3, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if maxRedeemers is -1', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 3, -1, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(422)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if maxRedeemers is foo', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 3, 'foo', token.id, (err, res, prepaid) ->
|
2015-09-25 13:03:44 -04:00
|
|
|
expect(err).toBeNull()
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(res.statusCode).toBe(422)
|
2015-09-25 13:03:44 -04:00
|
|
|
done()
|
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if months is -1', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: -1, 3, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(422)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if months is foo', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 'foo', 3, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(422)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Should error if maxRedeemers and months are less than 3', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginNewUser (user1) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 1, 1, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'User submits valid prepaid code purchase', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
stripeTokenID = token.id
|
|
|
|
loginJoe (joe) ->
|
|
|
|
joeData = joe.toObject()
|
|
|
|
joeData.stripe = {
|
|
|
|
token: stripeTokenID
|
|
|
|
planID: 'basic'
|
|
|
|
}
|
|
|
|
request.put {uri: getURL('/db/user'), json: joeData, headers: headers }, (err, res, body) ->
|
|
|
|
joeData = body
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
expect(joeData.stripe.customerID).toBeDefined()
|
|
|
|
expect(firstSubscriptionID = joeData.stripe.subscriptionID).toBeDefined()
|
|
|
|
expect(joeData.stripe.planID).toBe('basic')
|
|
|
|
expect(joeData.stripe.token).toBeUndefined()
|
|
|
|
# TODO: is this test still valid after new token?
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 3, 3, token.id, (err, res, prepaid) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
expect(prepaid.type).toEqual('terminal_subscription')
|
|
|
|
expect(prepaid.code).toBeDefined()
|
|
|
|
# Saving this code for later tests
|
|
|
|
# TODO: don't make tests dependent on each other
|
|
|
|
joeCode = prepaid.code
|
|
|
|
expect(prepaid.creator).toBeDefined()
|
|
|
|
expect(prepaid.maxRedeemers).toEqual(3)
|
2015-11-02 16:06:58 -05:00
|
|
|
expect(prepaid.exhausted).toBe(false)
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(prepaid.properties).toBeDefined()
|
|
|
|
expect(prepaid.properties.months).toEqual(3)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'Should have logged a Payment with the correct amount', (done) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
query =
|
|
|
|
purchaser: joe._id
|
|
|
|
Payment.find query, (err, payments) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(payments).not.toBeNull()
|
|
|
|
expect(payments.length).toEqual(1)
|
|
|
|
expect(payments[0].get('amount')).toEqual(8991)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Anonymous cant redeem a prepaid code', (done) ->
|
|
|
|
logoutUser () ->
|
|
|
|
subscribeWithPrepaid joeCode, (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res?.statusCode).toEqual(401)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'User cant redeem a nonexistant prepaid code', (done) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
subscribeWithPrepaid 'abc123', (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
2015-10-06 15:23:47 -04:00
|
|
|
expect(res.statusCode).toEqual(404)
|
2015-10-01 18:23:20 -04:00
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'User cant redeem empty code', (done) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
subscribeWithPrepaid '', (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(422)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Anonymous cant fetch a prepaid code', (done) ->
|
|
|
|
expect(joeCode).not.toBeNull()
|
|
|
|
logoutUser () ->
|
|
|
|
fetchPrepaid joeCode, (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(403)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'User can fetch a prepaid code', (done) ->
|
|
|
|
expect(joeCode).not.toBeNull()
|
|
|
|
loginJoe (joe) ->
|
|
|
|
fetchPrepaid joeCode, (err, res, body) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200)
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(body).toBeDefined()
|
|
|
|
return done() unless body
|
2015-09-25 16:58:17 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
prepaid = JSON.parse(body)
|
|
|
|
expect(prepaid.code).toEqual(joeCode)
|
|
|
|
expect(prepaid.maxRedeemers).toEqual(3)
|
|
|
|
expect(prepaid.properties?.months).toEqual(3)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
# TODO: Move redeem subscription prepaid code tests to subscription tests file
|
|
|
|
describe 'Subscription redeem tests', ->
|
|
|
|
it 'Creator can redeeem a prepaid code', (done) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
expect(joeCode).not.toBeNull()
|
|
|
|
expect(joeData.stripe?.customerID).toBeDefined()
|
|
|
|
expect(joeData.stripe?.subscriptionID).toBeDefined()
|
|
|
|
return done() unless joeData.stripe?.customerID
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
# joe has a stripe subscription, so test if the months are added to the end of it.
|
|
|
|
stripe.customers.retrieve joeData.stripe.customerID, (err, customer) =>
|
|
|
|
expect(err).toBeNull()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
findStripeSubscription customer.id, subscriptionID: joeData.stripe?.subscriptionID, (subscription) =>
|
|
|
|
if subscription
|
|
|
|
stripeSubscriptionPeriodEndDate = new moment(subscription.current_period_end * 1000)
|
|
|
|
else
|
|
|
|
expect(stripeSubscriptionPeriodEndDate).toBeDefined()
|
|
|
|
return done()
|
|
|
|
|
|
|
|
subscribeWithPrepaid joeCode, (err, res, result) =>
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200)
|
|
|
|
endDate = stripeSubscriptionPeriodEndDate.add(3, 'months').toISOString().substring(0, 10)
|
|
|
|
expect(result?.stripe?.free).toEqual(endDate)
|
|
|
|
expect(result?.purchased?.gems).toEqual(14000)
|
|
|
|
findStripeSubscription customer.id, subscriptionID: joeData.stripe?.subscriptionID, (subscription) =>
|
|
|
|
expect(subscription).toBeNull()
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'User can redeem a prepaid code', (done) ->
|
|
|
|
loginSam (sam) ->
|
|
|
|
subscribeWithPrepaid joeCode, (err, res, result) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200)
|
|
|
|
endDate = new moment().add(3, 'months').toISOString().substring(0, 10)
|
|
|
|
expect(result?.stripe?.free).toEqual(endDate)
|
|
|
|
expect(result?.purchased?.gems).toEqual(10500)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Wont allow the same person to redeem twice', (done) ->
|
|
|
|
loginSam (sam) ->
|
|
|
|
subscribeWithPrepaid joeCode, (err, res, result) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(403)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Will return redeemed code as part of codes list', (done) ->
|
|
|
|
loginSam (sam) ->
|
|
|
|
request.get "#{getURL('/db/user')}/#{sam.id}/prepaid_codes", (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200)
|
|
|
|
codes = JSON.parse res.body
|
|
|
|
expect(codes.length).toEqual(1)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Third user can redeem a prepaid code', (done) ->
|
|
|
|
loginNewUser (user) ->
|
|
|
|
subscribeWithPrepaid joeCode, (err, res, result) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200)
|
|
|
|
endDate = new moment().add(3, 'months').toISOString().substring(0, 10)
|
|
|
|
expect(result?.stripe?.free).toEqual(endDate)
|
|
|
|
expect(result?.purchased?.gems).toEqual(10500)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Fourth user cannot redeem code', (done) ->
|
|
|
|
loginNewUser (user) ->
|
|
|
|
subscribeWithPrepaid joeCode, (err, res, result) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(403)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Can fetch a list of purchased and redeemed prepaid codes', (done) ->
|
|
|
|
stripe.tokens.create {
|
|
|
|
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
}, (err, token) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
purchasePrepaid 'terminal_subscription', months: 1, 3, token.id, (err, res, prepaid) ->
|
|
|
|
request.get "#{getURL('/db/user')}/#{joe.id}/prepaid_codes", (err, res) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(res.statusCode).toEqual(200);
|
|
|
|
codes = JSON.parse res.body
|
|
|
|
expect(codes.length).toEqual(2)
|
|
|
|
expect(codes[0].maxRedeemers).toEqual(3)
|
|
|
|
expect(codes[0].properties).toBeDefined()
|
|
|
|
expect(codes[0].properties.months).toEqual(3)
|
|
|
|
done()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
it 'Test for injection', (done) ->
|
|
|
|
loginNewUser (user) ->
|
|
|
|
code = { $exists: true }
|
|
|
|
subscribeWithPrepaid code, (err, res, result) ->
|
2015-09-25 13:03:44 -04:00
|
|
|
expect(err).toBeNull()
|
2015-10-01 18:23:20 -04:00
|
|
|
expect(res.statusCode).not.toEqual(200)
|
2015-09-25 13:03:44 -04:00
|
|
|
done()
|
2015-10-06 15:23:47 -04:00
|
|
|
|
2015-11-02 18:27:13 -05:00
|
|
|
# it 'Test a bunch of people trying to redeem at once', (done) ->
|
|
|
|
# doRedeem = (userX, code, testnum, retry, fnDone) =>
|
|
|
|
# loginUser userX, () =>
|
|
|
|
# endDate = new moment().add(3, 'months').toISOString().substring(0, 10)
|
|
|
|
# subscribeWithPrepaid code, (err, res, result) ->
|
|
|
|
# if err
|
|
|
|
# return fnDone(err)
|
|
|
|
#
|
|
|
|
# expect(err).toBeNull()
|
|
|
|
# expect(result).toBeDefined()
|
|
|
|
# if result.stripe
|
|
|
|
# expect(result.stripe).toBeDefined()
|
|
|
|
# expect(result.stripe.free).toEqual(endDate)
|
|
|
|
# expect(result?.purchased?.gems).toEqual(10500)
|
|
|
|
# return fnDone(null, {status: "ok", msg: "Redeemed " + retry})
|
|
|
|
# else
|
|
|
|
# return fnDone(null, {status: 'error', msg: "Redeem attempt Error #{result} (#{userX.id})" + retry })
|
|
|
|
#
|
|
|
|
# redeemPrepaidFn = (code, testnum) =>
|
|
|
|
# (fnDone) =>
|
|
|
|
# loginNewUser (user1) =>
|
|
|
|
# doRedeem(user1, code, testnum, 0, fnDone)
|
|
|
|
#
|
|
|
|
# stripe.tokens.create {
|
|
|
|
# card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
|
|
|
# }, (err, token) ->
|
|
|
|
# loginNewUser (user) =>
|
|
|
|
# codeRedeemers = 50
|
|
|
|
# codeMonths = 3
|
|
|
|
# redeemers = 51
|
|
|
|
# purchasePrepaid 'terminal_subscription', months: codeMonths, codeRedeemers, token.id, (err, res, prepaid) ->
|
|
|
|
# expect(err).toBeNull()
|
|
|
|
# expect(prepaid).toBeDefined()
|
|
|
|
# expect(prepaid.code).toBeDefined()
|
|
|
|
# tasks = (redeemPrepaidFn(prepaid.code, i) for i in [0...redeemers])
|
|
|
|
# async.parallel tasks, (err, results) =>
|
|
|
|
# redeemed = 0
|
|
|
|
# error = 0
|
|
|
|
# for result in results
|
|
|
|
# redeemed += 1 if result.status is 'ok'
|
|
|
|
# error += 1 if result.status is 'error'
|
|
|
|
# expect(redeemed).toEqual(codeRedeemers)
|
|
|
|
# expect(error).toEqual(redeemers - codeRedeemers)
|
|
|
|
# done()
|