Commit 44bea2ea by chrisndodge

Merge pull request #8305 from edx/cdodge/hotfix-support-multiple-regcode-redemptions

don't assume in the ORM query a single RegCodeRedemption per enrollme…
parents 8664fddb fdfda81a
...@@ -1185,10 +1185,15 @@ class RegistrationCodeRedemption(models.Model): ...@@ -1185,10 +1185,15 @@ class RegistrationCodeRedemption(models.Model):
Returns RegistrationCodeRedemption object if registration code Returns RegistrationCodeRedemption object if registration code
has been used during the course enrollment else Returns None. has been used during the course enrollment else Returns None.
""" """
try: # theoretically there could be more than one (e.g. someone self-unenrolls
return cls.objects.get(course_enrollment=course_enrollment) # then re-enrolls with a different regcode)
except RegistrationCodeRedemption.DoesNotExist: reg_codes = cls.objects.filter(course_enrollment=course_enrollment).order_by('-redeemed_at')
return None if reg_codes:
# return the first one. In all normal use cases of registration codes
# the user will only have one
return reg_codes[0]
return None
@classmethod @classmethod
def is_registration_code_redeemed(cls, course_reg_code): def is_registration_code_redeemed(cls, course_reg_code):
......
...@@ -27,7 +27,8 @@ from shoppingcart.models import ( ...@@ -27,7 +27,8 @@ from shoppingcart.models import (
Order, OrderItem, CertificateItem, Order, OrderItem, CertificateItem,
InvalidCartItem, CourseRegistrationCode, PaidCourseRegistration, CourseRegCodeItem, InvalidCartItem, CourseRegistrationCode, PaidCourseRegistration, CourseRegCodeItem,
Donation, OrderItemSubclassPK, Donation, OrderItemSubclassPK,
Invoice, CourseRegistrationCodeInvoiceItem, InvoiceTransaction, InvoiceHistory Invoice, CourseRegistrationCodeInvoiceItem, InvoiceTransaction, InvoiceHistory,
RegistrationCodeRedemption
) )
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
from student.models import CourseEnrollment from student.models import CourseEnrollment
...@@ -470,6 +471,60 @@ class PaidCourseRegistrationTest(ModuleStoreTestCase): ...@@ -470,6 +471,60 @@ class PaidCourseRegistrationTest(ModuleStoreTestCase):
# check that the registration codes are generated against the order # check that the registration codes are generated against the order
self.assertEqual(len(CourseRegistrationCode.objects.filter(order=self.cart)), item.qty) self.assertEqual(len(CourseRegistrationCode.objects.filter(order=self.cart)), item.qty)
def test_regcode_redemptions(self):
"""
Asserts the data model around RegistrationCodeRedemption
"""
self.cart.order_type = 'business'
self.cart.save()
CourseRegCodeItem.add_to_order(self.cart, self.course_key, 2)
self.cart.purchase()
reg_code = CourseRegistrationCode.objects.filter(order=self.cart)[0]
enrollment = CourseEnrollment.enroll(self.user, self.course_key)
redemption = RegistrationCodeRedemption(
registration_code=reg_code,
redeemed_by=self.user,
course_enrollment=enrollment
)
redemption.save()
test_redemption = RegistrationCodeRedemption.registration_code_used_for_enrollment(enrollment)
self.assertEqual(test_redemption.id, redemption.id) # pylint: disable=no-member
def test_regcode_multi_redemptions(self):
"""
Asserts the data model around RegistrationCodeRedemption and
what happens when we do multiple redemptions by same user
"""
self.cart.order_type = 'business'
self.cart.save()
CourseRegCodeItem.add_to_order(self.cart, self.course_key, 2)
self.cart.purchase()
reg_codes = CourseRegistrationCode.objects.filter(order=self.cart)
self.assertEqual(len(reg_codes), 2)
enrollment = CourseEnrollment.enroll(self.user, self.course_key)
ids = []
for reg_code in reg_codes:
redemption = RegistrationCodeRedemption(
registration_code=reg_code,
redeemed_by=self.user,
course_enrollment=enrollment
)
redemption.save()
ids.append(redemption.id) # pylint: disable=no-member
test_redemption = RegistrationCodeRedemption.registration_code_used_for_enrollment(enrollment)
self.assertIn(test_redemption.id, ids) # pylint: disable=no-member
def test_add_with_default_mode(self): def test_add_with_default_mode(self):
""" """
Tests add_to_cart where the mode specified in the argument is NOT in the database Tests add_to_cart where the mode specified in the argument is NOT in the database
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment