def test_comment_visibility(admin_user): shop = factories.get_default_shop() admin_contact = get_person_contact(admin_user) staff_user = factories.create_random_user("en", is_staff=True) staff_contact = get_person_contact(staff_user) shop.staff_members.add(staff_user) normal_user = factories.create_random_user("en") normal_contact = get_person_contact(normal_user) task_type = TaskType.objects.create(name="Request", shop=shop) task = create_task(shop, admin_contact, task_type, "my task") task.comment(admin_contact, "This is only visibile for super users", TaskCommentVisibility.ADMINS_ONLY) task.comment(staff_contact, "This is only visibile for staff only", TaskCommentVisibility.STAFF_ONLY) task.comment(normal_contact, "This is visibile for everyone", TaskCommentVisibility.PUBLIC) # admin see all comments assert task.comments.for_contact(admin_contact).count() == 3 # staff see all public + staff only assert task.comments.for_contact(staff_contact).count() == 2 # normal contact see all public assert task.comments.for_contact(normal_contact).count() == 1 # anonymous contact see all public assert task.comments.for_contact(AnonymousContact()).count() == 1
def test_save_marketing_check(rf, admin_user): admin_contact = get_person_contact(admin_user) shop = get_default_shop() with override_settings(SHUUP_CHECKOUT_CONFIRM_FORM_PROPERTIES={}): request = apply_request_middleware(rf.get("/"), shop=shop, user=admin_user, customer=admin_contact) form = ConfirmForm(request=request) assert form.fields["marketing"].initial is False assert form.fields["marketing"].widget.__class__ == forms.CheckboxInput admin_contact.options = {} admin_contact.options["marketing_permission_asked"] = True admin_contact.save() form = ConfirmForm(request=request) assert form.fields["marketing"].initial is False assert form.fields["marketing"].widget.__class__ == forms.HiddenInput admin_contact.marketing_permission = True admin_contact.save() form = ConfirmForm(request=request) assert form.fields["marketing"].initial is True assert form.fields["marketing"].widget.__class__ == forms.HiddenInput # test with anonymous request = apply_request_middleware(rf.get("/"), shop=shop, user=admin_user, customer=AnonymousContact()) form = ConfirmForm(request=request) assert form.fields["marketing"].initial is False assert form.fields["marketing"].widget.__class__ == forms.CheckboxInput
def _get_template_engine_and_context(product_sku='6.0745'): engine = django.template.engines['jinja2'] assert isinstance(engine, django_jinja.backend.Jinja2) shop = get_default_shop() shop.currency = 'USD' shop.prices_include_tax = False shop.save() request = RequestFactory().get('/') request.shop = shop request.customer = AnonymousContact() request.person = request.customer PriceDisplayOptions(include_taxes=False).set_for_request(request) tax = get_default_tax() create_default_tax_rule(tax) tax_class = get_default_tax_class() order, order_line = _get_order_and_order_line(request) product = create_product(sku=product_sku, shop=shop, tax_class=tax_class) context = { 'request': request, 'prod': product, # TODO: Test also with variant products 'sline': _get_source_line(request), 'bline': _get_basket_line(request), 'oline': order_line, 'order': order } return (engine, context)
def test_filter_parameter_contact_groups(): customer_price = 10.3 anonymous_price = 14.6 def get_price_info_mock(context, product, quantity=1): if context.customer.get_default_group() == AnonymousContact().get_default_group(): price = context.shop.create_price(anonymous_price) else: price = context.shop.create_price(customer_price) return PriceInfo(quantity * price, quantity * price, quantity) with patch.object(DummyPricingModule, 'get_price_info', side_effect=get_price_info_mock): (engine, context) = _get_template_engine_and_context(product_sku="123") # test with anonymous context['request'].customer = AnonymousContact() context['request'].person = context['request'].customer result = engine.from_string("{{ prod|price(quantity=2) }}") assert result.render(context) == "$%0.2f" % (anonymous_price * 2) # Get fresh content. I guess the prices shouldn't change between request. (engine, context) = _get_template_engine_and_context(product_sku="1234") # test with customer context['request'].customer = create_random_person() context['request'].person = context['request'].customer result = engine.from_string("{{ prod|price(quantity=2) }}") assert result.render(context) == "$%0.2f" % (customer_price * 2)
def get_price_info_mock(context, product, quantity=1): if context.customer.get_default_group() == AnonymousContact( ).get_default_group(): price = context.shop.create_price(anonymous_price) else: price = context.shop.create_price(customer_price) return PriceInfo(quantity * price, quantity * price, quantity)
def test_supplier_price_without_selected_supplier(rf): shop = factories.get_shop() supplier1 = Supplier.objects.create(name="Test 1") supplier1.shops.add(shop) supplier2 = Supplier.objects.create(name="Test 2") supplier2.shops.add(shop) strategy = "shuup.testing.supplier_pricing.supplier_strategy:CheapestSupplierPriceSupplierStrategy" with override_settings(SHUUP_PRICING_MODULE="supplier_pricing", SHUUP_SHOP_PRODUCT_SUPPLIERS_STRATEGY=strategy): customer = AnonymousContact() pricing_mod = get_pricing_module() supplier1_ctx = pricing_mod.get_context_from_data(shop, customer, supplier=supplier1) supplier2_ctx = pricing_mod.get_context_from_data(shop, customer, supplier=supplier2) # Supplied by both suppliers product1_default_price = 10 product1 = factories.create_product( "sku1", shop=shop, supplier=supplier1, default_price=product1_default_price) shop_product1 = product1.get_shop_instance(shop) shop_product1.suppliers.add(supplier2) # Both suppliers should get price from shop # product default price assert product1.get_price( supplier1_ctx).amount.value == product1_default_price assert product1.get_price( supplier2_ctx).amount.value == product1_default_price # Now let's add per supplier prices supplier1_price = 7 supplier2_price = 8 SupplierPrice.objects.create(shop=shop, supplier=supplier1, product=product1, amount_value=supplier1_price) SupplierPrice.objects.create(shop=shop, supplier=supplier2, product=product1, amount_value=supplier2_price) assert product1.get_price( supplier1_ctx).amount.value == supplier1_price assert product1.get_price( supplier2_ctx).amount.value == supplier2_price # Now pricing context without defined supplier # should return cheapest price context = pricing_mod.get_context_from_data(shop, customer) assert shop_product1.get_supplier().pk == supplier1.pk assert product1.get_price(context).amount.value == supplier1_price
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_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_set_from_anonymous_to_customer_auth(rf): """ Set some random customer to the basket when authenticated """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = AnonymousContact() # can not set the customer for something different as the request customer with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, factories.create_random_person()) assert exc.value.code == "no_permission" assert basket.customer == AnonymousContact() assert basket_commands.handle_set_customer(request, basket, get_person_contact(user))["ok"] is True assert basket.customer == get_person_contact(user)
def index_related_discount_shop_products( discounts: "Iterable[Discount]"): # noqa: C901 """ Index all shop products that are affected by the given discounts """ from shuup.discounts.modules import ProductDiscountModule indexed_shop_products = set() for discount in discounts: discounts_groups_ids = [AnonymousContact.get_default_group().pk] if discount.contact_group: discounts_groups_ids.append(discount.contact_group_id) shop_products = (ShopProduct.objects.select_related( "shop", "product").prefetch_related("suppliers").filter( shop=discount.shop)) if discount.supplier: shop_products = shop_products.filter(suppliers=discount.supplier) if discount.product: shop_products = shop_products.filter(product=discount.product) if discount.category: if discount.exclude_selected_category: shop_products = shop_products.exclude( categories=discount.category) else: shop_products = shop_products.filter( categories=discount.category) # reindex all products that will be affected by this discount for shop_product in shop_products: if shop_product.pk in indexed_shop_products: continue indexed_shop_products.add(shop_product.pk) ProductCatalogDiscountedPrice.objects.filter( catalog_rule__module_identifier=ProductDiscountModule. identifier, shop=shop_product.shop, product=shop_product.product, ).delete() suppliers = shop_product.suppliers.all() if discount.supplier: suppliers = suppliers.filter(pk=discount.supplier_id) for supplier in suppliers: index_shop_product_price(shop_product, supplier, discounts_groups_ids) index_linked_shop_products(discount, discounts_groups_ids, indexed_shop_products)
def test_anonymous_set_company_customer(rf): """ Set a company as the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = AnonymousContact() person = factories.create_random_person() company = factories.create_random_company() company.members.add(person) with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "not_company_member" assert basket.customer == AnonymousContact()
def test_omniscience(admin_user, regular_user): assert not get_person_contact(admin_user).is_all_seeing configuration.set(None, get_all_seeing_key(admin_user), True) 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 configuration.set(None, get_all_seeing_key(admin_user), False)
def test_discount_for_anons(rf): default_price = 10 request, product = _init_test_for_product(rf, default_price) assert request.customer == AnonymousContact() anon_default_group = AnonymousContact().get_default_group() product_discount_amount = 2 Discount.objects.create(shop=request.shop, active=True, contact_group=anon_default_group, discount_amount_value=product_discount_amount) assert product.get_price_info(request).price == request.shop.create_price( default_price - product_discount_amount) # Setting customer to request takes out the discount request.customer = factories.create_random_person() assert product.get_price_info(request).price == request.shop.create_price( default_price)
def test_dev_onboarding(browser, admin_user, live_server, settings): Shop.objects.first().delete() # Delete first shop created by test initializations call_command("shuup_init", *[], **{}) shop = Shop.objects.first() assert not shop.maintenance_mode initialize_admin_browser_test(browser, live_server, settings, onboarding=True) browser.fill("address-first_name", "Matti") browser.fill("address-last_name", "Teppo") browser.fill("address-phone", "112") browser.fill("address-street", "Teststreet") browser.fill("address-postal_code", "20540") browser.fill("address-city", "Turku") click_element(browser, "#select2-id_address-country-container") wait_until_appeared(browser, "input.select2-search__field") browser.find_by_css("input.select2-search__field").first.value = "Finland" wait_until_appeared(browser, ".select2-results__option:not([aria-live='assertive'])") browser.execute_script('$($(".select2-results__option")[0]).trigger({type: "mouseup"})') click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("To start accepting payments right away")) click_element(browser, "div[data-name='manual_payment'] button[name='activate']") browser.fill("manual_payment-service_name", "Laskulle") click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("To start shipping products right away")) click_element(browser, "div[data-name='manual_shipping'] button[name='activate']") browser.fill("manual_shipping-service_name", "Kotiinkuljetus") click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("theme for your shop")) click_element(browser, "div[data-identifier='candy_pink'] button[data-theme='shuup.themes.classic_gray']") click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("initial content and configure")) click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("install some sample data")) browser.execute_script('document.getElementsByName("sample-categories")[0].checked=true') browser.execute_script('document.getElementsByName("sample-products")[0].checked=true') click_element(browser, "button[name='next']") wait_until_condition(browser, lambda x: x.is_text_present("Welcome to Shuup!")) click_element(browser, "input[value='Publish shop']") shop.refresh_from_db() assert not shop.maintenance_mode assert Product.objects.count() == 10 supplier = Supplier.objects.first() customer = AnonymousContact() assert len([ product for product in Product.objects.all() if product.get_shop_instance(shop).is_orderable(supplier, customer, 1) ]) == 10
def test_get_best_selling_products(): from shuup.front.template_helpers import general context = get_jinja_context() # No products sold assert len(list(general.get_best_selling_products(context, n_products=3))) == 0 shop = get_default_shop() supplier = get_default_supplier() supplier2 = Supplier.objects.create(name="supplier2", enabled=True, is_approved=True) supplier3 = Supplier.objects.create(name="supplier3", enabled=True, is_approved=True) supplier2.shops.add(shop) supplier3.shops.add(shop) product1 = create_product("product1", shop, supplier, 10) product2 = create_product("product2", shop, supplier, 20) create_order_with_product(product1, supplier, quantity=1, taxless_base_unit_price=10, shop=shop) create_order_with_product(product2, supplier, quantity=2, taxless_base_unit_price=20, shop=shop) cache.clear() # Two products sold for cache_test in range(2): best_selling_products = list(general.get_best_selling_products(context, n_products=3)) assert len(best_selling_products) == 2 assert product1 in best_selling_products assert product2 in best_selling_products # Make order unorderable shop_product = product1.get_shop_instance(shop) shop_product.visibility = ShopProductVisibility.NOT_VISIBLE shop_product.save() cache.clear() for cache_test in range(2): best_selling_products = list(general.get_best_selling_products(context, n_products=3)) assert len(best_selling_products) == 1 assert product1 not in best_selling_products assert product2 in best_selling_products # add a new product with discounted amount product3 = create_product("product3", supplier=supplier, shop=shop, default_price=30) create_order_with_product(product3, supplier, quantity=1, taxless_base_unit_price=30, shop=shop) from shuup.customer_group_pricing.models import CgpDiscount CgpDiscount.objects.create( shop=shop, product=product3, group=AnonymousContact.get_default_group(), discount_amount_value=5 ) cache.clear() for cache_test in range(2): best_selling_products = list(general.get_best_selling_products(context, n_products=3, orderable_only=True)) assert len(best_selling_products) == 2 assert product1 not in best_selling_products assert product2 in best_selling_products assert product3 in best_selling_products
def _assert_price(product, shop, expected_price, expected_base_price, customer=None): context = PricingContext(shop=shop, customer=customer or AnonymousContact()) price = product.get_price_info(context) assert price.price.value == expected_price assert price.base_price.value == expected_base_price
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 test_discount_for_anonymous(rf, admin_user, price, discount): request, shop, group = initialize_test(rf, True, AnonymousContact()) product = create_product("product", shop=shop, default_price=price) CgpDiscount.objects.create(product=product, group=group, shop=shop, discount_amount_value=discount) price_info = product.get_price_info(request) assert price_info.price == shop.create_price(max(price - discount, 0))
def test_anonymous_stored_basket_detail_view(rf, regular_user, admin_user): shop = factories.get_default_shop() cart = _create_cart_with_products( rf, shop, AnonymousUser(), AnonymousContact(), AnonymousContact(), 2, False ) assert cart assert cart.product_count == 2 stored_basket = StoredBasket.objects.first() assert stored_basket and stored_basket.class_spec view = CartDetailView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop) set_shop(request, shop) response = view(request, pk=stored_basket.pk) if hasattr(response, "render"): response.render() assert response.status_code == 200
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_discount_for_multi_group_using_customer(rf, admin_user, price, discount, anonymous_discount): customer = create_customer() anonymous = AnonymousContact() request, shop, _ = initialize_test(rf, True, customer) product = create_product("product", shop=shop, default_price=price) CgpDiscount.objects.create(product=product, group=customer.groups.first(), shop=shop, discount_amount_value=discount) CgpDiscount.objects.create(product=product, group=anonymous.get_default_group(), shop=shop, discount_amount_value=anonymous_discount) # discount for customer request, shop, _ = initialize_test(rf, True, customer) price_info = product.get_price_info(request) assert price_info.price == shop.create_price(max(price-discount, 0)) # discount for anonymous request, shop, _ = initialize_test(rf, True, anonymous) price_info = product.get_price_info(request) assert price_info.price == shop.create_price(max(price-anonymous_discount, 0))
def test_best_selling_products_with_multiple_orders(): from shuup.front.template_helpers import general context = get_jinja_context() supplier = get_default_supplier() shop = get_default_shop() n_products = 2 price = 10 product_1 = create_product("test-sku-1", supplier=supplier, shop=shop, default_price=price) product_2 = create_product("test-sku-2", supplier=supplier, shop=shop, default_price=price) create_order_with_product(product_1, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) # Two initial products sold for cache_test in range(2): assert product_1 in general.get_best_selling_products(context, n_products=n_products) assert product_2 in general.get_best_selling_products(context, n_products=n_products) product_3 = create_product("test-sku-3", supplier=supplier, shop=shop, default_price=price) create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop) # Third product sold in greater quantity cache.clear() assert product_3 in general.get_best_selling_products(context, n_products=n_products) create_order_with_product(product_1, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product outsold by first two products for cache_test in range(2): assert product_3 not in general.get_best_selling_products(context, n_products=n_products) children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(5)] for child in children: child.link_to_parent(product_3) create_order_with_product(child, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product now sold in greatest quantity for cache_test in range(2): assert product_3 == general.get_best_selling_products(context, n_products=n_products)[0] # add a new product with discounted amount product_4 = create_product("test-sku-4", supplier=supplier, shop=shop, default_price=price) create_order_with_product(product_4, supplier, quantity=2, taxless_base_unit_price=price, shop=shop) from shuup.customer_group_pricing.models import CgpDiscount CgpDiscount.objects.create( shop=shop, product=product_4, group=AnonymousContact.get_default_group(), discount_amount_value=(price * 0.1) )
def test_set_from_anonymous_to_customer_not_auth(rf): """ Set some customer to the basket when not authenticated """ with override_settings(**CORE_BASKET_SETTINGS): request = apply_request_middleware(rf.get("/")) basket = get_basket(request, "basket") basket.customer = AnonymousContact() customer = factories.create_random_person() assert basket_commands.handle_set_customer(request, basket, customer)["ok"] is True assert basket.customer == customer
def _get_template_engine_and_context(product_sku="6.0745", create_var_product=False): engine = django.template.engines["jinja2"] assert isinstance(engine, django_jinja.backend.Jinja2) shop = get_default_shop() shop.currency = "USD" shop.prices_include_tax = False shop.save() request = RequestFactory().get("/") request.shop = shop request.customer = AnonymousContact() request.person = request.customer PriceDisplayOptions(include_taxes=False).set_for_request(request) tax = get_default_tax() create_default_tax_rule(tax) tax_class = get_default_tax_class() order, order_line = _get_order_and_order_line(request) product = create_product(sku=product_sku, shop=shop, tax_class=tax_class) supplier = get_default_supplier(shop) if create_var_product: var_product = create_product(sku="32.9", shop=shop, tax_class=tax_class) child_product_1 = create_product(sku="4.50", shop=shop, tax_class=tax_class, supplier=supplier, default_price="4.5") child_product_2 = create_product(sku="12.00", shop=shop, tax_class=tax_class, supplier=supplier, default_price="12") child_product_1.link_to_parent(var_product, variables={"color": "red"}) child_product_2.link_to_parent(var_product, variables={"color": "blue"}) context = { "request": request, "prod": product, "var_prod": var_product if create_var_product else None, # TODO: Test also with variant products "sline": _get_source_line(request), "bline": _get_basket_line(request), "oline": order_line, "order": order, } return (engine, context)
def _set_person(self, request): if should_force_anonymous_contact(request.user): request.person = AnonymousContact() else: request.person = get_person_contact(request.user) if not request.person.is_active: messages.add_message(request, messages.INFO, _("Logged out since this account is inactive.")) logout(request) # Usually logout is connected to the `refresh_on_logout` # method via a signal and that already sets request.person # to anonymous. But set it explicitly too, just to be sure. request.person = get_person_contact(None)
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_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 get_contact_filter(contact: Optional[Contact]): if contact: # filter all prices for the contact OR to the groups of the contact return Q( Q(contact=contact) # evaluate contact group to prevent doing expensive joins on db | Q(contact_group_id__in=list( contact.groups.values_list("pk", flat=True))) | Q(contact_group__isnull=True, contact__isnull=True)) # anonymous contact return Q( Q(contact_group__isnull=True, contact__isnull=True) | Q(contact_group_id=AnonymousContact.get_default_group().pk))
def test_purchasability(): anon_contact = AnonymousContact() shop_product = get_default_shop_product() supplier = get_default_supplier(shop_product.shop) assert shop_product.purchasable 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, purchasable=False): 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_product_query(visibility, show_in_list, show_in_search, admin_user, regular_user): shop = get_default_shop() product = create_product("test-sku", shop=shop) shop_product = product.get_shop_instance(shop) anon_contact = AnonymousContact() regular_contact = get_person_contact(regular_user) admin_contact = get_person_contact(admin_user) shop_product.visibility = visibility shop_product.save() assert shop_product.visibility_limit == ProductVisibility.VISIBLE_TO_ALL # Anonymous contact should be the same as no contact assert (product in Product.objects.listed(shop=shop)) == show_in_list assert (product in Product.objects.searchable(shop=shop)) == show_in_search assert (product in Product.objects.listed(shop=shop, customer=anon_contact)) == show_in_list assert (product in Product.objects.searchable( shop=shop, customer=anon_contact)) == show_in_search # Admin should see all non-deleted results configuration.set(None, get_all_seeing_key(admin_contact), True) assert product in Product.objects.listed(shop=shop, customer=admin_contact) assert product in Product.objects.searchable(shop=shop, customer=admin_contact) # Anonymous contact shouldn't see products with logged in visibility limit shop_product.visibility_limit = ProductVisibility.VISIBLE_TO_LOGGED_IN shop_product.save() assert product not in Product.objects.listed(shop=shop, customer=anon_contact) assert product not in Product.objects.searchable(shop=shop, customer=anon_contact) # Reset visibility limit shop_product.visibility_limit = ProductVisibility.VISIBLE_TO_ALL shop_product.save() # No one should see deleted products product.soft_delete() assert product not in Product.objects.listed(shop=shop) assert product not in Product.objects.searchable(shop=shop) assert product not in Product.objects.listed(shop=shop, customer=admin_contact) assert product not in Product.objects.searchable(shop=shop, customer=admin_contact) configuration.set(None, get_all_seeing_key(admin_contact), False)
def test_sample_data_wizard_pane(rf, admin_user, settings): settings.SHUUP_SETUP_WIZARD_PANE_SPEC = [ "shuup.testing.modules.sample_data.views.SampleObjectsWizardPane" ] shop = get_default_shop() get_default_tax_class() data = { 'pane_id': 'sample', 'sample-business_segment': 'default', 'sample-categories': True, 'sample-products': True, 'sample-carousel': True } request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = WizardView.as_view()(request) assert response.status_code == 200 assert Product.objects.count() == len( BUSINESS_SEGMENTS["default"]["products"]) anon_contact = AnonymousContact() supplier = get_default_supplier() # check for the injected plugin using the carousel assert Carousel.objects.count() == 1 carousel = Carousel.objects.first() assert Slide.objects.count() == len( BUSINESS_SEGMENTS["default"]["carousel"]["slides"]) svc = SavedViewConfig.objects.first() assert svc.view_name == "IndexView" layout = svc.get_layout_data("front_content") assert layout['rows'][0]['cells'][0]['config']['carousel'] == carousel.pk assert layout['rows'][0]['cells'][0]['plugin'] == CarouselPlugin.identifier for product in Product.objects.all(): # all products must be orderable and have images assert product.get_shop_instance(shop).is_orderable( supplier=supplier, customer=anon_contact, quantity=1) assert product.primary_image is not None assert Category.objects.count() == len( BUSINESS_SEGMENTS["default"]["categories"]) request = apply_request_middleware(rf.get("/"), user=admin_user) response = WizardView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard")
def test_default_anonymous_contact_group_repr_and_str(): adg = AnonymousContact.get_default_group() assert repr(adg) == '<ContactGroup:%d-default_anonymous_group>' % adg.pk assert str(adg) == 'Anonymous Contacts'