Skip to content

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

Fix updating existing address instances

Marcin Gębala39af4e8d245d
diff --git a/saleor/graphql/account/mutations.py b/saleor/graphql/account/mutations.py
index dd62dbf034..17d39a8622 100644
--- a/saleor/graphql/account/mutations.py
+++ b/saleor/graphql/account/mutations.py
@@ -25,6 +25,10 @@ def send_user_password_reset_email(user, site):
emails.send_password_reset_email.delay(context, user.email)
+BILLING_ADDRESS_FIELD = 'default_billing_address'
+SHIPPING_ADDRESS_FIELD = 'default_shipping_address'
+
+
class UserInput(graphene.InputObjectType):
email = graphene.String(
description='The unique email address of the user.')
@@ -75,33 +79,47 @@ class CustomerCreate(ModelMutation):
def user_is_allowed(cls, user, input):
return user.has_perm('account.manage_users')
+ @classmethod
+ def construct_address(
+ cls, address_field_name, address_input, user_instance, errors):
+ if not address_field_name in [
+ BILLING_ADDRESS_FIELD, SHIPPING_ADDRESS_FIELD]:
+ raise AssertionError(
+ 'Wrong address_field_name: %s' % address_field_name)
+
+ address_instance = getattr(user_instance, address_field_name)
+ if not address_instance:
+ address_instance = models.Address()
+
+ cls.construct_instance(address_instance, address_input)
+ cls.clean_instance(address_instance, errors)
+ return address_instance
+
@classmethod
def clean_input(cls, info, instance, input, errors):
- default_shipping_address = input.pop('default_shipping_address', None)
- default_billing_address = input.pop('default_billing_address', None)
+ shipping_address_input = input.pop(SHIPPING_ADDRESS_FIELD, None)
+ billing_address_input = input.pop(BILLING_ADDRESS_FIELD, None)
cleaned_input = super().clean_input(info, instance, input, errors)
- if default_shipping_address:
- default_shipping_address = models.Address(
- **default_shipping_address)
- cls.clean_instance(default_shipping_address, errors)
- cleaned_input[
- 'default_shipping_address'] = default_shipping_address
- if default_billing_address:
- default_billing_address = models.Address(**default_billing_address)
- cls.clean_instance(default_billing_address, errors)
- cleaned_input['default_billing_address'] = default_billing_address
+ if shipping_address_input:
+ shipping_address = cls.construct_address(
+ SHIPPING_ADDRESS_FIELD, shipping_address_input, instance, errors)
+ cleaned_input[SHIPPING_ADDRESS_FIELD] = shipping_address
+
+ if billing_address_input:
+ billing_address = cls.construct_address(
+ BILLING_ADDRESS_FIELD, billing_address_input, instance, errors)
+ cleaned_input[BILLING_ADDRESS_FIELD] = billing_address
return cleaned_input
@classmethod
def save(cls, info, instance, cleaned_input):
- default_shipping_address = cleaned_input.get(
- 'default_shipping_address')
+ default_shipping_address = cleaned_input.get(SHIPPING_ADDRESS_FIELD)
if default_shipping_address:
default_shipping_address.save()
instance.default_shipping_address = default_shipping_address
- default_billing_address = cleaned_input.get('default_billing_address')
+ default_billing_address = cleaned_input.get(BILLING_ADDRESS_FIELD)
if default_billing_address:
default_billing_address.save()
instance.default_billing_address = default_billing_address
@@ -120,27 +138,6 @@ class CustomerUpdate(CustomerCreate):
exclude = ['password']
model = models.User
- @classmethod
- def clean_input(cls, info, instance, input, errors):
- default_shipping_address = input.pop('default_shipping_address', None)
- default_billing_address = input.pop('default_billing_address', None)
- cleaned_input = super().clean_input(info, instance, input, errors)
-
- if default_shipping_address:
- instance.default_shipping_address = models.Address(
- **default_shipping_address)
- cls.clean_instance(instance.default_shipping_address, errors)
- cleaned_input[
- 'default_shipping_address'] = instance.default_shipping_address
- if default_billing_address:
- instance.default_billing_address = models.Address(
- **default_billing_address)
- cls.clean_instance(instance.default_billing_address, errors)
- cleaned_input[
- 'default_billing_address'] = instance.default_billing_address
-
- return cleaned_input
-
class StaffCreate(ModelMutation):
class Arguments:
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 18e9efac13..0f145719c2 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -226,11 +226,13 @@ def test_customer_create(
send_password_reset_mock, admin_api_client, user_api_client, address):
query = """
mutation CreateCustomer(
- $email: String, $note: String, $shipping: AddressInput,
- $send_mail: Boolean) {
- customerCreate(
- input: {
- email: $email, note: $note, defaultShippingAddress: $shipping,
+ $email: String, $note: String, $billing: AddressInput,
+ $shipping: AddressInput, $send_mail: Boolean) {
+ customerCreate(input: {
+ email: $email,
+ note: $note,
+ defaultShippingAddress: $shipping,
+ defaultBillingAddress: $billing
sendPasswordEmail: $send_mail
}) {
errors {
@@ -239,9 +241,15 @@ def test_customer_create(
}
user {
id
+ defaultBillingAddress {
+ id
+ }
+ defaultShippingAddress {
+ id
+ }
email
- isStaff
isActive
+ isStaff
note
}
}
@@ -251,9 +259,9 @@ def test_customer_create(
note = 'Test user'
address_data = convert_dict_keys_to_camel_case(address.as_data())
- variables = json.dumps({
- 'email': email, 'note': note, 'shipping': address_data,
- 'send_mail': True})
+ variables = json.dumps(
+ {'email': email, 'note': note, 'shipping': address_data,
+ 'billing': address_data, 'send_mail': True})
response = user_api_client.post(
reverse('api'), {'query': query, 'variables': variables})
@@ -266,7 +274,10 @@ def test_customer_create(
User = get_user_model()
customer = User.objects.get(email=email)
+ assert customer.default_billing_address == address
assert customer.default_shipping_address == address
+ assert customer.default_shipping_address.pk != customer.default_billing_address.pk
+
assert 'errors' not in content
data = content['data']['customerCreate']
assert data['errors'] == []
@@ -286,29 +297,47 @@ def test_customer_create(
def test_customer_update(
admin_api_client, customer_user, user_api_client, address):
query = """
- mutation UpdateCustomer(
- $id: ID!, $note: String, $billing: AddressInput) {
- customerUpdate(id: $id, input: {note: $note,
- defaultBillingAddress: $billing}) {
+ mutation UpdateCustomer($id: ID!, $note: String, $billing: AddressInput, $shipping: AddressInput) {
+ customerUpdate(id: $id, input: {
+ note: $note,
+ defaultBillingAddress: $billing
+ defaultShippingAddress: $shipping
+ }) {
errors {
field
message
}
user {
id
- email
- isStaff
- isActive
note
+ defaultBillingAddress {
+ id
+ }
+ defaultShippingAddress {
+ id
+ }
}
}
}
"""
+ # this test requires addresses to be set and checks whether new address
+ # instances weren't created, but the existing ones got updated
+ assert customer_user.default_billing_address
+ assert customer_user.default_shipping_address
+ billing_address_pk = customer_user.default_billing_address.pk
+ shipping_address_pk = customer_user.default_shipping_address.pk
+
id = graphene.Node.to_global_id('User', customer_user.id)
note = 'Test update note'
address_data = convert_dict_keys_to_camel_case(address.as_data())
- variables = json.dumps({'id': id, 'note': note, 'billing': address_data})
+
+ new_street_address = 'Updated street address'
+ address_data['streetAddress1'] = new_street_address
+
+ variables = json.dumps({
+ 'id': id, 'note': note, 'billing': address_data,
+ 'shipping': address_data})
# check unauthorized access
response = user_api_client.post(
@@ -322,7 +351,13 @@ def test_customer_update(
User = get_user_model()
customer = User.objects.get(email=customer_user.email)
- assert customer.default_billing_address == address
+ # check that existing instances are updated
+ assert customer.default_billing_address.pk == billing_address_pk
+ assert customer.default_shipping_address.pk == shipping_address_pk
+
+ assert customer.default_billing_address.street_address_1 == new_street_address
+ assert customer.default_shipping_address.street_address_1 == new_street_address
+
assert 'errors' not in content
data = content['data']['customerUpdate']
assert data['errors'] == []