예제 #1
0
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"]
예제 #2
0
def test_get_pagination_variables():
    populate_if_required()  # Makes sure there is at least 30 products in db

    products = Product.objects.all()[:19]
    assert len(products) == 19
    vars = {"products": products}

    context = get_jinja_context(**vars)
    variables = general.get_pagination_variables(context,
                                                 context["products"],
                                                 limit=2)
    assert variables["page"].number == 1
    assert len(variables["objects"]) == 2
    assert variables["page_range"][0] == 1
    assert variables["page_range"][-1] == 5

    context = get_jinja_context(path="/?page=5", **vars)
    variables = general.get_pagination_variables(context,
                                                 context["products"],
                                                 limit=2)
    assert variables["page"].number == 5
    assert len(variables["objects"]) == 2
    assert variables["page_range"][0] == 3
    assert variables["page_range"][-1] == 7

    variables = general.get_pagination_variables(context,
                                                 context["products"],
                                                 limit=20)
    assert not variables["is_paginated"]
    assert variables["page"].number == 1
    assert variables["page_range"][0] == variables["page_range"][-1] == 1

    context = get_jinja_context(path="/?page=42", **vars)
    variables = general.get_pagination_variables(context,
                                                 context["products"],
                                                 limit=4)
    assert variables["page"].number == 5
    assert len(variables["objects"]) == 3
    assert variables["page_range"][0] == 1
    assert variables["page_range"][-1] == 5

    vars = {"products": []}
    context = get_jinja_context(path="/", **vars)
    variables = general.get_pagination_variables(context,
                                                 context["products"],
                                                 limit=4)
    assert not variables["is_paginated"]
    assert variables["page_range"][0] == variables["page_range"][-1] == 1
예제 #3
0
def test_get_listed_products_filter():
    context = get_jinja_context()
    shop = get_default_shop()
    supplier = get_default_supplier()

    product_1 = create_product(
        "test-sku-1",
        supplier=supplier,
        shop=shop,
    )
    product_2 = create_product(
        "test-sku-2",
        supplier=supplier,
        shop=shop,
    )
    filter_dict = {"id": product_1.id}
    product_list = general.get_listed_products(context,
                                               n_products=2,
                                               filter_dict=filter_dict)
    assert product_1 in product_list
    assert product_2 not in product_list

    product_list = general.get_listed_products(context,
                                               n_products=2,
                                               filter_dict=filter_dict,
                                               orderable_only=False)
    assert product_1 in product_list
    assert product_2 not in product_list
예제 #4
0
def test_get_best_selling_products():
    context = get_jinja_context()
    cache.clear()
    # No products sold
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=2))) == 0

    supplier = get_default_supplier()
    shop = get_default_shop()
    product = get_default_product()
    create_order_with_product(product,
                              supplier,
                              quantity=1,
                              taxless_base_unit_price=10,
                              shop=shop)
    cache.clear()
    # One product sold
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=2))) == 1

    # Make order unorderable
    shop_product = product.get_shop_instance(shop)
    shop_product.visibility = ShopProductVisibility.NOT_VISIBLE
    shop_product.save()
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=2))) == 0
예제 #5
0
def test_cross_sell_plugin_renders():
    """
    Test that the plugin renders a product
    """
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("test-sku",
                             shop=shop,
                             supplier=supplier,
                             stock_behavior=StockBehavior.UNSTOCKED)
    computed = create_product("test-computed-sku",
                              shop=shop,
                              supplier=supplier,
                              stock_behavior=StockBehavior.UNSTOCKED)
    type = ProductCrossSellType.COMPUTED

    ProductCrossSell.objects.create(product1=product,
                                    product2=computed,
                                    type=type)
    assert ProductCrossSell.objects.filter(product1=product,
                                           type=type).count() == 1

    context = get_jinja_context(product=product)
    rendered = ProductCrossSellsPlugin({"type": type}).render(context)
    assert computed.sku in rendered
예제 #6
0
def test_social_media_plugin_ordering():
    """
    Test that social media plugin ordering works as expected
    """
    context = get_jinja_context()
    icon_classes = SocialMediaLinksPlugin.icon_classes
    link_1_type = "Facebook"
    link_1 = {
        "url": "http://www.facebook.com",
        "ordering": 2,
    }

    link_2_type = "Twitter"
    link_2 = {
        "url": "http://www.twitter.com",
        "ordering": 1,
    }

    links = {
        link_1_type: link_1,
        link_2_type: link_2,
    }
    plugin = SocialMediaLinksPlugin({"links": links})
    assert len(plugin.get_links()) == 2

    # Make sure link 2 comes first
    assert plugin.get_links()[0][2] == link_2["url"]
예제 #7
0
def get_context(rf, customer=None):
    request = rf.get("/")
    request.shop = get_default_shop()
    apply_request_middleware(request)
    if customer:
        request.customer = customer
    vars = {"request": request}
    return get_jinja_context(**vars)
예제 #8
0
def test_plugin_renders_absolute_links():
    """
    Test that the plugin renders only absolute links.
    """
    context = get_jinja_context()
    page = create_page(eternal=True, visible_in_menu=True, shop=get_default_shop())
    absolute_link = "/%s" % page.url
    plugin = PageLinksPlugin({"show_all_pages": True})
    assert absolute_link in plugin.render(context)
예제 #9
0
def test_carousel_plugin_form_get_context():
    shop = get_default_shop()
    context = get_jinja_context()
    test_carousel = Carousel.objects.create(name="test")
    plugin = CarouselPlugin(config={"carousel": test_carousel.pk})
    assert plugin.get_context_data(context).get("carousel") == None

    test_carousel.shops = [shop]
    plugin = CarouselPlugin(config={"carousel": test_carousel.pk})
    assert plugin.get_context_data(context).get("carousel") == test_carousel
예제 #10
0
def test_page_links_plugin_show_all():
    """
    Test that show_all_pages forces plugin to return all visible pages
    """
    context = get_jinja_context()
    page = create_page(eternal=True, visible_in_menu=True, shop=get_default_shop())
    plugin = PageLinksPlugin({"show_all_pages": False})
    assert not plugin.get_context_data(context)["pages"]

    plugin = PageLinksPlugin({"show_all_pages": True})
    assert page in plugin.get_context_data(context)["pages"]
예제 #11
0
def test_banner_box_plugin():
    shop = get_default_shop()
    context = get_jinja_context()
    test_carousel = Carousel.objects.create(name="test")
    test_carousel.shops = [shop]
    plugin = BannerBoxPlugin(config={
        "carousel": test_carousel.pk,
        "title": "Test"
    })
    data = plugin.get_context_data(context)
    assert data.get("carousel") == test_carousel
    assert data.get("title") == "Test"
예제 #12
0
def test_get_random_products():
    supplier = get_default_supplier()
    shop = get_default_shop()
    products = [
        create_product("sku-%d" % x, supplier=supplier, shop=shop)
        for x in range(2)
    ]
    children = [
        create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop)
        for x in range(2)
    ]
    for child in children:
        child.link_to_parent(products[0])
    context = get_jinja_context()
    # only 2 parent products exist
    assert len(list(general.get_random_products(context, n_products=10))) == 2
예제 #13
0
def test_page_links_plugin_hide_expired(show_all_pages):
    """
    Make sure plugin correctly filters out expired pages based on plugin
    configuration
    """
    context = get_jinja_context()
    page = create_page(eternal=True, visible_in_menu=True, shop=get_default_shop())
    another_page = create_page(eternal=True, visible_in_menu=True, shop=get_default_shop())
    plugin = PageLinksPlugin({"pages": [page.pk, another_page.pk], "show_all_pages": show_all_pages})
    assert page in plugin.get_context_data(context)["pages"]

    page.available_from = None
    page.available_to = None
    page.save()
    assert page in plugin.get_context_data(context)["pages"]

    plugin.config["hide_expired"] = True
    pages_in_context = plugin.get_context_data(context)["pages"]
    assert page not in pages_in_context
    assert another_page in pages_in_context
예제 #14
0
def test_get_listed_products_orderable_only():
    context = get_jinja_context()
    shop = get_default_shop()
    simple_supplier = get_simple_supplier()
    n_products = 2

    # Create product without stock
    product = create_product("test-sku",
                             supplier=simple_supplier,
                             shop=shop,
                             stock_behavior=StockBehavior.STOCKED)
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=True)) == 0
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=False)) == 1

    # Increase stock on product
    quantity = product.get_shop_instance(shop).minimum_purchase_quantity
    simple_supplier.adjust_stock(product.id, quantity)
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=True)) == 1
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=False)) == 1

    # Decrease stock on product
    simple_supplier.adjust_stock(product.id, -quantity)
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=True)) == 0
    assert len(
        general.get_listed_products(context, n_products,
                                    orderable_only=False)) == 1
예제 #15
0
def test_best_selling_products_with_multiple_orders():
    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)
    product_2 = create_product("test-sku-2", supplier=supplier, shop=shop)
    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)
    cache.clear()
    # Two initial products sold
    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)
    create_order_with_product(product_3,
                              supplier,
                              quantity=2,
                              taxless_base_unit_price=price,
                              shop=shop)
    cache.clear()
    # Third product sold in greater quantity
    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
    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
    assert product_3 == general.get_best_selling_products(
        context, n_products=n_products)[0]
예제 #16
0
def test_get_root_categories():
    populate_if_required()
    context = get_jinja_context()
    for root in general.get_root_categories(context=context):
        assert not root.parent_id
예제 #17
0
def test_get_all_manufacturers():
    populate_if_required()
    context = get_jinja_context()
    # TODO: This is not a good test
    assert len(general.get_all_manufacturers(context)) == 0