From 66b1e1b61dcbc5fe1d9d423addfcba5ab2405027 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Fri, 20 Nov 2015 13:17:33 -0800 Subject: [PATCH] Fix /db/prepaid//redeemers when the creator is one of the redeemed, and prepaid tests in general --- server/prepaids/prepaid_handler.coffee | 6 +- test/server/functional/prepaid.spec.coffee | 67 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/server/prepaids/prepaid_handler.coffee b/server/prepaids/prepaid_handler.coffee index 06b0c1671..d39e7ae3b 100644 --- a/server/prepaids/prepaid_handler.coffee +++ b/server/prepaids/prepaid_handler.coffee @@ -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) => diff --git a/test/server/functional/prepaid.spec.coffee b/test/server/functional/prepaid.spec.coffee index f950ae0bc..d7386d37b 100644 --- a/test/server/functional/prepaid.spec.coffee +++ b/test/server/functional/prepaid.spec.coffee @@ -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) ->