Skip to content

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

Finish invoice update mutation, unit tests

Tomasz Szymanski247ffbf74779
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 80614a42de..80031e6267 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -563,6 +563,8 @@ class DeleteInvoice(ModelDeleteMutation):
class UpdateInvoice(ModelMutation):
+ invoice = graphene.Field(Invoice, description="An updated invoice.")
+
class Arguments:
id = graphene.ID(required=True, description="ID of an invoice to update.")
number = graphene.String(description="Invoice number")
@@ -573,12 +575,11 @@ class UpdateInvoice(ModelMutation):
model = models.Invoice
permissions = (OrderPermissions.MANAGE_ORDERS,)
- # @classmethod
- # def clean_input(cls, _info, _instance, data):
- # pass
-
@classmethod
def perform_mutation(cls, _root, info, **data):
invoice = cls.get_instance(info, **data)
- invoice.update_invoice(**data)
- return super().perform_mutation(_root, info, **data)
+ update_fields = ("number", "url")
+ invoice.update_invoice(
+ **{arg: data[arg] for arg in update_fields if data.get(arg) is not None}
+ )
+ return UpdateInvoice(invoice=invoice)
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index ff20f5807e..7014832bf8 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -526,4 +526,4 @@ class Invoice(CountableDjangoObjectType):
description = "Represents an Invoice."
interfaces = [relay.Node]
model = models.Invoice
- only_fields = ["id"]
+ only_fields = ["id", "number", "url"]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 443a13d518..c357fdaf12 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1857,6 +1857,8 @@ input IntRangeInput {
type Invoice implements Node {
id: ID!
+ number: String
+ url: String!
}
scalar JSONString
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 60cf772f7f..c91e43d53d 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -505,9 +505,9 @@ class Invoice(ModelWithMetadata):
url = models.URLField(max_length=2048)
status = models.CharField(max_length=32, default=InvoiceStatus.PENDING)
- def update_invoice(self, order, number, url, metadata):
- # TODO: modify to actual update signature for plugin
- self.number = number
- self.url = url
- self.metadata = metadata
+ def update_invoice(self, number=None, url=None):
+ if number is not None:
+ self.number = number
+ if url is not None:
+ self.url = url
self.save()
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 3ad55a73b4..5e566d8b10 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -2842,6 +2842,26 @@ DELETE_INVOICE_MUTATION = """
"""
+UPDATE_INVOICE_MUTATION = """
+ mutation UpdateInvoice($id: ID!, $number: String, $url: String) {
+ updateInvoice(
+ id: $id,
+ number: $number,
+ url: $url
+ ) {
+ invoice {
+ number
+ url
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+"""
+
+
def test_request_invoice(user_api_client, permission_manage_orders, orders):
number = "01/12/2020/TEST"
variables = {
@@ -2891,3 +2911,39 @@ def test_delete_invoice_no_permissions(user_api_client, orders):
variables = {"id": graphene.Node.to_global_id("Invoice", invoice.pk)}
response = user_api_client.post_graphql(DELETE_INVOICE_MUTATION, variables)
assert_no_permission(response)
+
+
+def test_update_invoice(user_api_client, permission_manage_orders, orders):
+ invoice = Invoice.objects.create(order=orders[0])
+ number = "01/12/2020/TEST"
+ url = "http://www.example.com"
+ variables = {
+ "id": graphene.Node.to_global_id("Invoice", invoice.pk),
+ "number": number,
+ "url": url,
+ }
+ user_api_client.user.user_permissions.add(permission_manage_orders)
+ response = user_api_client.post_graphql(UPDATE_INVOICE_MUTATION, variables)
+ content = get_graphql_content(response)
+ invoice.refresh_from_db()
+ assert invoice.number == content["data"]["updateInvoice"]["invoice"]["number"]
+ assert invoice.url == content["data"]["updateInvoice"]["invoice"]["url"]
+
+
+def test_update_invoice_no_permissions(user_api_client, orders):
+ invoice = Invoice.objects.create(order=orders[0])
+ variables = {
+ "id": graphene.Node.to_global_id("Invoice", invoice.pk),
+ "number": "01/12/2020/TEST",
+ "url": "http://www.example.com",
+ }
+ response = user_api_client.post_graphql(UPDATE_INVOICE_MUTATION, variables)
+ assert_no_permission(response)
+
+
+def test_update_invoice_invalid_id(user_api_client, permission_manage_orders):
+ variables = {"id": "SW52b2ljZToxMzM3", "number": "01/12/2020/TEST"}
+ user_api_client.user.user_permissions.add(permission_manage_orders)
+ response = user_api_client.post_graphql(UPDATE_INVOICE_MUTATION, variables)
+ content = get_graphql_content(response)
+ assert content["data"]["updateInvoice"]["errors"][0]["field"] == "id"