def test_intra_request_user_changing(rf, regular_user): get_default_shop() # Create a shop mw = ShoopFrontMiddleware() request = apply_request_middleware(rf.get("/"), user=regular_user) mw.process_request(request) assert request.person == get_person_contact(regular_user) logout(request) assert request.user == AnonymousUser() assert request.person == AnonymousContact() assert request.customer == AnonymousContact()
def test_with_inactive_contact(rf, regular_user, admin_user): get_default_shop() # Create a shop # Get or create contact for regular user contact = get_person_contact(regular_user) assert contact.is_active contact.is_active = False contact.save() request = apply_request_middleware(rf.get("/"), user=regular_user) mw = ShoopFrontMiddleware() mw.process_request(request) assert request.user == AnonymousUser() assert request.person == AnonymousContact() assert request.customer == AnonymousContact()
def test_package_orderability(): package_product = get_package_product() shop = get_default_shop() sp = package_product.get_shop_instance(shop) supplier = sp.suppliers.get() assert not list( sp.get_orderability_errors( supplier=supplier, quantity=1, customer=AnonymousContact()))
def test_get_company_contact(regular_user): person_contact = get_person_contact(regular_user) assert person_contact != AnonymousContact() assert not get_company_contact(regular_user) company_contact = create_random_company() company_contact.members.add(person_contact) assert get_company_contact(regular_user) == company_contact
def test_anonymous_contact(): a1 = AnonymousContact() a2 = AnonymousContact() # Basic Contact stuff assert a1.is_anonymous, "AnonymousContact is anonymous" assert not a1.is_all_seeing, "AnonymousContact is not all seeing" assert a1.identifier is None assert a1.is_active, "AnonymousContact is active" assert a1.language == '' assert a1.marketing_permission assert a1.phone == '' assert a1.www == '' assert a1.timezone is None assert a1.prefix == '' assert a1.name == '', "AnonymousContact has no name" assert a1.suffix == '' assert a1.name_ext == '' assert a1.email == '', "AnonymousContact has no email" assert str(a1) == '' # Primary key / id assert a1.pk is None assert a1.id is None # AnonymousContact instance evaluates as false assert not a1 # All AnonymousContacts should be equal assert a1 == a2 # Cannot be saved with pytest.raises(NotImplementedError): a1.save() # Cannot be deleted with pytest.raises(NotImplementedError): a1.delete() assert isinstance(a1.groups, QuerySet) assert a1.groups.first().identifier == AnonymousContact.default_contact_group_identifier assert a1.groups.count() == 1 assert len(a1.groups.all()) == 1
def test_anonymous_customers_default_group(rf): request, shop, group = initialize_test(rf, True) discount_value = 49 product = create_product("random-52", shop=shop, default_price=121) request.customer = AnonymousContact() CgpPrice.objects.create(product=product, group=request.customer.get_default_group(), shop=shop, price_value=discount_value) price_info = product.get_price_info(request) assert price_info.price == shop.create_price(discount_value)
def get_price_info(shop, customer, product, quantity): """ Get price info of given product for given context parameters. :type shop: shoop.core.models.Shop :type customer: shoop.core.models.Contact :type product: shoop.core.models.Product :type quantity: numbers.Number """ pricing_mod = get_pricing_module() pricing_ctx = pricing_mod.get_context_from_data( shop=shop, customer=(customer or AnonymousContact()), ) return pricing_mod.get_price_info(pricing_ctx, product, quantity=quantity)
def test_customer_is_anonymous(rf): request, shop, group = initialize_test(rf, True) price = shop.create_price product = create_product("random-1", shop=shop, default_price=100) CgpPrice.objects.create(product=product, group=group, shop=shop, price_value=50) request.customer = AnonymousContact() price_info = product.get_price_info(request) assert price_info.price == price(100)
def test_package(): shop = get_default_shop() supplier = get_default_supplier() package_product = create_product("PackageParent", shop=shop, supplier=supplier) assert not package_product.get_package_child_to_quantity_map() children = [ create_product("PackageChild-%d" % x, shop=shop, supplier=supplier) for x in range(4) ] package_def = {child: 1 + i for (i, child) in enumerate(children)} package_product.make_package(package_def) assert package_product.is_package_parent() package_product.save() sp = package_product.get_shop_instance(shop) assert not list( sp.get_orderability_errors( supplier=supplier, quantity=1, customer=AnonymousContact())) with pytest.raises(ValueError): # Test re-packaging fails package_product.make_package(package_def) # Check that OrderCreator can deal with packages source = BasketishOrderSource() source.lines.append( SourceLine( type=OrderLineType.PRODUCT, product=package_product, supplier=get_default_supplier(), quantity=10, unit_price=TaxlessPrice(10), )) source.shop = get_default_shop() source.status = get_initial_order_status() request = apply_request_middleware(RequestFactory().get("/")) creator = OrderCreator(request) order = creator.create_order(source) pids_to_quantities = order.get_product_ids_and_quantities() for child, quantity in six.iteritems(package_def): assert pids_to_quantities[child.pk] == 10 * quantity
def _get_template_engine_and_context(): engine = django.template.engines['jinja2'] assert isinstance(engine, django_jinja.backend.Jinja2) request = RequestFactory().get('/') request.shop = Shop(currency='USD', prices_include_tax=False) request.customer = AnonymousContact() request.person = request.customer context = { 'request': request, 'prod': Product(sku='6.0745'), # TODO: Test also with variant products 'sline': _get_source_line(request), 'bline': _get_basket_line(request), 'oline': _get_order_line(request), } return (engine, context)
def test_product_query(admin_user, regular_user): anon_contact = AnonymousContact() shop_product = get_default_shop_product() shop = shop_product.shop product = shop_product.product regular_contact = get_person_contact(regular_user) admin_contact = get_person_contact(admin_user) with modify(shop_product, save=True, listed=True, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_ALL): assert Product.objects.list_visible( shop=shop, customer=anon_contact).filter(pk=product.pk).exists() with modify(shop_product, save=True, listed=False, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_ALL): assert not Product.objects.list_visible( shop=shop, customer=anon_contact).filter(pk=product.pk).exists() assert not Product.objects.list_visible( shop=shop, customer=regular_contact).filter(pk=product.pk).exists() assert Product.objects.list_visible( shop=shop, customer=admin_contact).filter(pk=product.pk).exists() with modify(shop_product, save=True, listed=True, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN): assert not Product.objects.list_visible( shop=shop, customer=anon_contact).filter(pk=product.pk).exists() assert Product.objects.list_visible( shop=shop, customer=regular_contact).filter(pk=product.pk).exists() product.soft_delete() assert not Product.objects.all_except_deleted().filter( pk=product.pk).exists()
def test_pricing_module_is_active(): """ Make sure that our custom pricing module is active. """ shop = Shop(currency='USD', prices_include_tax=False) customer = AnonymousContact() product = Product(sku='6.0745') pricing_mod = get_pricing_module() pricing_ctx = pricing_mod.get_context_from_data(shop, customer) pi = product.get_price_info(pricing_ctx, quantity=2) price = shop.create_price assert pi.price == price('12.149') assert pi.base_price == price('48.596') assert pi.quantity == 2 assert pi.discounted_unit_price == price('6.0745') assert pi.base_unit_price == price('24.298') assert pi.discount_rate == Decimal('0.75')
def save(self, basket, data): request = basket.request stored_basket = self._get_stored_basket(basket) stored_basket.data = data # TODO: (TAX) the `basket.*_price` getters probably raise some sort of exception # if a given price can't be calculated? stored_basket.taxless_total = basket.taxless_total_price stored_basket.taxful_total = basket.taxful_total_price stored_basket.product_count = basket.product_count user = getattr(request, "user", AnonymousUser()) customer = getattr(request, "customer", AnonymousContact()) if not user.is_anonymous: stored_basket.owner_user = user if not customer.is_anonymous: stored_basket.owner_contact = customer stored_basket.save() product_ids = set(basket.get_product_ids_and_quantities().keys()) stored_basket.products = product_ids basket_get_kwargs = {"pk": stored_basket.pk, "key": stored_basket.key} request.session[self._get_session_key(basket)] = basket_get_kwargs
def test_product_orderability(): anon_contact = AnonymousContact() shop_product = get_default_shop_product() supplier = get_default_supplier() with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, orderable=True): shop_product.raise_if_not_orderable(supplier=supplier, customer=anon_contact, quantity=1) assert shop_product.is_orderable(supplier=supplier, customer=anon_contact, quantity=1) with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN, orderable=True): with pytest.raises(ProductNotOrderableProblem): shop_product.raise_if_not_orderable(supplier=supplier, customer=anon_contact, quantity=1) assert not shop_product.is_orderable( supplier=supplier, customer=anon_contact, quantity=1)
def test_anonymous_contact_vs_person(regular_user): anon = AnonymousContact() person = get_person_contact(regular_user) assert anon != person assert person != anon
def test_anonymity(admin_user, regular_user): assert not get_person_contact(admin_user).is_anonymous assert not get_person_contact(regular_user).is_anonymous assert get_person_contact(None).is_anonymous assert get_person_contact(AnonymousUser()).is_anonymous assert AnonymousContact().is_anonymous
def test_omniscience(admin_user, regular_user): assert get_person_contact(admin_user).is_all_seeing assert not get_person_contact(regular_user).is_all_seeing assert not get_person_contact(None).is_all_seeing assert not get_person_contact(AnonymousUser()).is_all_seeing assert not AnonymousContact().is_all_seeing
def get_request(): request = RequestFactory().get('/') request.shop = Shop(currency='USD', prices_include_tax=False) request.customer = AnonymousContact() request.person = request.customer
def matches(self, context): customer = (context.customer if context.customer is not None else AnonymousContact()) customers_groups = customer.groups.all() return self.contact_groups.filter(pk__in=customers_groups).exists()
def customer(self): return (self._customer or AnonymousContact())
def _get_pricing_context(shop, customer=None): return get_pricing_module().get_context_from_data( shop=shop, customer=(customer or AnonymousContact()), )
def get_price_info(shop, customer, product, quantity): ctx_request = RequestFactory().get("/") ctx_request.shop = shop ctx_request.customer = (customer or AnonymousContact()) context = get_pricing_module().get_context_from_request(ctx_request) return product.get_price_info(context, quantity=quantity)
def orderer(self): return (self._orderer or AnonymousContact())
def test_category_visibility(admin_user, regular_user): visible_public_category = Category.objects.create( status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="visible_public", name=DEFAULT_NAME) hidden_public_category = Category.objects.create( status=CategoryStatus.INVISIBLE, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="hidden_public", name=DEFAULT_NAME) deleted_public_category = Category.objects.create( status=CategoryStatus.DELETED, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="deleted_public", name=DEFAULT_NAME) logged_in_category = Category.objects.create( status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_LOGGED_IN, identifier="visible_logged_in", name=DEFAULT_NAME) group_visible_category = Category.objects.create( status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_GROUPS, identifier="visible_groups", name=DEFAULT_NAME) assert visible_public_category.name == DEFAULT_NAME assert str(visible_public_category) == DEFAULT_NAME anon_contact = AnonymousContact() regular_contact = get_person_contact(regular_user) admin_contact = get_person_contact(admin_user) for (customer, category, expect) in [ (anon_contact, visible_public_category, True), (anon_contact, hidden_public_category, False), (anon_contact, deleted_public_category, False), (anon_contact, logged_in_category, False), (anon_contact, group_visible_category, False), (regular_contact, visible_public_category, True), (regular_contact, hidden_public_category, False), (regular_contact, deleted_public_category, False), (regular_contact, logged_in_category, True), (regular_contact, group_visible_category, False), (admin_contact, visible_public_category, True), (admin_contact, hidden_public_category, True), (admin_contact, deleted_public_category, False), (admin_contact, logged_in_category, True), (admin_contact, group_visible_category, True), ]: result = Category.objects.all_visible(customer=customer).filter( pk=category.pk).exists() assert result == expect, "Queryset visibility of %s for %s as expected" % ( category.identifier, customer) assert category.is_visible( customer ) == expect, "Direct visibility of %s for %s as expected" % ( category.identifier, customer) assert not Category.objects.all_except_deleted().filter( pk=deleted_public_category.pk).exists( ), "Deleted category does not show up in 'all_except_deleted'"