From 1128e43150bba9753ebc3d81fa4370beffb2dc74 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Mon, 5 Sep 2016 10:18:08 +1000 Subject: [PATCH] =?UTF-8?q?Adds=20test=20for=20GroupMemberCondition=20?= =?UTF-8?q?=E2=80=94=20it=20fails,=20obviously.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- registrasion/tests/test_group_member.py | 65 +++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 registrasion/tests/test_group_member.py diff --git a/registrasion/tests/test_group_member.py b/registrasion/tests/test_group_member.py new file mode 100644 index 00000000..84b194aa --- /dev/null +++ b/registrasion/tests/test_group_member.py @@ -0,0 +1,65 @@ +import pytz + +from django.contrib.auth.models import Group +from django.core.exceptions import ValidationError + +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 + +UTC = pytz.timezone('UTC') + + +class GroupMemberTestCase(RegistrationCartTestCase): + + @classmethod + def _create_group_and_flag(cls): + ''' Creates cls.GROUP, and restricts cls.PROD_1 only to users who are + members of the group. ''' + + group = Group.objects.create( + name="TEST GROUP", + ) + + flag = conditions.GroupMemberFlag.objects.create( + description="Group member flag", + condition=conditions.FlagBase.ENABLE_IF_TRUE, + ) + flag.group.add(group) + flag.products.add(cls.PROD_1) + + cls.GROUP = group + + def test_product_not_enabled_until_user_joins_group(self): + ''' Tests that GroupMemberFlag disables a product for a user until + they are a member of a specific group. ''' + + self._create_group_and_flag() + + # USER_1 cannot see PROD_1 until they're in GROUP. + available = ProductController.available_products( + self.USER_1, + products=[self.PROD_1], + ) + self.assertNotIn(self.PROD_1, available) + + self.USER_1.groups.add(self.GROUP) + + # USER_1 cannot see PROD_1 until they're in GROUP. + available = ProductController.available_products( + self.USER_1, + products=[self.PROD_1], + ) + self.assertIn(self.PROD_1, available) + + # USER_2 is still locked out + available = ProductController.available_products( + self.USER_2, + products=[self.PROD_1], + ) + self.assertNotIn(self.PROD_1, available)