Skip to content

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

Refactor addres delete mutaiions for dashboard and storefront

fowczarekad1b8995dbc4
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 2406234cff..66e7b591f3 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -6,7 +6,7 @@ from ....checkout import AddressType
from ...account.enums import AddressTypeEnum
from ...account.types import Address, AddressInput, User
from ...core.mutations import BaseMutation, ModelMutation
-from .base import BaseAddressUpdate, send_user_password_reset_email
+from .base import BaseAddressDelete, BaseAddressUpdate, send_user_password_reset_email
from .staff import CustomerCreate, UserAddressInput
@@ -191,6 +191,13 @@ class AccountAddressUpdate(BaseAddressUpdate):
class Meta:
description = "Updates an address of the logged-in user."
model = models.Address
+ exclude = ["user_addresses"]
+
+
+class AccountAddressDelete(BaseAddressDelete):
+ class Meta:
+ description = "Delete an address of the logged-in user."
+ model = models.Address
class CustomerPasswordResetInput(graphene.InputObjectType):
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 7bfc03e51d..b796e32790 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -97,6 +97,8 @@ class PasswordReset(BaseMutation):
class BaseAddressUpdate(ModelMutation):
+ """Base mutation for address update used by staff and account."""
+
user = graphene.Field(
User, description="A user instance for which the address was edited."
)
@@ -126,7 +128,9 @@ class BaseAddressUpdate(ModelMutation):
return response
-class AddressDelete(ModelDeleteMutation):
+class BaseAddressDelete(ModelDeleteMutation):
+ """Base mutation for address delete used by staff and account."""
+
user = graphene.Field(
User, description="A user instance for which the address was deleted."
)
@@ -135,8 +139,7 @@ class AddressDelete(ModelDeleteMutation):
id = graphene.ID(required=True, description="ID of the address to delete.")
class Meta:
- description = "Deletes an address"
- model = models.Address
+ abstract = True
@classmethod
def clean_instance(cls, info, instance):
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 06bfe15e7a..505b3f336a 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -29,7 +29,7 @@ from ...core.mutations import (
from ...core.types import Upload
from ...core.utils import validate_image_file
from ..utils import CustomerDeleteMixin, StaffDeleteMixin, UserDeleteMixin
-from .base import BaseAddressUpdate
+from .base import BaseAddressDelete, BaseAddressUpdate
BILLING_ADDRESS_FIELD = "default_billing_address"
SHIPPING_ADDRESS_FIELD = "default_shipping_address"
@@ -377,10 +377,17 @@ class AddressSetDefault(BaseMutation):
class AddressUpdate(BaseAddressUpdate):
class Meta:
- permissions = ("account.manage_users",)
description = "Updates an address"
model = models.Address
exclude = ["user_addresses"]
+ permissions = ("account.manage_users",)
+
+
+class AddressDelete(BaseAddressDelete):
+ class Meta:
+ description = "Deletes an address"
+ model = models.Address
+ permissions = ("account.manage_users",)
class UserAvatarUpdate(BaseMutation):
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index b9ca026219..5121cb7493 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -8,6 +8,7 @@ from .bulk_mutations import CustomerBulkDelete, StaffBulkDelete, UserBulkSetActi
from .enums import CountryCodeEnum
from .filters import CustomerFilter, StaffUserFilter
from .mutations.account import (
+ AccountAddressDelete,
AccountAddressUpdate,
AccountRequestDeletion,
AccountUpdate,
@@ -18,7 +19,6 @@ from .mutations.account import (
LoggedUserUpdate,
)
from .mutations.base import (
- AddressDelete,
PasswordReset,
SetPassword,
UserClearStoredMeta,
@@ -26,6 +26,7 @@ from .mutations.base import (
)
from .mutations.staff import (
AddressCreate,
+ AddressDelete,
AddressSetDefault,
AddressUpdate,
CustomerCreate,
@@ -124,6 +125,7 @@ class AccountMutations(graphene.ObjectType):
account_request_deletion = AccountRequestDeletion.Field()
account_address_update = AccountAddressUpdate.Field()
+ account_address_delete = AccountAddressDelete.Field()
staff_create = StaffCreate.Field()
staff_delete = StaffDelete.Field()
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index b7071a3d34..9a715ddd6b 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1449,10 +1449,12 @@ def test_customer_update_own_address(
assert address_obj.city == address_data["city"]
+ "query", [ADDRESS_UPDATE_MUTATION, ACCOUNT_ADDRESS_UPDATE_MUTATION]
+)
def test_customer_update_address_for_other(
- user_api_client, customer_user, address_other_country, graphql_address_data
+ user_api_client, customer_user, address_other_country, graphql_address_data, query
):
- query = ADDRESS_UPDATE_MUTATION
address_obj = address_other_country
assert customer_user not in address_obj.user_addresses.all()
@@ -1496,22 +1498,38 @@ def test_address_delete_mutation(
address_obj.refresh_from_db()
+ACCOUNT_ADDRESS_DELETE_MUTATION = """
+ mutation deleteUserAddress($id: ID!) {
+ accountAddressDelete(id: $id) {
+ address {
+ city
+ }
+ user {
+ id
+ }
+ }
+ }
+"""
+
+
def test_customer_delete_own_address(user_api_client, customer_user):
- query = ADDRESS_DELETE_MUTATION
+ query = ACCOUNT_ADDRESS_DELETE_MUTATION
address_obj = customer_user.addresses.first()
variables = {"id": graphene.Node.to_global_id("Address", address_obj.id)}
response = user_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
- data = content["data"]["addressDelete"]
+ data = content["data"]["accountAddressDelete"]
assert data["address"]["city"] == address_obj.city
with pytest.raises(address_obj._meta.model.DoesNotExist):
address_obj.refresh_from_db()
+ "query", [ADDRESS_DELETE_MUTATION, ACCOUNT_ADDRESS_DELETE_MUTATION]
+)
def test_customer_delete_address_for_other(
- user_api_client, customer_user, address_other_country
+ user_api_client, customer_user, address_other_country, query
):
- query = ADDRESS_DELETE_MUTATION
address_obj = address_other_country
assert customer_user not in address_obj.user_addresses.all()
variables = {"id": graphene.Node.to_global_id("Address", address_obj.id)}