symposion_app/registrasion/tests/test_group_member.py

74 lines
2.4 KiB
Python
Raw Normal View History

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):
2016-09-15 05:33:43 +00:00
''' Creates cls.GROUP_1, and restricts cls.PROD_1 only to users who are
members of the group. Likewise GROUP_2 and PROD_2 '''
2016-09-15 05:33:43 +00:00
groups = []
products = [cls.PROD_1, cls.PROD_2]
for i, product in enumerate(products):
group = Group.objects.create(name="TEST GROUP" + str(i))
flag = conditions.GroupMemberFlag.objects.create(
description="Group member flag " + str(i),
condition=conditions.FlagBase.ENABLE_IF_TRUE,
)
flag.group.add(group)
flag.products.add(product)
2016-09-15 05:33:43 +00:00
groups.append(group)
2016-09-15 05:33:43 +00:00
cls.GROUP_1 = groups[0]
cls.GROUP_2 = groups[1]
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()
2016-09-15 05:33:43 +00:00
groups = [self.GROUP_1, self.GROUP_2]
products = [self.PROD_1, self.PROD_2]
for group, product in zip(groups, products):
# USER_1 cannot see PROD_1 until they're in GROUP.
available = ProductController.available_products(
self.USER_1,
products=[product],
)
self.assertNotIn(product, available)
self.USER_1.groups.add(group)
# USER_1 cannot see PROD_1 until they're in GROUP.
available = ProductController.available_products(
self.USER_1,
products=[product],
)
self.assertIn(product, available)
# USER_2 is still locked out
available = ProductController.available_products(
self.USER_2,
products=[product],
)
self.assertNotIn(product, available)