def check_valid_cart(request): login = request.COOKIES.get('login_hash') if login is None or Consumer.loggedInUsers.get(login) is None: guest = request.COOKIES.get('guest_hash') if guest is None: return HttpResponse('fail') status = GuestShoppingCartLogic.check_empty_cart_guest(guest) if not status: status = GuestShoppingCartLogic.check_valid_cart(guest) if status is True: return HttpResponse('OK') else: return HttpResponse(status) else: return HttpResponse('fail') else: status = UserShoppingCartLogic.check_empty_cart_user(login) if not status: status = UserShoppingCartLogic.check_valid_cart(login) if status is True: return HttpResponse('OK') else: return HttpResponse(status) else: return HttpResponse('fail')
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 logout(request): if request.method == 'POST': login = request.COOKIES.get('login_hash') if login is not None: if Consumer.loggedInUsers.get(login) is not None: UserShoppingCartLogic.remove_shopping_cart_db( Consumer.loggedInUsers.get(login)) UserShoppingCartLogic.add_all_shopping_cart_to_user( Consumer.loggedInUsersShoppingCart[login]) del Consumer.loggedInUsers[login] del Consumer.loggedInUsersShoppingCart[login] return HttpResponse('success') return HttpResponse('fail')
def pay_all_guest(guest): if guest is not None: # check if cart has items empty = check_empty_cart_guest(guest) if empty is not True: purchase_id = 0 # if so, check foreach item if the requested amount exist cart_items = Consumer.guestShoppingCart[guest] # cart_items is a array consist of shopping_cart objects shopping_policy_status = UserShoppingCartLogic.shopping_policy_check( "guest", cart_items) if shopping_policy_status is not True: return shopping_policy_status message = check_stock_for_shopping_cart(cart_items) if message is not True: return message # if so, sum all items costs, get from costumer his credentials total_cost = 0 # for each item, calculate visible_discount for shopping_cart_item in cart_items: item = get_item(shopping_cart_item.item_id) new_price = UserShoppingCartLogic.get_new_price_for_item( item, shopping_cart_item) total_cost = total_cost + shopping_cart_item.item_quantity * new_price new_quantity = item.quantity - shopping_cart_item.item_quantity status = ItemsLogic.update_stock(item.id, new_quantity) if status is False: return 'Something went wrong with the purchase' # live alerts owners = Owners.get_owners_by_shop(item.shop_name) owners_name = [] for owner in owners: owners_name.append(owner.username) PurchasesAlerts.notify_purchasing_alerts( owners_name, '<strong>' + guest + '</strong> has bought item <a href="http://localhost:8000/app/item/?item_id=' + str(item.id) + '"># <strong>' + str(item.id) + '</strong></a> from your shop') pay_confirmation = ExternalSystems.payment.pay(total_cost, guest) if pay_confirmation is False: return 'Payment System Denied.' sup_confirmation = ExternalSystems.supply.supply_a_purchase( guest, purchase_id) if sup_confirmation is False: return 'Supply System Denied.' status = remove_shopping_cart_guest(guest) if status is False: return 'Something went wrong with the purchase' LoggerLogic.add_event_log("GUEST", "PAY ALL") return [purchase_id, total_cost] return 'Shopping cart is empty'
def add_item_to_cart(request): if request.method == 'POST': item_id = int(request.POST.get('item_id')) quantity = int(request.POST.get('quantity')) item = ItemsLogic.get_item(item_id) if item.quantity < quantity: return HttpResponse('Stock_Error') login = request.COOKIES.get('login_hash') if login is None: login = request.POST.get('login_hash') if login is not None and Consumer.loggedInUsers.get(login) is not None: username = Consumer.loggedInUsers.get(login) status = UserShoppingCartLogic.add_item_shopping_cart( login, ShoppingCartItem(username, item_id, quantity, None)) if status is False: return HttpResponse('fail') else: return HttpResponse('OK') else: if item.kind == 'ticket': return HttpResponse('guest ticket') guest = request.COOKIES.get('guest_hash') if guest is None: guest = 'guest' + Consumer.guestIndex Consumer.guestIndex += 1 status = GuestShoppingCartLogic.add_guest_item_shopping_cart( guest, item_id, quantity) if status is False: return HttpResponse('fail') else: string_guest = str(guest) return HttpResponse(string_guest)
def update_code_shopping_cart(request): if request.method == 'POST': code = request.POST.get("code") event = "UPDATE CODE SHOPPING CART" suspect_sql_injection = LoggerLogic.identify_sql_injection(code, event) if suspect_sql_injection: return HttpResponse(LoggerLogic.MESSAGE_SQL_INJECTION) item = ItemsLogic.get_item_by_code(code) if item is False: return HttpResponse('fail') login = request.COOKIES.get('login_hash') if login is None or Consumer.loggedInUsers.get(login) is None: guest = request.COOKIES.get('guest_hash') if guest is None: return HttpResponse('fail') status = GuestShoppingCartLogic.update_code_shopping_cart_guest( guest, item.id, code) else: status = UserShoppingCartLogic.update_code_shopping_cart( login, item.id, code) if status is False: return HttpResponse('fail') else: return HttpResponse('OK')
def test_add_lottery_customer(self): register(RegisteredUser('ToniToniToniToni', '12121212')) register(RegisteredUser('NoniNoni', '12121212')) user = get_user('ToniToniToniToni') user1user1 = get_user('NoniNoni') add_system_manager(SystemManager(user.username, user.password)) item1 = Item(1, 'My Shop', 'banana', 'vegas', 'good', 10, 500, 'prize', None, 0, 0, 0) item2 = Item(1, 'My Shop', 'banana', 'vegas', 'good', 10, 500, 'ticket', None, 0, 0, 0) add_lottery_and_items(item1, item2, 500, '2019-12-26 17:38', 'YoniYoni') lst = get_lotteries() lottery = lst[0] username = '******' access_token = hashlib.md5(username.encode()).hexdigest() Consumer.loggedInUsers[access_token] = username Consumer.loggedInUsersShoppingCart[access_token] = [] UserShoppingCartLogic.add_item_shopping_cart(access_token, ShoppingCartItem('NoniNoni', lottery.lotto_id, 3, None)) message = pay_all(access_token) customer_lottery = get_lottery_customer(lottery.lotto_id, 'NoniNoni') self.assertTrue(customer_lottery is not False) sleep(3) self.assertTrue(len(get_user_purchases('NoniNoni')) > 0)
def test_supply_system(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') 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)) ExternalSystems.supply = ProxySupplySystem.ProxySupplySystem() status = UserShoppingCartLogic.pay_all(access_token) if isinstance(status, list) is not True: status = False self.assertFalse(status) ExternalSystems.supply = SupplySystem.SupplySystem() status = UserShoppingCartLogic.pay_all(access_token) if isinstance(status, list): status = True self.assertTrue(status)
def shopping_cart_items_helper(request): login = request.COOKIES.get('login_hash') guest = request.COOKIES.get('guest_hash') context = { 'topbar': Topbar_Navbar.get_top_bar(login), 'navbar': Topbar_Navbar.get_nav_bar(login, guest) } if login is not None and Consumer.loggedInUsers.get(login) is not None: context.update(UserShoppingCartLogic.order_of_user(login)) else: if guest is not None: context.update(GuestShoppingCartLogic.order_of_guest(guest)) return context
def remove_item_shopping_cart(request): if request.method == 'POST': item_id = request.POST.get('item_id') login = request.COOKIES.get('login_hash') if login is not None and Consumer.loggedInUsers.get(login) is None: status = UserShoppingCartLogic.remove_item_shopping_cart( login, item_id) else: guest = request.COOKIES.get('guest_hash') if guest is None: return HttpResponse('fail') status = GuestShoppingCartLogic.remove_item_shopping_cart_guest( guest, item_id) if status is False: return HttpResponse('fail') else: return HttpResponse('OK')
def update_item_shopping_cart(request): if request.method == 'POST': item_id = int(request.POST.get("item_id")) new_quantity = int(request.POST.get("quantity")) login = request.COOKIES.get('login_hash') if login is None or Consumer.loggedInUsers.get(login) is None: guest = request.COOKIES.get('guest_hash') if guest is None: return HttpResponse('fail') status = GuestShoppingCartLogic.update_item_shopping_cart_guest( guest, item_id, new_quantity) else: status = UserShoppingCartLogic.update_item_shopping_cart( login, item_id, new_quantity) if status is False: return HttpResponse('fail') else: return HttpResponse('OK')
def pay_all(request): if request.method == 'POST': login = request.COOKIES.get('login_hash') if login is None: login = request.POST.get('login_hash') if login is None or Consumer.loggedInUsers.get(login) is None: guest = request.COOKIES.get('guest_hash') if guest is None: return HttpResponse('fail') username = '******' message = GuestShoppingCartLogic.pay_all_guest(guest) else: username = Consumer.loggedInUsers.get(login) message = UserShoppingCartLogic.pay_all(login) if isinstance(message, list): to_send = 'OK' + str(message[0]) + '}' + str(message[1]) return HttpResponse(to_send) else: LoggerLogic.add_error_log(username, "PAY ALL", message) return HttpResponse(message)
def get_nav_bar(login, guest): cart_count = 0 user_buttons = '' if login is not None: username = Consumer.loggedInUsers.get(login) if username is not None: cart_count = len(UserShoppingCartLogic.get_cart_items(login)) user_buttons = loader.render_to_string( 'components/UserNavButtons.html', context={}) else: if guest is not None: cart_count = len( GuestShoppingCartLogic.get_guest_shopping_cart_item(guest)) else: if guest is not None: cart_count = len( GuestShoppingCartLogic.get_guest_shopping_cart_item(guest)) return loader.render_to_string('components/NavbarButtons.html', context={ 'cart_items': cart_count, 'user_buttons': user_buttons })
def buy_item(login_token, username, shop_name, item_id, quantity): return UserShoppingCartLogic.add_item_shopping_cart( login_token, ShoppingCartItem(username, item_id, quantity, ""))
def pay_cart(login_token): return UserShoppingCartLogic.pay_all(login_token)
def test_discounts_torture(self): status = UsersLogic.register(RegisteredUser('YoniYoni', "SsS0897SsS")) self.assertEqual(status, "SUCCESS") status = UsersLogic.update_details('YoniYoni', 'AFG', 20, 'Male') self.assertEqual(status, "SUCCESS") status = UsersLogic.register(RegisteredUser('YoniYoni2', "SsS0897SsS")) self.assertEqual(status, "SUCCESS") status = UsersLogic.update_details('YoniYoni2', 'ZMB', 20, 'Male') self.assertEqual(status, "SUCCESS") status = UsersLogic.register(RegisteredUser('YoniYoni3', "SsS0897SsS")) self.assertEqual(status, "SUCCESS") status = UsersLogic.update_details('YoniYoni3', 'ISR', 100, 'Female') self.assertEqual(status, "SUCCESS") status = ShopLogic.create_shop(Shop('eBay', "Active"), 'YoniYoni') self.assertEqual(status, "SUCCESS") status = ShopLogic.create_shop(Shop('Amazon', "Active"), 'YoniYoni2') self.assertEqual(status, "SUCCESS") status = ShopLogic.create_shop(Shop('Carmel', "Active"), 'YoniYoni3') self.assertEqual(status, "SUCCESS") item1 = Item(1, 'eBay', 'carrot', 'vegas', 'good', 10, 500, 'regular', None, 0, 0, 0) item2 = Item(2, 'Amazon', 'apple', 'fruits', 'good', 10, 500, 'regular', None, 0, 0, 0) item3 = Item(3, 'Carmel', 'Kipa', 'Yudaika', 'good', 10, 500, 'regular', None, 0, 0, 0) status = ItemsLogic.add_item_to_shop(item1, 'YoniYoni') self.assertTrue(status) status = ItemsLogic.add_item_to_shop(item2, 'YoniYoni2') self.assertTrue(status) status = ItemsLogic.add_item_to_shop(item3, 'YoniYoni3') self.assertTrue(status) disc1 = VisibleDiscount(item1.id, item1.shop_name, 10, '2018-17-06', '2019-21-03') disc2 = VisibleDiscount(item2.id, item2.shop_name, 20, '2018-17-06', '2019-22-03') disc3 = VisibleDiscount(item3.id, item3.shop_name, 30, '2018-17-06', '2019-23-03') status = DiscountLogic.add_visible_discount(disc1, 'YoniYoni') self.assertTrue(status) status = DiscountLogic.add_visible_discount(disc2, 'YoniYoni2') self.assertTrue(status) status = DiscountLogic.add_visible_discount(disc3, 'YoniYoni3') self.assertTrue(status) disc1 = VisibleDiscountCategory(item1.category, item1.shop_name, 10, '2018-17-06', '2019-21-03') disc2 = VisibleDiscountCategory(item2.category, item2.shop_name, 20, '2018-17-06', '2019-22-03') disc3 = VisibleDiscountCategory(item3.category, item3.shop_name, 30, '2018-17-06', '2019-23-03') status = DiscountLogic.add_visible_discount_category(disc1, 'YoniYoni') self.assertTrue(status) status = DiscountLogic.add_visible_discount_category( disc2, 'YoniYoni2') self.assertTrue(status) status = DiscountLogic.add_visible_discount_category( disc3, 'YoniYoni3') self.assertTrue(status) access_token = hashlib.md5('YoniYoni'.encode()).hexdigest() Consumer.loggedInUsers[access_token] = 'YoniYoni' Consumer.loggedInUsersShoppingCart[access_token] = [] status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni', 2, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni', 3, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.get_cart_cost(access_token) self.assertEqual(status, 113) status = UserShoppingCartLogic.pay_all(access_token) self.assertTrue(StoB(status)) access_token = hashlib.md5('YoniYoni2'.encode()).hexdigest() Consumer.loggedInUsers[access_token] = 'YoniYoni2' Consumer.loggedInUsersShoppingCart[access_token] = [] status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni2', 1, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni2', 3, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.get_cart_cost(access_token) self.assertEqual(status, 130) status = UserShoppingCartLogic.pay_all(access_token) self.assertTrue(StoB(status)) access_token = hashlib.md5('YoniYoni3'.encode()).hexdigest() Consumer.loggedInUsers[access_token] = 'YoniYoni3' Consumer.loggedInUsersShoppingCart[access_token] = [] status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni3', 2, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.add_item_shopping_cart( access_token, ShoppingCartItem('YoniYoni3', 1, 10, None)) self.assertTrue(status) status = UserShoppingCartLogic.get_cart_cost(access_token) self.assertEqual(status, 145) status = UserShoppingCartLogic.pay_all(access_token) self.assertTrue(StoB(status))
def get_cart_cost(username): return UserShoppingCartLogic.get_cart_cost(username)
def test_torture3(self): # Adding Users status = UsersLogic.register( RegisteredUser('u1ser1u1ser1', 'wxde12exd12')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u2ser2u2ser2', '34c124c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u3ser3u3ser3', '1c241c24c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u4ser4u4ser4', '3214v132v4132')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u5seru5ser', '12121212')) self.assertTrue(status) # Adding System Managers status = UsersLogic.add_system_manager( SystemManager('sys1sys1', 'POWER123')) self.assertTrue(status) # Creating Shops status = ShopLogic.create_shop(Shop('myShop1', 'Active'), 'u1ser1u1ser1') self.assertTrue(status) status = ShopLogic.create_shop(Shop('myShop2', 'Active'), 'u2ser2u2ser2') self.assertTrue(status) status = UsersLogic.add_owner('u1ser1u1ser1', Owner('u3ser3u3ser3', 'myShop1', 0)) self.assertTrue(status) owner = Owners.get_owner('u1ser1u1ser1', 'myShop1') status = UsersLogic.add_manager( owner.username, StoreManager('u4ser4u4ser4', 'myShop1', 1, 1, 1, 1, 1, 1, 1, 1)) status = UsersLogic.add_manager( 'u2ser2u2ser2', StoreManager('u4ser4u4ser4', 'myShop2', 1, 1, 1, 1, 1, 1, 1, 1)) ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'banana', 'fruits', 'fruit;healthy;yellow', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'doll', 'toys', 'fun', 30, 10, 'regular', None, 0, 0, 0), 'u2ser2u2ser2') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'soda', 'drinks', 'good', 4.90, 20, 'regular', None, 0, 0, 0), 'u1ser1u1ser1') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'cucumber', 'vegetables', 'fun', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'vodka', 'drinks', 'bad;for;your;health', 70, 2, 'regular', None, 0, 0, 0), 'u3ser3u3ser3') username1 = 'u4ser4u4ser4' username2 = 'u2ser2u2ser2' username3 = 'u1ser1u1ser1' username4 = 'u3ser3u3ser3' username5 = 'u5seru5ser' access_token1 = hashlib.md5(username1.encode()).hexdigest() Consumer.loggedInUsers[access_token1] = username1 Consumer.loggedInUsersShoppingCart[access_token1] = [] access_token2 = hashlib.md5(username2.encode()).hexdigest() Consumer.loggedInUsers[access_token2] = username2 Consumer.loggedInUsersShoppingCart[access_token2] = [] access_token3 = hashlib.md5(username3.encode()).hexdigest() Consumer.loggedInUsers[access_token3] = username3 Consumer.loggedInUsersShoppingCart[access_token3] = [] access_token4 = hashlib.md5(username4.encode()).hexdigest() Consumer.loggedInUsers[access_token4] = username4 Consumer.loggedInUsersShoppingCart[access_token4] = [] access_token5 = hashlib.md5(username5.encode()).hexdigest() Consumer.loggedInUsers[access_token5] = username5 Consumer.loggedInUsersShoppingCart[access_token5] = [] UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 1, 10, None)) UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 2, 5, None)) UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 3, 15, None)) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 3) self.assertEqual(items[0].code, None) UserShoppingCartLogic.remove_item_shopping_cart(access_token5, 1) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 2) UserShoppingCartLogic.remove_item_shopping_cart(access_token5, 2) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 1) # Only item id 3 left UserShoppingCartLogic.pay_all(access_token5) items1 = UsersLogic.get_purchase_history('u5seru5ser') items2 = ItemsLogic.get_all_purchased_items('sys1sys1') items3 = ShopLogic.get_shop_purchase_history('u4ser4u4ser4', 'myShop1') self.assertEqual(items1[0].item_id, items2[0].item_id) self.assertEqual(items2[0].quantity, items3[0].quantity) self.assertEqual(items1[0].price, items3[0].price) self.assertTrue('Nadav Ha Gever')
def is_item_bought(login_token, item_id): for item in UserShoppingCartLogic.get_cart_items(login_token): if item.item_id == item_id: return True return False