Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Handle empty param case

Tomasz Szymanski1dae361df296
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index dfbf2ef515..38af5ff59f 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -562,19 +562,37 @@ class CreateInvoice(ModelMutation):
)
class Meta:
- description = "Updates an invoice."
+ description = "Creates an invoice."
model = models.Invoice
permissions = (OrderPermissions.MANAGE_ORDERS,)
error_type_class = InvoiceError
error_type_field = "invoice_errors"
+ @classmethod
+ def clean_input(cls, info, instance, data):
+ cleaning_map = {
+ "url": InvoiceErrorCode.EMPTY_URL,
+ "number": InvoiceErrorCode.EMPTY_NUMBER,
+ }
+ for field, error_code in cleaning_map.items():
+ if data["input"][field] == "":
+ raise ValidationError(
+ {
+ field: ValidationError(
+ f"{field} cannot be empty.", code=error_code,
+ )
+ }
+ )
+ return data["input"]
+
@classmethod
def perform_mutation(cls, _root, info, **data):
- order = cls.get_node_or_error(
+ instance = cls.get_node_or_error(
info, data["order_id"], only_type=Order, field="orderId"
)
- invoice = cls.construct_instance(cls.get_instance(info, **data), data["input"])
- invoice.order = order
+ cleaned_input = cls.clean_input(info, instance, data)
+ invoice = cls.construct_instance(cls.get_instance(info, **data), cleaned_input)
+ invoice.order = instance
invoice.status = InvoiceStatus.READY
invoice.save()
return CreateInvoice(invoice=invoice)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 31f3927832..b817c5b8d3 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1880,6 +1880,8 @@ type InvoiceError {
}
enum InvoiceErrorCode {
+ EMPTY_URL
+ EMPTY_NUMBER
URL_OR_NUMBER_NOT_SET
NOT_FOUND
}
diff --git a/saleor/order/error_codes.py b/saleor/order/error_codes.py
index 00f4d4c72b..25f1bf507d 100644
--- a/saleor/order/error_codes.py
+++ b/saleor/order/error_codes.py
@@ -25,5 +25,7 @@ class OrderErrorCode(Enum):
class InvoiceErrorCode(Enum):
+ EMPTY_URL = "empty_url"
+ EMPTY_NUMBER = "empty_number"
URL_OR_NUMBER_NOT_SET = "url_or_number_not_set"
NOT_FOUND = "not_found"
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 57178022b0..f4288310a2 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -3092,9 +3092,40 @@ def test_create_invoice_missing_params(
},
]
user_api_client.user.user_permissions.add(permission_manage_orders)
+
for variables in variable_set:
response = user_api_client.post_graphql(CREATE_INVOICE_MUTATION, variables)
assert response.status_code == 400
+
+ assert not Invoice.objects.filter(
+ order_id=order.pk, status=InvoiceStatus.READY
+ ).exists()
+
+
+def test_create_invoice_empty_params(user_api_client, permission_manage_orders, orders):
+ order = orders[0]
+ number = "01/12/2020/TEST"
+ url = "http://www.example.com"
+ error_mapping = {
+ InvoiceErrorCode.EMPTY_URL: {
+ "orderId": graphene.Node.to_global_id("Order", order.pk),
+ "number": number,
+ "url": "",
+ },
+ InvoiceErrorCode.EMPTY_NUMBER: {
+ "orderId": graphene.Node.to_global_id("Order", order.pk),
+ "number": "",
+ "url": url,
+ },
+ }
+ user_api_client.user.user_permissions.add(permission_manage_orders)
+
+ for error_name, variables in error_mapping.items():
+ response = user_api_client.post_graphql(CREATE_INVOICE_MUTATION, variables)
+ content = get_graphql_content(response)
+ error = content["data"]["createInvoice"]["invoiceErrors"][0]
+ assert error["code"] == error_name.name
+
assert not Invoice.objects.filter(
order_id=order.pk, status=InvoiceStatus.READY
).exists()