def test_happy_hours_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", enabled=True) assert Shop.objects.count() == 2 # Staff user gets shop automatically data = { "name": "Happiest Hour 2pm", "weekdays": [6], # Sun "from_hour": datetime.time(hour=14, minute=0), "to_hour": datetime.time(hour=14, minute=0) } request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = HappyHourEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 happy_hour1 = HappyHour.objects.first() assert happy_hour1 is not None assert happy_hour1.shops.first() == shop assert happy_hour1.time_ranges.count( ) == 1 # Since happy hour starts and ends on same day # 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 = HappyHourEditView.as_view() response = view_func(request) assert response.status_code == 302 assert HappyHour.objects.count() == 2 happy_hour2 = HappyHour.objects.exclude(id=happy_hour1.pk).first() assert happy_hour1 != happy_hour2 assert happy_hour2.shops.count() == 1 assert happy_hour2.shops.filter(id=shop2.pk).first() # Staff user can only view coupon codes since that has the right shop _assert_view_get(rf, happy_hour1, shop, staff_user) _assert_view_get(rf, happy_hour2, shop, staff_user, True) # Superuser can see both if needed, but only when right shop is active _assert_view_get(rf, happy_hour1, shop, admin_user) _assert_view_get(rf, happy_hour2, shop, admin_user, True) _assert_view_get(rf, happy_hour2, shop2, admin_user)
def test_happy_hours_admin_edit_form_set_discount(rf, staff_user, admin_user): shop = factories.get_default_shop() shop.staff_members.add(staff_user) discount = Discount.objects.create() data = { "name": "Happiest Hour 2pm", "weekdays": [6], # Sun "from_hour": datetime.time(hour=14, minute=0), "to_hour": datetime.time(hour=14, minute=0) } request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = HappyHourEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 happy_hour = HappyHour.objects.first() data.update({"discounts": [discount]}) request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) view_func = HappyHourEditView.as_view() response = view_func(request, pk=happy_hour.pk) if hasattr(response, "render"): response.render() assert response.status_code == 302 discount = Discount.objects.first() assert discount.coupon_code is None # discount is missing shop so this shouldn't be set assert discount.shops.count() == 0 discount.shops = [shop] request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) view_func = HappyHourEditView.as_view() response = view_func(request, pk=happy_hour.pk) if hasattr(response, "render"): response.render() assert response.status_code == 302 discount = Discount.objects.first() assert discount.happy_hours.count() == 1 assert discount.happy_hours.first() == happy_hour
def test_happy_hours_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", enabled=True) assert Shop.objects.count() == 2 # Staff user gets shop automatically data = { "name": "Happiest Hour 2pm", "weekdays": [6], # Sun "from_hour": datetime.time(hour=14, minute=0), "to_hour": datetime.time(hour=14, minute=0) } request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = HappyHourEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 happy_hour1 = HappyHour.objects.first() assert happy_hour1 is not None assert happy_hour1.shops.first() == shop assert happy_hour1.time_ranges.count() == 1 # Since happy hour starts and ends on same day # 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 = HappyHourEditView.as_view() response = view_func(request) assert response.status_code == 302 assert HappyHour.objects.count() == 2 happy_hour2 = HappyHour.objects.exclude(id=happy_hour1.pk).first() assert happy_hour1 != happy_hour2 assert happy_hour2.shops.count() == 1 assert happy_hour2.shops.filter(id=shop2.pk).first() # Staff user can only view coupon codes since that has the right shop _assert_view_get(rf, happy_hour1, shop, staff_user) _assert_view_get(rf, happy_hour2, shop, staff_user, True) # Superuser can see both if needed, but only when right shop is active _assert_view_get(rf, happy_hour1, shop, admin_user) _assert_view_get(rf, happy_hour2, shop, admin_user, True) _assert_view_get(rf, happy_hour2, shop2, admin_user)
def test_happy_hours_admin_edit_view_over_midnight(rf, staff_user, admin_user): shop = factories.get_default_shop() shop.staff_members.add(staff_user) factories.get_shop(identifier="shop2", enabled=True) assert Shop.objects.count() == 2 # Staff user gets shop automatically from_hour = datetime.time(hour=21, minute=0) to_hour = datetime.time(hour=3, minute=0) data = { "name": "Happiest Hour 2pm", "weekdays": [1], # Tue "from_hour": from_hour, "to_hour": to_hour } request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = HappyHourEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 happy_hour = HappyHour.objects.first() assert happy_hour is not None assert happy_hour.shops.first() == shop assert happy_hour.time_ranges.count() == 2 # Since happy hour starts and ends on different day assert happy_hour.time_ranges.filter(weekday=1).exists() assert happy_hour.time_ranges.filter(weekday=1, from_hour=from_hour, to_hour=datetime.time(23, 59)).exists() assert happy_hour.time_ranges.filter(weekday=2, from_hour=datetime.time(0), to_hour=to_hour).exists() _assert_view_get(rf, happy_hour, shop, staff_user) # Viewing the edit should also still work
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 = HappyHourEditView.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
def test_happy_hours_admin_edit_view_over_midnight(rf, staff_user, admin_user): shop = factories.get_default_shop() shop.staff_members.add(staff_user) factories.get_shop(identifier="shop2", enabled=True) assert Shop.objects.count() == 2 # Staff user gets shop automatically from_hour = datetime.time(hour=21, minute=0) to_hour = datetime.time(hour=3, minute=0) data = { "name": "Happiest Hour 2pm", "weekdays": [1], # Tue "from_hour": from_hour, "to_hour": to_hour } request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = HappyHourEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 happy_hour = HappyHour.objects.first() assert happy_hour is not None assert happy_hour.shops.first() == shop assert happy_hour.time_ranges.count( ) == 2 # Since happy hour starts and ends on different day assert happy_hour.time_ranges.filter(weekday=1).exists() assert happy_hour.time_ranges.filter(weekday=1, from_hour=from_hour, to_hour=datetime.time(23, 59)).exists() assert happy_hour.time_ranges.filter(weekday=2, from_hour=datetime.time(0), to_hour=to_hour).exists() _assert_view_get(rf, happy_hour, shop, staff_user) # Viewing the edit should also still work
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)])