def test_policies_torture(self): UsersLogic.register(RegisteredUser('ShaharBenS', "SsS0897SsS")) UsersLogic.update_details('ShaharBenS', 'AFG', 20, 'Male') UsersLogic.register(RegisteredUser('ShaharBenS2', "SsS0897SsS")) ShopLogic.create_shop(Shop('eBay', "Active"), 'ShaharBenS2') ShopLogic.create_shop(Shop('Amazon', "Active"), 'ShaharBenS2') item1 = Item(1, 'eBay', 'apple', 'vegas', 'good', 10, 500, 'regular', None, 0, 0, 0) item2 = Item(2, 'Amazon', 'apple', 'fruits', 'good', 10, 500, 'regular', None, 0, 0, 0) ItemsLogic.add_item_to_shop(item1, 'ShaharBenS2') ItemsLogic.add_item_to_shop(item2, 'ShaharBenS2') ShoppingPolicyLogic.add_shopping_policy_on_shop( 'ShaharBenS2', 'eBay', "age = ''20''", "AL", 3) ShoppingPolicyLogic.add_shopping_policy_on_shop( 'ShaharBenS2', 'Amazon', "age > ''15''", "UT", 5) ShoppingPolicyLogic.add_shopping_policy_on_identity( 'Ultimate_ShaharShahar', "sex = ''Male''", "AL", 9) ShoppingPolicyLogic.add_shopping_policy_on_category( 'Ultimate_ShaharShahar', "vegas", "state = ''AFG''", "UT", 5) ShoppingPolicyLogic.add_shopping_policy_on_items( 'Ultimate_ShaharShahar', "apple", "state != ''AFG''", "E", 2) access_token = hashlib.md5('ShaharBenS'.encode()).hexdigest() Consumer.loggedInUsers[access_token] = 'ShaharBenS' Consumer.loggedInUsersShoppingCart[access_token] = [] UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('ShaharBenS', 2, 3, None)) UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('ShaharBenS', 1, 7, None)) status = UserShoppingCartLogic.pay_all(access_token) self.assertFalse(StoB(status))
def getShopShoppingPolicies(request): if request.method == 'GET': shop_name = request.GET.get('shop_name') shop_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_shop(shop_name) html = "" for policy in shop_policies: is_none = "" selectors = {} if policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[policy.restriction] = 'selected="selected"' html += loader.render_to_string('components/shopping_shop_policy.html', context={ 'id': policy.policy_id, 'selector_value': policy.restriction, 'quantity': policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) return HttpResponse(html)
def check_shop_shopping_policies(username, cart_items): shop_policies = ShoppingPolicyLogic.get_all_shops_shopping_policies() for shop_policy in shop_policies: if not (username == "guest"): if is_meet_conditions(username, shop_policy.conditions) is False: continue if shop_policy.restriction == 'N': continue num_of_items = 0 relevant = False cart_item_shop = None for cart_item in cart_items: cart_item_shop = ItemsLogic.get_item(cart_item.item_id).shop_name if shop_policy.shop_name == cart_item_shop: num_of_items = num_of_items + cart_item.item_quantity relevant = True if shop_policy.restriction == 'AL': if relevant and num_of_items < shop_policy.quantity: return "FAILED: Not enough " + cart_item_shop + " items in cart; You allowed at least " + str( shop_policy.quantity) elif shop_policy.restriction == 'E': if relevant and num_of_items != shop_policy.quantity: return "FAILED: Not exact num of " + cart_item_shop + " items in cart; You allowed exactly " + str( shop_policy.quantity) elif shop_policy.restriction == 'UT': if relevant and num_of_items > shop_policy.quantity: return "FAILED: Too much " + cart_item_shop + " items in cart; You allowed at most " + str( shop_policy.quantity) return True
def check_category_shopping_policies(username, cart_items): category_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_category( ) for category_policy in category_policies: if username is not "guest": if is_meet_conditions(username, category_policy.conditions) is False: continue if category_policy.restriction is 'N': continue num_of_items = 0 cart_item_category = None for cart_item in cart_items: cart_item_category = ItemsLogic.get_item( cart_item.item_id).category if category_policy.category == cart_item_category: num_of_items = num_of_items + cart_item.item_quantity if category_policy.restriction is 'AL': if num_of_items < category_policy.quantity: return "FAILED: Not enough " + cart_item_category + " items in cart; You allowed at least " + category_policy.quantity elif category_policy.restriction is 'E': if num_of_items != category_policy.quantity: return "FAILED: Not exact num of " + cart_item_category + " items in cart; You allowed exactly " + category_policy.quantity elif category_policy.restriction is 'UT': if num_of_items > category_policy.quantity: return "FAILED: Too much " + cart_item_category + " items in cart; You allowed at most " + category_policy.quantity return True
def getItemShoppingPolicyConditions(request): if request.method == 'GET': policy_id = request.GET.get('policy_id') item_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_items() for SP in item_policies: if SP.policy_id == int(policy_id): if SP.conditions == "1=1": return HttpResponse("") return HttpResponse(SP.conditions.replace("'", "''")) return HttpResponse("FAILED: Can't find that policy")
def getCategoryShoppingPolicyConditions(request): if request.method == 'GET': policy_id = request.GET.get('policy_id') category_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_category() for CP in category_policies: if CP.policy_id == int(policy_id): if CP.conditions == "1=1": return HttpResponse("") return HttpResponse(CP.conditions.replace("'", "''")) return HttpResponse("FAILED: Can't find that policy")
def getGlobalShoppingPolicyConditions(request): if request.method == 'GET': policy_id = request.GET.get('policy_id') global_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() for GP in global_policies: if GP.policy_id == int(policy_id): if GP.conditions == "1=1": return HttpResponse("") return HttpResponse(GP.conditions.replace("'", "''")) return HttpResponse("FAILED: Can't find that policy")
def getShopShoppingPolicyConditions(request): if request.method == 'GET': shop_name = request.GET.get('shop_name') policy_id = request.GET.get('policy_id') shop_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_shop(shop_name) for SP in shop_policies: if SP.policy_id == int(policy_id): if SP.conditions == "1=1": return HttpResponse("") return HttpResponse(SP.conditions.replace("'", "''")) return HttpResponse("FAILED: Can't find that policy")
def remove_shopping_policy_on_category(request): if request.method == 'POST': login = request.COOKIES.get('login_hash') if login is not None: username = Consumer.loggedInUsers.get(login) if username is None: return HttpResponse('FAILED: username is None') policy_id = request.POST.get('policy_id') status = ShoppingPolicyLogic.remove_shopping_policy_on_category(username, policy_id) if status is not True: return HttpResponse(status) return HttpResponse('SUCCESS') return HttpResponse('FAILED: you are not logged in!') return HttpResponse('FAILED: not a POST request')
def update_shopping_policy_on_items(request): if request.method == 'POST': login = request.COOKIES.get('login_hash') if login is not None: username = Consumer.loggedInUsers.get(login) if username is None: return HttpResponse('FAILED: username is None') policy_id = request.POST.get('policy_id') field_name = request.POST.get('field_name') new_value = request.POST.get('new_value') status = ShoppingPolicyLogic.update_shopping_policy_on_items(username, policy_id, field_name, new_value) if status is not True: return HttpResponse(status) return HttpResponse('SUCCESS') return HttpResponse('FAILED: you are not logged in!') return HttpResponse('FAILED: not a POST request')
def add_shopping_policy_on_identity(request): if request.method == 'POST': login = request.COOKIES.get('login_hash') if login is not None: username = Consumer.loggedInUsers.get(login) if username is None: return HttpResponse('FAILED: username is None') conditions = request.POST.get('conditions') restriction = request.POST.get('restriction') quantity = request.POST.get('quantity') status = ShoppingPolicyLogic.add_shopping_policy_on_identity(username, conditions, restriction, quantity) if status is not True: return HttpResponse(status) return HttpResponse('SUCCESS') return HttpResponse('FAILED: you are not logged in!') return HttpResponse('FAILED: not a POST request')
def check_identity_shopping_policies(username, cart_items): identity_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity( ) for identity_policy in identity_policies: if username is not "guest": if is_meet_conditions(username, identity_policy.conditions) is False: continue if identity_policy.restriction is 'N': continue elif identity_policy.restriction is 'AL': if len(cart_items) < identity_policy.quantity: return "FAILED: Not enough items in cart; You allowed at least " + identity_policy.quantity elif identity_policy.restriction is 'E': if len(cart_items) != identity_policy.quantity: return "FAILED: Not exact num of items in cart; You allowed exactly " + identity_policy.quantity elif identity_policy.restriction is 'UT': if len(cart_items) > identity_policy.quantity: return "FAILED: Too much items in cart; You allowed at most " + identity_policy.quantity return True
def check_identity_shopping_policies(username, cart_items): identity_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() for identity_policy in identity_policies: if not (username == "guest"): if is_meet_conditions(username, identity_policy.conditions) is False: continue if identity_policy.restriction == 'N': continue num_of_items = 0 for cart_item in cart_items: num_of_items = num_of_items + cart_item.item_quantity if identity_policy.restriction == 'AL': if num_of_items < identity_policy.quantity: return "FAILED: Not enough items in cart; You allowed at least " + str(identity_policy.quantity) elif identity_policy.restriction == 'E': if num_of_items != identity_policy.quantity: return "FAILED: Not exact num of items in cart; You allowed exactly " + str(identity_policy.quantity) elif identity_policy.restriction == 'UT': if num_of_items > identity_policy.quantity: return "FAILED: Too much items in cart; You allowed at most " + str(identity_policy.quantity) return True
def test_condition_bad_syntax(self): ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "N", 0) ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0) ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0) ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0) status = StoB(ShoppingPolicyLogic.update_shopping_policy_on_shop('ShaharBenS2', 1, 'conditions', "age >> 18 AND sex = ''Male''", 'eBay')) self.assertFalse(status) status = StoB(ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, 'conditions', "state1 = ''AFG'' AND sex = ''Male''")) self.assertFalse(status) status = StoB(ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, 'conditions', "(age > 18 AND sex = ''Male'') OR (NOT state != ''ZMBZA'')", )) self.assertFalse(status) status = StoB(ShoppingPolicyLogic.update_shopping_policy_on_identity('Ultimate_ShaharShahar', 1, 'conditions', "NOT sex != ''Female'' (DELETE * FROM *)")) self.assertFalse(status)
def get_system_policies(request): if request.method == 'GET': login = request.COOKIES.get('login_hash') if login is not None: username = Consumer.loggedInUsers.get(login) if username is not None: if UsersLogic.is_system_manager(username): item_policies_html = "" item_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_items( ) for item_policy in item_policies: is_none = "" selectors = {} if item_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[ item_policy.restriction] = 'selected="selected"' item_policies_html += loader.render_to_string( 'components/shopping_item_policy.html', context={ 'id': item_policy.policy_id, 'item_name': item_policy.item_name, 'selector_value': item_policy.restriction, 'quantity': item_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) category_policies_html = "" category_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_category( ) for category_policy in category_policies: is_none = "" selectors = {} if category_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[category_policy. restriction] = 'selected="selected"' category_policies_html += loader.render_to_string( 'components/shopping_category_policy.html', context={ 'id': category_policy.policy_id, 'category_name': category_policy.category, 'selector_value': category_policy.restriction, 'quantity': category_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) global_policies_html = "" global_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity( ) for global_policy in global_policies: is_none = "" selectors = {} if global_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[ global_policy.restriction] = 'selected="selected"' global_policies_html += loader.render_to_string( 'components/shopping_global_policy.html', context={ 'id': global_policy.policy_id, 'selector_value': global_policy.restriction, 'quantity': global_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) topbar = loader.render_to_string( 'components/TopbarLoggedIn.html', context={'username': username}) cart_count = len(ShoppingLogic.get_cart_items(username)) navbar = loader.render_to_string( 'components/NavbarButtons.html', context={'cart_items': cart_count}) return render(request, 'system-policies.html', context={ 'topbar': topbar, 'navbar': navbar, 'item_policies': item_policies_html, 'category_policies': category_policies_html, 'global_policies': global_policies_html }) return HttpResponse("You don't have the privilege to be here")
def test_add_get_policy(self): status = ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "UT", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) IP = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() CP = ShoppingPolicyLogic.get_all_shopping_policy_on_category() ITP = ShoppingPolicyLogic.get_all_shopping_policy_on_items() SP = ShoppingPolicyLogic.get_all_shopping_policy_on_shop('eBay') self.assertEqual(len(IP), 2) self.assertEqual(len(CP), 2) self.assertEqual(len(ITP), 2) self.assertEqual(len(SP), 2)
def get_all_shopping_policy_on_category(request): if request.method == 'GET': return ShoppingPolicyLogic.get_all_shopping_policy_on_category() return HttpResponse('FAILED')
def test_update_policy(self): status = True status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0)) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_shop('ShaharBenS2', 1, "restriction", "N", 'eBay')) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_shop('ShaharBenS2', 1, "quantity", 10, 'eBay')) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_identity('Ultimate_ShaharShahar', 1, "restriction", "AL")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_identity('Ultimate_ShaharShahar', 1, "quantity", 4)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "restriction", "E")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "quantity", 3)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "category", "books")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "restriction", "UT")) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "quantity", 10)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "item_name", "DP by GoF")) self.assertTrue(status) IP = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() CP = ShoppingPolicyLogic.get_all_shopping_policy_on_category() ITP = ShoppingPolicyLogic.get_all_shopping_policy_on_items() SP = ShoppingPolicyLogic.get_all_shopping_policy_on_shop('eBay') self.assertEqual(IP[0].restriction, "AL") self.assertEqual(IP[0].quantity, 4) self.assertEqual(CP[0].restriction, "E") self.assertEqual(CP[0].quantity, 3) self.assertEqual(CP[0].category, "books") self.assertEqual(ITP[0].restriction, "UT") self.assertEqual(ITP[0].quantity, 10) self.assertEqual(ITP[0].item_name, "DP by GoF") self.assertEqual(SP[0].restriction, "N") self.assertEqual(SP[0].quantity, 10)