From 8e1f79951315b83d11e0928dc39d3aa6fd241e60 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 16:11:28 +1000 Subject: [PATCH 1/2] Test case for issue #68 --- registrasion/tests/test_flag.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/registrasion/tests/test_flag.py b/registrasion/tests/test_flag.py index 094ac1a4..4952c8a7 100644 --- a/registrasion/tests/test_flag.py +++ b/registrasion/tests/test_flag.py @@ -6,6 +6,7 @@ from registrasion.models import commerce from registrasion.models import conditions from registrasion.controllers.category import CategoryController from controller_helpers import TestingCartController +from controller_helpers import TestingInvoiceController from registrasion.controllers.product import ProductController from test_cart import RegistrationCartTestCase @@ -350,3 +351,29 @@ class FlagTestCases(RegistrationCartTestCase): # and also PROD_1, which is now exhausted for user. items = commerce.ProductItem.objects.filter(cart=cart.cart) self.assertTrue([i for i in items if i.product == self.PROD_1]) + + def test_oops(self): + ''' Flags should be enabled, even if *some* enabling products are cnx. + Tests issue #68. + ''' + + self.add_product_flag() + cart1 = TestingCartController.for_user(self.USER_1) + + with self.assertRaises(ValidationError): + # Can't do this without PROD_2 + cart1.add_to_cart(self.PROD_1, 1) + + cart1.add_to_cart(self.PROD_2, 1) + + inv = TestingInvoiceController.for_cart(cart1.cart) + inv.pay("Lol", inv.invoice.value) + + cart2 = TestingCartController.for_user(self.USER_1) + cart2.add_to_cart(self.PROD_2, 1) + + inv.refund() + + # Even though cart1 has been cancelled, we have the item in cart2. + # So we should be able to add PROD_1, which depends on PROD_2 + cart2.add_to_cart(self.PROD_1, 1) From 43649002cb0333a3ccb46dd19c798ce6c0ec32aa Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 16:18:27 +1000 Subject: [PATCH 2/2] Makes ProductCondition work if you have both valid and cancelled instances of a product. Fixes #68 --- registrasion/controllers/conditions.py | 11 ++++++++++- registrasion/tests/test_flag.py | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/registrasion/controllers/conditions.py b/registrasion/controllers/conditions.py index 51078016..72e592a2 100644 --- a/registrasion/controllers/conditions.py +++ b/registrasion/controllers/conditions.py @@ -172,11 +172,20 @@ class ProductConditionController(IsMetByFilter, ConditionController): in_user_carts = Q(enabling_products__productitem__cart__user=user) released = commerce.Cart.STATUS_RELEASED + paid = commerce.Cart.STATUS_PAID + active = commerce.Cart.STATUS_ACTIVE in_released_carts = Q( enabling_products__productitem__cart__status=released ) + not_in_paid_or_active_carts = ~( + Q(enabling_products__productitem__cart__status=paid) | + Q(enabling_products__productitem__cart__status=active) + ) + queryset = queryset.filter(in_user_carts) - queryset = queryset.exclude(in_released_carts) + queryset = queryset.exclude( + in_released_carts & not_in_paid_or_active_carts + ) return queryset diff --git a/registrasion/tests/test_flag.py b/registrasion/tests/test_flag.py index 4952c8a7..3d8914a1 100644 --- a/registrasion/tests/test_flag.py +++ b/registrasion/tests/test_flag.py @@ -352,7 +352,7 @@ class FlagTestCases(RegistrationCartTestCase): items = commerce.ProductItem.objects.filter(cart=cart.cart) self.assertTrue([i for i in items if i.product == self.PROD_1]) - def test_oops(self): + def test_product_stays_enabled_even_if_some_are_cancelled(self): ''' Flags should be enabled, even if *some* enabling products are cnx. Tests issue #68. ''' @@ -377,3 +377,8 @@ class FlagTestCases(RegistrationCartTestCase): # Even though cart1 has been cancelled, we have the item in cart2. # So we should be able to add PROD_1, which depends on PROD_2 cart2.add_to_cart(self.PROD_1, 1) + + cart2.set_quantity(self.PROD_2, 0) + + with self.assertRaises(ValidationError): + cart2.add_to_cart(self.PROD_1, 1)