Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extra validation for billing address
Tomasz Szymanskif2aeb10f0182
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex 6ab22dea08..93bec4b5d1 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -532,11 +532,34 @@ class RequestInvoice(BaseMutation):required=False, description="Invoice number (optional).")+ @classmethod+ def clean_instance(cls, info, instance):+ if instance.status == OrderStatus.DRAFT:+ raise ValidationError(+ {+ "orderId": ValidationError(+ "Provided order status cannot be draft.",+ code=InvoiceErrorCode.INVALID_STATUS,+ )+ }+ )++ if not instance.billing_address:+ raise ValidationError(+ {+ "orderId": ValidationError(+ "Billing address is not set on order.",+ code=InvoiceErrorCode.NOT_READY,+ )+ }+ )+@classmethoddef perform_mutation(cls, _root, info, **data):order = cls.get_node_or_error(info, data["order_id"], only_type=Order, field="orderId")+ cls.clean_instance(info, order)invoice = models.Invoice.objects.create(order=order, status=InvoiceStatus.PENDING, number=data.get("number")@@ -595,6 +618,16 @@ class CreateInvoice(ModelMutation):})+ if not instance.billing_address:+ raise ValidationError(+ {+ "orderId": ValidationError(+ "Billing address is not set on order.",+ code=InvoiceErrorCode.NOT_READY,+ )+ }+ )+@classmethoddef perform_mutation(cls, _root, info, **data):instance = cls.get_node_or_error(@@ -609,8 +642,8 @@ class CreateInvoice(ModelMutation):events.invoice_created_event(user=info.context.user,invoice=invoice,- number=data["input"].get("number"),- url=data["input"].get("url"),+ number=cleaned_input["number"],+ url=cleaned_input["url"],)return CreateInvoice(invoice=invoice)diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 0c52493470..066c9b7506 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -3026,6 +3026,44 @@ def test_request_invoice().exists()+def test_request_invoice_draft_order(staff_api_client, permission_manage_orders, order):+ order.status = OrderStatus.DRAFT+ order.save()+ number = "01/12/2020/TEST"+ variables = {+ "orderId": graphene.Node.to_global_id("Order", order.pk),+ "number": number,+ }+ response = staff_api_client.post_graphql(+ REQUEST_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ assert not Invoice.objects.filter(number=number, order=order.pk).exists()+ error = content["data"]["requestInvoice"]["invoiceErrors"][0]+ assert error["field"] == "orderId"+ assert error["code"] == InvoiceErrorCode.INVALID_STATUS.name+++def test_request_invoice_no_billing_address(+ staff_api_client, permission_manage_orders, order+):+ order.billing_address = None+ order.save()+ number = "01/12/2020/TEST"+ variables = {+ "orderId": graphene.Node.to_global_id("Order", order.pk),+ "number": number,+ }+ response = staff_api_client.post_graphql(+ REQUEST_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ assert not Invoice.objects.filter(number=number, order=order.pk).exists()+ error = content["data"]["requestInvoice"]["invoiceErrors"][0]+ assert error["field"] == "orderId"+ assert error["code"] == InvoiceErrorCode.NOT_READY.name++def test_request_invoice_no_number(staff_api_client, permission_manage_orders, order):variables = {"orderId": graphene.Node.to_global_id("Order", order.pk)}staff_api_client.post_graphql(@@ -3216,6 +3254,30 @@ def test_create_invoice(staff_api_client, permission_manage_orders, order):).exists()+def test_create_invoice_no_billing_address(+ staff_api_client, permission_manage_orders, order+):+ order.billing_address = None+ order.save()+ number = "01/12/2020/TEST"+ url = "http://www.example.com"+ variables = {+ "orderId": graphene.Node.to_global_id("Order", order.pk),+ "number": number,+ "url": url,+ }+ response = staff_api_client.post_graphql(+ CREATE_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ assert not Invoice.objects.filter(+ order_id=order.pk, url=url, number=number+ ).exists()+ error = content["data"]["createInvoice"]["invoiceErrors"][0]+ assert error["field"] == "orderId"+ assert error["code"] == InvoiceErrorCode.NOT_READY.name++def test_create_invoice_for_draft_order(staff_api_client, permission_manage_orders, order):