mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 15:48:11 -05:00
Fix /db/prepaid/<id>/redeemers when the creator is one of the redeemed, and prepaid tests in general
This commit is contained in:
parent
7ede7928ca
commit
66b1e1b61d
2 changed files with 68 additions and 5 deletions
|
@ -80,13 +80,11 @@ PrepaidHandler = class PrepaidHandler extends Handler
|
|||
return @sendDatabaseError(res, err) if err
|
||||
return @sendNotFoundError(res, 'User for given ID not found') if not user
|
||||
userID = user.get('_id')
|
||||
# Prepaid.count {'redeemers.userID': userID}, (err, count) =>
|
||||
# return @sendDatabaseError(res, err) if err
|
||||
# return @sendSuccess(res, @formatEntity(req, prepaid)) if count
|
||||
return @sendSuccess(res, @formatEntity(req, prepaid)) if user.get('coursePrepaidID')
|
||||
|
||||
query =
|
||||
_id: prepaid.get('_id')
|
||||
'redeemers.userID': { $ne: req.user.get('_id') }
|
||||
'redeemers.userID': { $ne: user.get('_id') }
|
||||
$where: "this.redeemers.length < #{prepaid.get('maxRedeemers')}"
|
||||
update = { $push: { redeemers : { date: new Date(), userID: userID } }}
|
||||
Prepaid.update query, update, (err, nMatched) =>
|
||||
|
|
|
@ -43,6 +43,7 @@ describe '/db/prepaid', ->
|
|||
redeemers: [],
|
||||
creator: user1.get('_id')
|
||||
code: 0
|
||||
type: 'course'
|
||||
})
|
||||
prepaid.save (err, prepaid) ->
|
||||
otherUser = new User()
|
||||
|
@ -67,6 +68,7 @@ describe '/db/prepaid', ->
|
|||
redeemers: [],
|
||||
creator: user1.get('_id')
|
||||
code: 1
|
||||
type: 'course'
|
||||
})
|
||||
prepaid.save (err, prepaid) ->
|
||||
otherUser = new User()
|
||||
|
@ -84,6 +86,7 @@ describe '/db/prepaid', ->
|
|||
redeemers: [],
|
||||
creator: user1.get('_id')
|
||||
code: 2
|
||||
type: 'course'
|
||||
})
|
||||
prepaid.save (err, prepaid) ->
|
||||
loginNewUser (user2) ->
|
||||
|
@ -97,11 +100,14 @@ describe '/db/prepaid', ->
|
|||
|
||||
it 'is idempotent across prepaids collection', (done) ->
|
||||
loginNewUser (user1) ->
|
||||
otherUser = new User()
|
||||
otherUser = new User({
|
||||
'coursePrepaidID': new ObjectId()
|
||||
})
|
||||
otherUser.save (err, otherUser) ->
|
||||
prepaid1 = new Prepaid({
|
||||
redeemers: [{userID: otherUser.get('_id')}],
|
||||
code: 3
|
||||
type: 'course'
|
||||
})
|
||||
prepaid1.save (err, prepaid1) ->
|
||||
prepaid2 = new Prepaid({
|
||||
|
@ -109,6 +115,7 @@ describe '/db/prepaid', ->
|
|||
redeemers: [],
|
||||
creator: user1.get('_id')
|
||||
code: 4
|
||||
type: 'course'
|
||||
})
|
||||
prepaid2.save (err, prepaid2) ->
|
||||
url = getURL("/db/prepaid/#{prepaid2.id}/redeemers")
|
||||
|
@ -118,6 +125,64 @@ describe '/db/prepaid', ->
|
|||
return done() unless res.statusCode is 200
|
||||
expect(body.redeemers.length).toBe(0)
|
||||
done()
|
||||
|
||||
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()
|
||||
|
||||
it 'Clear database', (done) ->
|
||||
clearModels [Course, CourseInstance, Payment, Prepaid, User], (err) ->
|
||||
|
|
Loading…
Reference in a new issue