def test_coupon_codes_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 = {"code": "potus"} request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = CouponCodeEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 coupon1 = CouponCode.objects.first() assert coupon1.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) assert request.shop == shop2 view_func = CouponCodeEditView.as_view() response = view_func(request) assert response.status_code == 302 assert CouponCode.objects.count() == 2 coupon2 = CouponCode.objects.exclude(id=coupon1.pk).first() assert coupon1 != coupon2 assert coupon2.shops.count() == 1 assert coupon2.shops.filter(id=shop2.pk).exists() # Staff user can only view coupon codes for his shop _assert_view_get(rf, coupon1, shop, staff_user) _assert_view_get(rf, coupon2, shop, staff_user, True) # Superuser can see both if needed, but only when right shop is active _assert_view_get(rf, coupon1, shop, admin_user) _assert_view_get(rf, coupon2, shop, admin_user, True) _assert_view_get(rf, coupon2, shop2, admin_user)
def test_coupon_codes_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 = {"code": "usa"} request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) set_shop(request, shop) assert request.shop == shop view_func = CouponCodeEditView.as_view() response = view_func(request) if hasattr(response, "render"): response.render() assert response.status_code == 302 coupon1 = CouponCode.objects.first() data.update({"coupon_code_discounts": [discount]}) request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) view_func = CouponCodeEditView.as_view() response = view_func(request, pk=coupon1.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 discount.shops.add(shop) request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop) view_func = CouponCodeEditView.as_view() response = view_func(request, pk=coupon1.pk) if hasattr(response, "render"): response.render() assert response.status_code == 302 discount = Discount.objects.first() assert discount.coupon_code == coupon1
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 = CouponCodeEditView.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