Example #1
0
def test_discount_admin_edit_view(rf, staff_user, admin_user):
    with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True):
        shop = factories.get_default_shop()
        shop.staff_members.add(staff_user)
        factories.get_shop(identifier="shop2")
        assert Shop.objects.count() == 2

        # Staff user gets shop automatically
        product = factories.create_product("test", shop=shop)
        discount_percentage = 20
        data = {
            "product": product.pk,
            "discount_percentage": discount_percentage
        }
        request = apply_request_middleware(rf.post("/", data=data),
                                           user=staff_user,
                                           shop=shop)
        set_shop(request, shop)
        assert request.shop == shop
        view_func = DiscountEditView.as_view()
        response = view_func(request)
        if hasattr(response, "render"):
            response.render()

        assert response.status_code == 302
        discount1 = Discount.objects.first()
        assert discount1.shops.first() == shop

        # Test with superuser and with different shop
        shop2 = factories.get_shop(enabled=True)
        request = apply_request_middleware(rf.post("/", data=data),
                                           user=admin_user,
                                           shop=shop2)
        set_shop(request, shop2)
        view_func = DiscountEditView.as_view()
        response = view_func(request)
        assert response.status_code == 302
        assert Discount.objects.count() == 2

        discount2 = Discount.objects.exclude(id=discount1.pk).first()
        assert discount1 != discount2
        assert discount2.shops.count() == 1
        assert discount2.shops.filter(id=shop2.pk).exists()

        # Staff user can only view discount1 since that has the right shop
        _assert_view_get(rf, discount1, shop, staff_user)
        _assert_view_get(rf, discount2, shop, staff_user, True)

        # Superuser can see both if needed, but only when right shop is active
        _assert_view_get(rf, discount1, shop, admin_user)
        _assert_view_get(rf, discount2, shop, admin_user, True)
        _assert_view_get(rf, discount2, shop2, admin_user)
Example #2
0
def test_admin_catalog_discount_product_filter(rf, admin_user):
    shop, supplier, contact, group, category = _init_test()
    default_price = Decimal("15.0")
    discount_percentage = Decimal("10")
    product = factories.create_product("p1", shop=shop, supplier=supplier, default_price=default_price)
    product2 = factories.create_product("p2", shop=shop, supplier=supplier, default_price=default_price)

    ProductCatalog.index_product(product)
    ProductCatalog.index_product(product2)
    view = DiscountEditView.as_view()

    # create a 10% discount for the product
    payload = _get_default_discount_data({"product": product.pk, "discount_percentage": str(discount_percentage)})
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=None)
    assert response.status_code == 302

    anon_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False))
    customer_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False, contact=contact))

    # discount is indexed
    discounted_price = (default_price - (default_price * discount_percentage * Decimal(0.01))).quantize(Decimal("0.01"))
    _assert_products_queryset(
        anon_catalog,
        [
            (product.pk, default_price, discounted_price),
            (product2.pk, default_price, None),
        ],
    )
    _assert_products_queryset(
        customer_catalog,
        [
            (product.pk, default_price, discounted_price),
            (product2.pk, default_price, None),
        ],
    )

    # changed the discount product
    discount = Discount.objects.last()
    payload = _get_default_discount_data({"product": product2.pk, "discount_percentage": str(discount_percentage)})
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=discount.pk)
    assert response.status_code == 302

    _assert_products_queryset(
        anon_catalog,
        [
            (product.pk, default_price, None),
            (product2.pk, default_price, discounted_price),
        ],
    )
    _assert_products_queryset(
        customer_catalog,
        [
            (product.pk, default_price, None),
            (product2.pk, default_price, discounted_price),
        ],
    )
Example #3
0
def test_admin_catalog_discount_contact_group_filter(rf, admin_user):
    shop, supplier, contact, group, category = _init_test()
    default_price = Decimal("15.0")
    discounted_price_value = Decimal("12")
    product = factories.create_product("p1", shop=shop, supplier=supplier, default_price=default_price)
    product.get_shop_instance(shop).categories.add(category)

    ProductCatalog.index_product(product)
    view = DiscountEditView.as_view()

    # create a discounted price for the contact group
    payload = _get_default_discount_data(
        {"contact_group": group.pk, "discounted_price_value": str(discounted_price_value)}
    )
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=None)
    assert response.status_code == 302

    anon_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False))
    customer_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False, contact=contact))

    _assert_products_queryset(anon_catalog, [(product.pk, default_price, None)])
    _assert_products_queryset(customer_catalog, [(product.pk, default_price, discounted_price_value)])

    # remove the group from the discount
    discount = Discount.objects.last()
    payload = _get_default_discount_data({"discounted_price_value": str(discounted_price_value)})
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=discount.pk)
    assert response.status_code == 302

    _assert_products_queryset(anon_catalog, [(product.pk, default_price, discounted_price_value)])
    _assert_products_queryset(customer_catalog, [(product.pk, default_price, discounted_price_value)])
Example #4
0
def _assert_view_get(rf, instance, shop, user, raises_404=False):
    request = apply_request_middleware(rf.get("/"), user=user, shop=shop)
    set_shop(request, shop)
    view_func = DiscountEditView.as_view()
    if raises_404:
        with pytest.raises(Http404):
            view_func(request, pk=instance.pk)
    else:
        response = view_func(request, pk=instance.pk)
        assert response.status_code == 200
Example #5
0
def test_admin_catalog_discount_category_filter(rf, admin_user):
    shop, supplier, contact, group, category = _init_test()
    default_price = Decimal("15.0")
    discount_amount = Decimal("5")
    product = factories.create_product("p1", shop=shop, supplier=supplier, default_price=default_price)
    product.get_shop_instance(shop).categories.add(category)

    ProductCatalog.index_product(product)
    view = DiscountEditView.as_view()

    # create a $5 discount for the category
    payload = _get_default_discount_data({"category": category.pk, "discount_amount_value": str(discount_amount)})
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=None)
    assert response.status_code == 302

    anon_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False))
    customer_catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False, contact=contact))

    # discount is indexed
    discounted_price = (default_price - discount_amount).quantize(Decimal("0.01"))
    _assert_products_queryset(anon_catalog, [(product.pk, default_price, discounted_price)])
    _assert_products_queryset(customer_catalog, [(product.pk, default_price, discounted_price)])

    # make the exclude_selected_category flag be True
    discount = Discount.objects.last()
    payload = _get_default_discount_data(
        {
            "category": category.pk,
            "exclude_selected_category": "on",
            "discount_amount_value": str(discount_amount),
        }
    )
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = view(request, pk=discount.pk)
    assert response.status_code == 302

    # discounts removed from the product
    _assert_products_queryset(anon_catalog, [(product.pk, default_price, None)])
    _assert_products_queryset(customer_catalog, [(product.pk, default_price, None)])
Example #6
0
def test_admin_catalog_discount_happy_hour_filter(rf, admin_user):
    shop, supplier, contact, group, category = _init_test()
    default_price = Decimal("15.0")
    discounted_price_value = Decimal("12")
    product = factories.create_product("p1", shop=shop, supplier=supplier, default_price=default_price)
    product.get_shop_instance(shop).categories.add(category)

    ProductCatalog.index_product(product)
    discount_view = DiscountEditView.as_view()
    happy_hour_view = HappyHourEditView.as_view()

    # create a happy hour, from 9am to 11am
    payload = {"name": "Mondays", "from_hour": "09:00", "to_hour": "11:00", "weekdays": "0"}
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = happy_hour_view(request, pk=None)
    assert response.status_code == 302

    happy_hour = HappyHour.objects.last()

    # create a discounted price using the happy hour
    payload = _get_default_discount_data(
        {
            "start_datetime": datetime(2021, 1, 1).strftime("%Y-%m-%d %H:%M"),
            "end_datetime": datetime(2022, 1, 1).strftime("%Y-%m-%d %H:%M"),
            "happy_hours": happy_hour.pk,
            "discounted_price_value": str(discounted_price_value),
        }
    )
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = discount_view(request, pk=None)
    assert response.status_code == 302

    catalog = ProductCatalog(context=ProductCatalogContext(purchasable_only=False))

    # # Monday, 6am
    # with patch.object(timezone, "now", return_value=datetime(2021, 1, 4, 6, 0, tzinfo=pytz.utc)):
    #     _assert_products_queryset(catalog, [(product.pk, default_price, None)])
    # # Monday, 10am
    # with patch.object(timezone, "now", return_value=datetime(2021, 1, 4, 10, 0, tzinfo=pytz.utc)):
    #     _assert_products_queryset(catalog, [(product.pk, default_price, discounted_price_value)])

    # change the happy hour time to Tuesdays, from 3pm to 6pm
    discount = happy_hour.discounts.last()
    assert discount
    payload = {"name": "Tuesdays", "from_hour": "15:00", "to_hour": "18:00", "weekdays": "1", "discounts": discount.pk}
    request = apply_request_middleware(rf.post("/", data=payload), shop=shop, user=admin_user)
    with patch("django.db.transaction.on_commit", new=atomic_commit_mock):
        response = happy_hour_view(request, pk=happy_hour.pk)
    assert response.status_code == 302

    # Monday, 10am
    with patch.object(timezone, "now", return_value=datetime(2021, 1, 4, 10, 0, tzinfo=pytz.utc)):
        _assert_products_queryset(catalog, [(product.pk, default_price, None)])
    # Monday, 5pm
    with patch.object(timezone, "now", return_value=datetime(2021, 1, 4, 17, 0, tzinfo=pytz.utc)):
        _assert_products_queryset(catalog, [(product.pk, default_price, None)])
    # Tuesday, 5pm
    with patch.object(timezone, "now", return_value=datetime(2021, 1, 5, 17, 0, tzinfo=pytz.utc)):
        _assert_products_queryset(catalog, [(product.pk, default_price, discounted_price_value)])