Esempio n. 1
0
def test_exceptions_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": "No deals in next 2 days!",
            "start_datetime": datetime.datetime(year=2018, month=11, day=9),
            "end_datetime": datetime.datetime(year=2018, month=11, day=11),
        }
        request = apply_request_middleware(rf.post("/", data=data),
                                           user=staff_user,
                                           shop=shop)
        set_shop(request, shop)
        assert request.shop == shop
        view_func = AvailabilityExceptionEditView.as_view()
        response = view_func(request)
        if hasattr(response, "render"):
            response.render()

        assert response.status_code == 302
        exception1 = AvailabilityException.objects.first()
        assert exception1 is not None
        assert exception1.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 = AvailabilityExceptionEditView.as_view()
        response = view_func(request)
        assert response.status_code == 302
        assert AvailabilityException.objects.count() == 2

        exception2 = AvailabilityException.objects.exclude(
            id=exception1.pk).first()
        assert exception1 != exception2
        assert exception2.shops.count() == 1
        assert exception2.shops.filter(id=shop2.pk).exists()

        # Staff user can only view coupon codes since that has the right shop
        _assert_view_get(rf, exception1, shop, staff_user)
        _assert_view_get(rf, exception2, shop, staff_user, True)

        # Superuser can see both if needed, but only when right shop is active
        _assert_view_get(rf, exception1, shop, admin_user)
        _assert_view_get(rf, exception2, shop, admin_user, True)
        _assert_view_get(rf, exception2, shop2, admin_user)
Esempio n. 2
0
def test_exceptions_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": "No deals in next 2 days!",
        "start_datetime": datetime.datetime(year=2018, month=11, day=9),
        "end_datetime": datetime.datetime(year=2018, month=11, day=11),
    }
    request = apply_request_middleware(rf.post("/", data=data),
                                       user=staff_user,
                                       shop=shop)
    set_shop(request, shop)
    assert request.shop == shop
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request)
    if hasattr(response, "render"):
        response.render()

    assert response.status_code == 302
    exception1 = AvailabilityException.objects.first()
    data.update({"discounts": [discount]})
    request = apply_request_middleware(rf.post("/", data=data),
                                       user=staff_user,
                                       shop=shop)
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request, pk=exception1.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 = [shop]
    request = apply_request_middleware(rf.post("/", data=data),
                                       user=staff_user,
                                       shop=shop)
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request, pk=exception1.pk)
    if hasattr(response, "render"):
        response.render()

    assert response.status_code == 302
    discount = Discount.objects.first()
    assert discount.availability_exceptions.count() == 1
    assert discount.availability_exceptions.first() == exception1
Esempio n. 3
0
def test_exceptions_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": "No deals in next 2 days!",
            "start_datetime": datetime.datetime(year=2018, month=11, day=9),
            "end_datetime": datetime.datetime(year=2018, month=11, day=11),
        }
        request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop)
        set_shop(request, shop)
        assert request.shop == shop
        view_func = AvailabilityExceptionEditView.as_view()
        response = view_func(request)
        if hasattr(response, "render"):
            response.render()

        assert response.status_code == 302
        exception1 = AvailabilityException.objects.first()
        assert exception1 is not None
        assert exception1.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 = AvailabilityExceptionEditView.as_view()
        response = view_func(request)
        assert response.status_code == 302
        assert AvailabilityException.objects.count() == 2

        exception2 = AvailabilityException.objects.exclude(id=exception1.pk).first()
        assert exception1 != exception2
        assert exception2.shops.count() == 1
        assert exception2.shops.filter(id=shop2.pk).exists()

        # Staff user can only view coupon codes since that has the right shop
        _assert_view_get(rf, exception1, shop, staff_user)
        _assert_view_get(rf, exception2, shop, staff_user, True)

        # Superuser can see both if needed, but only when right shop is active
        _assert_view_get(rf, exception1, shop, admin_user)
        _assert_view_get(rf, exception2, shop, admin_user, True)
        _assert_view_get(rf, exception2, shop2, admin_user)
Esempio n. 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 = AvailabilityExceptionEditView.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
Esempio n. 5
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 = AvailabilityExceptionEditView.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
Esempio n. 6
0
def test_exceptions_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": "No deals in next 2 days!",
        "start_datetime": datetime.datetime(year=2018, month=11, day=9),
        "end_datetime": datetime.datetime(year=2018, month=11, day=11),
    }
    request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop)
    set_shop(request, shop)
    assert request.shop == shop
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request)
    if hasattr(response, "render"):
        response.render()

    assert response.status_code == 302
    exception1 = AvailabilityException.objects.first()
    data.update({"discounts": [discount]})
    request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop)
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request, pk=exception1.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 = [shop]
    request = apply_request_middleware(rf.post("/", data=data), user=staff_user, shop=shop)
    view_func = AvailabilityExceptionEditView.as_view()
    response = view_func(request, pk=exception1.pk)
    if hasattr(response, "render"):
        response.render()

    assert response.status_code == 302
    discount = Discount.objects.first()
    assert discount.availability_exceptions.count() == 1
    assert discount.availability_exceptions.first() == exception1