From d95cbc0627ed6af880721f997f0ab640f91ea2d7 Mon Sep 17 00:00:00 2001 From: Matt Lott Date: Wed, 7 Oct 2015 15:14:26 -0700 Subject: [PATCH] Course instance redeem API returns 200 for double redeem If a user has already redeemed course prepaid code, return 200 and the related course instances. Simplifies client-side experience. --- server/courses/course_instance_handler.coffee | 3 ++ .../functional/course_instance.spec.coffee | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/server/courses/course_instance_handler.coffee b/server/courses/course_instance_handler.coffee index 25abed4c8..a6e50509a 100644 --- a/server/courses/course_instance_handler.coffee +++ b/server/courses/course_instance_handler.coffee @@ -142,6 +142,9 @@ CourseInstanceHandler = class CourseInstanceHandler extends Handler return @sendDatabaseError(res, err) if err return @sendForbiddenError(res) if prepaid.get('redeemers')?.length >= prepaid.get('maxRedeemers') + if _.find((prepaid.get('redeemers') ? []), (a) -> a.userID.equals(req.user.id)) + return @sendSuccess(res, courseInstances) + # Add to prepaid redeemers query = _id: prepaid.get('_id') diff --git a/test/server/functional/course_instance.spec.coffee b/test/server/functional/course_instance.spec.coffee index 5084f48c5..ff6506a01 100644 --- a/test/server/functional/course_instance.spec.coffee +++ b/test/server/functional/course_instance.spec.coffee @@ -222,7 +222,7 @@ describe 'CourseInstance', -> describe 'Redeem prepaid code', -> - it 'Redeem prepaid code an instance of max 2', (done) -> + it 'Redeem prepaid code for an instance of max 2', (done) -> stripe.tokens.create { card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } }, (err, token) -> @@ -264,7 +264,7 @@ describe 'CourseInstance', -> expect(usersFound).toEqual(2) done() - it 'Redeem full prepaid code on instance of max 1', (done) -> + it 'Redeem full prepaid code for on instance of max 1', (done) -> stripe.tokens.create { card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } }, (err, token) -> @@ -326,3 +326,29 @@ describe 'CourseInstance', -> return done(err) if err expect(prepaid.get('redeemers')?.length).toEqual(prepaid.get('maxRedeemers')) done() + + it 'Redeem prepaid code twice', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user1) -> + createCourse 0, (err, course) -> + expect(err).toBeNull() + return done(err) if err + createCourseInstances user1, course.get('_id'), 2, token.id, (err, courseInstances) -> + expect(err).toBeNull() + return done(err) if err + expect(courseInstances.length).toEqual(1) + Prepaid.findById courseInstances[0].get('prepaidID'), (err, prepaid) -> + expect(err).toBeNull() + return done(err) if err + loginNewUser (user2) -> + # Redeem once + request.post {uri: courseInstanceRedeemURL, json: {prepaidCode: prepaid.get('code')} }, (err, res) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + # Redeem twice + request.post {uri: courseInstanceRedeemURL, json: {prepaidCode: prepaid.get('code')} }, (err, res) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + done()