示例#1
0
def test_create_refund_view(rf, admin_user):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product(sku="test-sku", shop=shop, supplier=supplier, default_price=3.33)
    order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=1, shop=shop)
    order.cache_prices()
    order.save()

    assert not order.has_refunds()
    assert len(order.lines.all()) == 1

    product_line = order.lines.first()

    data = {
        "form-0-line_number": 0,
        "form-0-quantity": 1,
        "form-0-amount": 1,
        "form-0-restock_products": False,
        "form-INITIAL_FORMS": 0,
        "form-MAX_NUM_FORMS": 1000,
        "form-TOTAL_FORMS": 1,
        "form-MIN_NUM_FORMS": 0,
    }

    request = apply_request_middleware(rf.post("/", data=data), user=admin_user)
    view = OrderCreateRefundView.as_view()
    response = view(request, pk=order.pk)
    assert response.status_code == 302
    assert order.has_refunds()

    assert len(order.lines.all()) == 2
    refund_line = order.lines.filter(type=OrderLineType.REFUND).last()
    assert refund_line
    assert refund_line.taxful_price == -product_line.taxful_price
示例#2
0
def test_create_refund_view(rf, admin_user):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product(sku="test-sku", shop=shop, supplier=supplier, default_price=3.33)
    order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=1, shop=shop)
    order.cache_prices()
    order.save()

    assert not order.has_refunds()
    assert len(order.lines.all()) == 1

    product_line = order.lines.first()

    data = {
        "form-0-line_number": 0,
        "form-0-quantity": 1,
        "form-0-amount": 1,
        "form-0-restock_products": False,
        "form-INITIAL_FORMS": 0,
        "form-MAX_NUM_FORMS": 1000,
        "form-TOTAL_FORMS": 1,
        "form-MIN_NUM_FORMS": 0,
    }

    request = apply_request_middleware(rf.post("/", data=data), user=admin_user)
    view = OrderCreateRefundView.as_view()
    response = view(request, pk=order.pk)
    assert response.status_code == 302
    assert order.has_refunds()

    assert len(order.lines.all()) == 2
    refund_line = order.lines.filter(type=OrderLineType.REFUND).last()
    assert refund_line
    assert refund_line.taxful_price == -product_line.taxful_price
示例#3
0
 def get_refund_view_content():
     request = apply_request_middleware(rf.get("/"), user=admin_user)
     view = OrderCreateRefundView.as_view()
     response = view(request, pk=order.pk)
     return BeautifulSoup(response.render().content)
示例#4
0
def test_refunds_with_multiple_suppliers(rf, admin_user):
    shop = get_default_shop()
    supplier1 = Supplier.objects.create(identifier="1", name="supplier1")
    supplier1.shops.add(shop)

    supplier2 = Supplier.objects.create(identifier="2")
    supplier2.shops.add(shop)

    supplier3 = Supplier.objects.create(identifier="3", name="s")
    supplier3.shops.add(shop)

    product1 = create_product("sku1", shop=shop, default_price=10)
    shop_product1 = product1.get_shop_instance(shop=shop)
    shop_product1.suppliers = [supplier1, supplier2, supplier3]

    product2 = create_product("sku2", shop=shop, default_price=10)
    shop_product2 = product1.get_shop_instance(shop=shop)
    shop_product2.suppliers = [supplier1, supplier2]

    product3 = create_product("sku3",
                              shop=shop,
                              default_price=10,
                              shipping_mode=ShippingMode.NOT_SHIPPED)
    shop_product3 = product1.get_shop_instance(shop=shop)
    shop_product3.suppliers = [supplier3]

    product_quantities = {
        supplier1: {
            product1: 5,
            product2: 6
        },
        supplier2: {
            product1: 3,
            product2: 13
        },
        supplier3: {
            product1: 1,
            product3: 50
        }
    }

    def get_quantity(supplier, product):
        return product_quantities[supplier.pk][product.pk]

    order = create_empty_order(shop=shop)
    order.full_clean()
    order.save()

    for supplier, product_data in six.iteritems(product_quantities):
        for product, quantity in six.iteritems(product_data):
            add_product_to_order(order, supplier, product, quantity, 5)

    order.cache_prices()
    order.create_payment(order.taxful_total_price)
    assert order.is_paid()

    # All supplier should be able to refund the order
    assert order.can_create_refund()
    assert order.can_create_refund(supplier1)
    assert order.can_create_refund(supplier2)
    assert order.can_create_refund(supplier3)

    assert not order.has_refunds()
    assert len(order.lines.all()) == 6

    product_line = order.lines.first()
    data = {
        "form-0-line_number": 0,
        "form-0-quantity": 1,
        "form-0-amount": 1,
        "form-0-restock_products": False,
        "form-INITIAL_FORMS": 0,
        "form-MAX_NUM_FORMS": 1000,
        "form-TOTAL_FORMS": 1,
        "form-MIN_NUM_FORMS": 0,
    }

    supplier_provider = "shuup.testing.supplier_provider.RequestSupplierProvider"
    with override_settings(
            SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC=supplier_provider):
        # Test refund view content as superuser
        request = apply_request_middleware(rf.get("/", data=data),
                                           user=admin_user)
        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 6)
        _assert_order_mobile_list_row_count(soup, 6)
        assert _get_create_full_refund_button(soup) is not None

        # Test refund view content as supplier 1
        request = apply_request_middleware(rf.get("/", data=data),
                                           user=admin_user)
        request.supplier = supplier1
        assert get_supplier(request) == supplier1

        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 2)
        _assert_order_mobile_list_row_count(soup, 2)
        assert _get_create_full_refund_button(soup) is None

        # Test refund view content as supplier 2 user
        request = apply_request_middleware(rf.get("/", data=data),
                                           user=admin_user)
        request.supplier = supplier2
        assert get_supplier(request) == supplier2

        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 2)
        _assert_order_mobile_list_row_count(soup, 2)
        assert _get_create_full_refund_button(soup) is None
def test_refunds_with_multiple_suppliers(rf, admin_user):
    shop = get_default_shop()
    supplier1 = Supplier.objects.create(identifier="1", name="supplier1")
    supplier1.shops.add(shop)

    supplier2 = Supplier.objects.create(identifier="2")
    supplier2.shops.add(shop)

    supplier3 = Supplier.objects.create(identifier="3", name="s")
    supplier3.shops.add(shop)

    product1 = create_product("sku1", shop=shop, default_price=10)
    shop_product1 = product1.get_shop_instance(shop=shop)
    shop_product1.suppliers = [supplier1, supplier2, supplier3]

    product2 = create_product("sku2", shop=shop, default_price=10)
    shop_product2 = product1.get_shop_instance(shop=shop)
    shop_product2.suppliers = [supplier1, supplier2]

    product3 = create_product("sku3", shop=shop, default_price=10, shipping_mode=ShippingMode.NOT_SHIPPED)
    shop_product3 = product1.get_shop_instance(shop=shop)
    shop_product3.suppliers = [supplier3]

    product_quantities = {
        supplier1: {
            product1: 5,
            product2: 6
        },
        supplier2: {
            product1: 3,
            product2: 13
        },
        supplier3: {
            product1: 1,
            product3: 50
        }
    }

    def get_quantity(supplier, product):
        return product_quantities[supplier.pk][product.pk]

    order = create_empty_order(shop=shop)
    order.full_clean()
    order.save()

    for supplier, product_data in six.iteritems(product_quantities):
        for product, quantity in six.iteritems(product_data):
            add_product_to_order(order, supplier, product, quantity, 5)

    order.cache_prices()
    order.create_payment(order.taxful_total_price)
    assert order.is_paid()

    # All supplier should be able to refund the order
    assert order.can_create_refund()
    assert order.can_create_refund(supplier1)
    assert order.can_create_refund(supplier2)
    assert order.can_create_refund(supplier3)

    assert not order.has_refunds()
    assert len(order.lines.all()) == 6

    product_line = order.lines.first()
    data = {
        "form-0-line_number": 0,
        "form-0-quantity": 1,
        "form-0-amount": 1,
        "form-0-restock_products": False,
        "form-INITIAL_FORMS": 0,
        "form-MAX_NUM_FORMS": 1000,
        "form-TOTAL_FORMS": 1,
        "form-MIN_NUM_FORMS": 0,
    }

    supplier_provider = "shuup.testing.supplier_provider.RequestSupplierProvider"
    with override_settings(SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC=supplier_provider):
        # Test refund view content as superuser
        request = apply_request_middleware(rf.get("/", data=data), user=admin_user)
        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 6)
        _assert_order_mobile_list_row_count(soup, 6)
        assert _get_create_full_refund_button(soup) is not None

        # Test refund view content as supplier 1
        request = apply_request_middleware(rf.get("/", data=data), user=admin_user)
        request.supplier = supplier1
        assert get_supplier(request) == supplier1

        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 2)
        _assert_order_mobile_list_row_count(soup, 2)
        assert _get_create_full_refund_button(soup) is None

        # Test refund view content as supplier 2 user
        request = apply_request_middleware(rf.get("/", data=data), user=admin_user)
        request.supplier = supplier2
        assert get_supplier(request) == supplier2

        view = OrderCreateRefundView.as_view()
        response = view(request, pk=order.pk)
        assert response.status_code == 200
        if hasattr(response, "render"):
            response.render()

        soup = BeautifulSoup(response.content)
        _assert_order_table_row_count(soup, 2)
        _assert_order_mobile_list_row_count(soup, 2)
        assert _get_create_full_refund_button(soup) is None