Skip to content

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

Add logged user update mutation

fowczarek54d6b679a9d0
diff --git a/saleor/graphql/account/mutations.py b/saleor/graphql/account/mutations.py
index 7dbc570f41..f641ff01af 100644
--- a/saleor/graphql/account/mutations.py
+++ b/saleor/graphql/account/mutations.py
@@ -62,13 +62,17 @@ class UserInput(graphene.InputObjectType):
note = graphene.String(description='A note about the user.')
-class CustomerInput(UserInput):
+class UserAddressInput(graphene.InputObjectType):
default_billing_address = AddressInput(
description='Billing address of the customer.')
default_shipping_address = AddressInput(
description='Shipping address of the customer.')
+class CustomerInput(UserInput, UserAddressInput):
+ pass
+
+
class UserCreateInput(CustomerInput):
send_password_email = graphene.Boolean(
description='Send an email with a link to set a password')
@@ -148,6 +152,25 @@ class CustomerUpdate(CustomerCreate):
model = models.User
+class LoggedCustomerUpdate(CustomerCreate):
+ class Arguments:
+ id = graphene.ID(
+ description='ID of a customer to update.', required=True)
+ input = UserAddressInput(
+ description='Fields required to update logged in customer.',
+ required=True)
+
+ class Meta:
+ description = 'Updates an existing logged in customer.'
+ exclude = ['password']
+ model = models.User
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ id = graphene.Node.to_global_id('User', user.id)
+ return user.has_perm('account.manage_users') or id == input['id']
+
+
class UserDelete(ModelDeleteMutation):
class Meta:
abstract = True
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index f3aa44214c..38837ebbca 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -6,7 +6,8 @@ from ..descriptions import DESCRIPTIONS
from .mutations import (
AddressCreate, AddressDelete, AddressUpdate, CustomerCreate,
CustomerDelete, CustomerPasswordReset, CustomerRegister, CustomerUpdate,
- PasswordReset, SetPassword, StaffCreate, StaffDelete, StaffUpdate)
+ LoggedCustomerUpdate, PasswordReset, SetPassword, StaffCreate, StaffDelete,
+ StaffUpdate)
from .resolvers import (
resolve_address_validator, resolve_customers, resolve_staff_users,
resolve_user)
@@ -53,6 +54,8 @@ class AccountMutations(graphene.ObjectType):
customer_register = CustomerRegister.Field()
customer_update = CustomerUpdate.Field()
+ logged_customer_update = LoggedCustomerUpdate.Field()
+
staff_create = StaffCreate.Field()
staff_delete = StaffDelete.Field()
staff_update = StaffUpdate.Field()
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index a04160dd4f..67f1875628 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -435,6 +435,117 @@ def test_customer_update(
assert not data['user']['isActive']
+def test_logged_customer_update(user_api_client, address):
+ query = """
+ mutation UpdateCustomer($id: ID!, $billing: AddressInput,
+ $shipping: AddressInput) {
+ loggedCustomerUpdate(id: $id,
+ input: {
+ defaultBillingAddress: $billing,
+ defaultShippingAddress: $shipping,
+ }) {
+ errors {
+ field
+ message
+ }
+ user {
+ id
+ email
+ 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
+ user = user_api_client.user
+ assert user.default_billing_address
+ assert user.default_shipping_address
+ billing_address_pk = user.default_billing_address.pk
+ shipping_address_pk = user.default_shipping_address.pk
+
+ id = graphene.Node.to_global_id('User', user.id)
+ address_data = convert_dict_keys_to_camel_case(address.as_data())
+
+ new_street_address = 'Updated street address'
+ address_data['streetAddress1'] = new_street_address
+
+ variables = {
+ 'id': id, 'billing': address_data,
+ 'shipping': address_data}
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+
+ User = get_user_model()
+ customer = User.objects.get(email=user.email)
+
+ customer_billing_address = customer.default_billing_address
+ customer_shipping_address = customer.default_shipping_address
+
+ # check that existing instances are updated
+ assert customer_billing_address.pk == billing_address_pk
+ assert customer_shipping_address.pk == shipping_address_pk
+
+ assert customer_billing_address.street_address_1 == new_street_address
+ assert customer_shipping_address.street_address_1 == new_street_address
+
+ data = content['data']['loggedCustomerUpdate']
+ assert data['errors'] == []
+
+
+def test_user_without_premission_can_only_update_own_data(staff_api_client,
+ customer_user,
+ address):
+ query = """
+ mutation UpdateCustomer($id: ID!, $billing: AddressInput,
+ $shipping: AddressInput) {
+ loggedCustomerUpdate(id: $id,
+ input: {
+ defaultBillingAddress: $billing,
+ defaultShippingAddress: $shipping,
+ }) {
+ errors {
+ field
+ message
+ }
+ user {
+ id
+ email
+ defaultBillingAddress {
+ id
+ }
+ defaultShippingAddress {
+ id
+ }
+ }
+ }
+ }
+ """
+
+ 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)
+ address_data = convert_dict_keys_to_camel_case(address.as_data())
+
+ new_street_address = 'Updated street address'
+ address_data['streetAddress1'] = new_street_address
+
+ variables = {
+ 'id': id, 'billing': address_data,
+ 'shipping': address_data}
+ response = staff_api_client.post_graphql(query, variables)
+ assert_no_permission(response)
+
+
def test_customer_delete(staff_api_client, customer_user, permission_manage_users):
query = """
mutation CustomerDelete($id: ID!) {