def test_category_filter(rf): request, shop, group = initialize_test(rf, False) cat = get_default_category() cat_filter = CategoryFilter.objects.create() cat_filter.categories.add(cat) cat_filter.save() assert cat_filter.values.first() == cat category = Category.objects.create( parent=None, identifier="testcat", name="catcat", ) cat_filter.values = [cat, category] cat_filter.save() assert cat_filter.values.count() == 2 product = create_product("Just-A-Product-Too", shop, default_price="200") shop_product = product.get_shop_instance(shop) shop_product.categories.add(cat) shop_product.save() assert cat_filter.filter_queryset(ShopProduct.objects.all()).exists() # filter matches
def test_category_links_plugin_with_customer(rf, show_all_categories): """ Test plugin for categories that is visible for certain group """ shop = get_default_shop() group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) request.customer = customer category = get_default_category() category.status = CategoryStatus.VISIBLE category.visibility = CategoryVisibility.VISIBLE_TO_GROUPS category.visibility_groups.add(group) category.shops.add(shop) category.save() vars = {"request": request} context = get_jinja_context(**vars) plugin = CategoryLinksPlugin({"categories": [category.pk], "show_all_categories": show_all_categories}) assert category.is_visible(customer) assert category in plugin.get_context_data(context)["categories"] customer_without_groups = create_random_person() customer_without_groups.groups.clear() assert not category.is_visible(customer_without_groups) request.customer = customer_without_groups context = get_jinja_context(**vars) assert category not in plugin.get_context_data(context)["categories"]
def test_category_filter(rf): request, shop, group = initialize_test(rf, False) cat = get_default_category() cat_filter = CategoryFilter.objects.create() cat_filter.categories.add(cat) cat_filter.save() assert cat_filter.values.first() == cat category = Category.objects.create( parent=None, identifier="testcat", name="catcat", ) cat_filter.values = [cat, category] cat_filter.save() assert cat_filter.values.count() == 2 product = create_product("Just-A-Product-Too", shop, default_price="200") shop_product = product.get_shop_instance(shop) shop_product.categories.add(cat) shop_product.save() assert cat_filter.filter_queryset( ShopProduct.objects.all()).exists() # filter matches
def test_category_removal(): product = get_product() category = get_default_category() product.category = category product.save() with pytest.raises(ProtectedError): category.delete() assert Product.objects.filter(pk=product.pk).exists()
def test_shopproduct_primary_category_removal(): product = get_product() category = get_default_category() sp = product.get_shop_instance(get_default_shop()) sp.primary_category = category sp.save() with pytest.raises(ProtectedError): category.delete() assert ShopProduct.objects.filter(pk=sp.pk).exists()
def test_category_links_plugin_show_all(rf): """ Test that show_all_categories forces plugin to return all visible categories """ category = get_default_category() category.status = CategoryStatus.VISIBLE category.shops.add(get_default_shop()) category.save() context = get_context(rf) plugin = CategoryLinksPlugin({"show_all_categories": False}) assert context["request"].customer.is_anonymous assert context["request"].shop in category.shops.all() assert not plugin.get_context_data(context)["categories"] plugin = CategoryLinksPlugin({"show_all_categories": True}) assert category in plugin.get_context_data(context)["categories"]
def test_slide_links(): test_carousel = Carousel.objects.create(name="test") test_image_1 = Image.objects.create(original_filename="slide1.jpg") with translation.override("en"): test_slide = Slide.objects.create(carousel=test_carousel, name="test", image=test_image_1) # Test external link assert len(test_carousel.slides.all()) == 1 test_link = "http://example.com" test_slide.external_link = test_link test_slide.save() assert test_slide.get_translated_field("external_link") == test_link assert test_slide.get_link_url() == test_link # Test Product url and link priorities test_product = get_default_product() test_slide.product_link = test_product test_slide.save() assert test_slide.get_link_url() == test_link test_slide.external_link = None test_slide.save() assert test_slide.get_link_url().startswith("/p/") # Close enough... # Test Category url and link priorities test_category = get_default_category() test_slide.category_link = test_category test_slide.save() assert test_slide.get_link_url().startswith("/p/") # Close enough... test_slide.product_link = None test_slide.save() assert test_slide.get_link_url().startswith("/c/") # Close enough... # Test CMS page url and link priorities attrs = {"url": "test"} test_page = create_page(**attrs) test_slide.cms_page_link = test_page test_slide.save() assert test_slide.get_link_url().startswith("/c/") # Close enough... test_slide.category_link = None test_slide.save() assert test_slide.get_link_url().startswith("/test/") # Check that external link overrides everything test_slide.external_link = test_link test_slide.save() assert test_slide.get_link_url() == test_link
def test_category_links_plugin(rf): """ Test that the plugin only displays visible categories with shop (since we can't have a request without shop or customer) """ category = get_default_category() context = get_context(rf) plugin = CategoryLinksPlugin({"show_all_categories": True}) assert context["request"].customer.is_anonymous assert category not in plugin.get_context_data(context)["categories"] category.status = CategoryStatus.VISIBLE category.shops.add(get_default_shop()) category.save() assert context["request"].customer.is_anonymous assert context["request"].shop in category.shops.all() assert category in plugin.get_context_data(context)["categories"]