Skip to content

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

Apply suggestions from code review

Filip Owczarek4f03b452b7b9
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 6e777d862a..f2ebce7107 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -15,9 +15,7 @@ from .base import (
class AccountRegisterInput(graphene.InputObjectType):
- email = graphene.String(
- description="The unique email address of the user.", required=True
- )
+ email = graphene.String(description="The email address of the user.", required=True)
password = graphene.String(description="Password", required=True)
@@ -210,7 +208,7 @@ class AccountSetDefaultAddress(BaseMutation):
address = cls.get_node_or_error(info, data.get("id"), Address)
user = info.context.user
- if address not in user.addresses.all():
+ if not user.addresses.filter(pk=address.pk).exists():
raise ValidationError({"id": "The address doesn't belong to that user."})
if data.get("type") == AddressTypeEnum.BILLING.value:
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 22af70ab7f..b4e1955dbc 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -2,6 +2,7 @@ import graphene
from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from django.db import transaction
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from graphql_jwt.exceptions import PermissionDenied
@@ -39,12 +40,13 @@ def can_edit_address(user, address):
"""Determine whether the user can edit the given address.
This method assumes that an address can be edited by:
- - users with proper permission (staff)
- - customers who "own" the given address.
+ - users with proper permissions (staff)
+ - customers associated to the given address.
"""
- has_perm = user.has_perm("account.manage_users")
- belongs_to_user = address in user.addresses.all()
- return has_perm or belongs_to_user
+ return (
+ user.has_perm("account.manage_users")
+ or user.addresses.filter(pk=address.pk).exists()
+ )
class SetPasswordInput(graphene.InputObjectType):
@@ -65,8 +67,8 @@ class SetPassword(ModelMutation):
class Meta:
description = (
- "Sets user password. Token sent by email "
- "using RequestPasswordReset mutation."
+ "Sets the user's password from the token sent by email "
+ "using the RequestPasswordReset mutation."
)
model = models.User
@@ -81,7 +83,7 @@ class SetPassword(ModelMutation):
@classmethod
def save(cls, info, instance, cleaned_input):
instance.set_password(cleaned_input["password"])
- instance.save()
+ instance.save(update_fields=["password"])
account_events.customer_password_reset_event(user=instance)
@@ -89,11 +91,11 @@ class RequestPasswordReset(BaseMutation):
class Arguments:
email = graphene.String(
required=True,
- description=("Email of the user that will be used for password recovery."),
+ description="Email of the user that will be used for password recovery.",
)
class Meta:
- description = "Sends an email with the account password change link."
+ description = "Sends an email with the account password modification link."
@classmethod
def perform_mutation(cls, _root, info, **data):
@@ -111,13 +113,13 @@ 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."
+ User, description="A user object for which the address was edited."
)
class Arguments:
id = graphene.ID(description="ID of the address to update", required=True)
input = AddressInput(
- description="Fields required to update address", required=True
+ description="Fields required to update the address", required=True
)
class Meta:
@@ -140,7 +142,7 @@ class BaseAddressUpdate(ModelMutation):
class BaseAddressDelete(ModelDeleteMutation):
- """Base mutation for address delete used by staff and account."""
+ """Base mutation for address delete used by staff and customers."""
user = graphene.Field(
User, description="A user instance for which the address was deleted."
@@ -180,14 +182,14 @@ class BaseAddressDelete(ModelDeleteMutation):
instance.delete()
instance.id = db_id
- response = cls.success_response(instance)
-
# Refresh the user instance to clear the default addresses. If the
# deleted address was used as default, it would stay cached in the
# user instance and the invalid ID returned in the response might cause
# an error.
user.refresh_from_db()
+ response = cls.success_response(instance)
+
response.user = user
return response
@@ -251,6 +253,7 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):
return cleaned_input
@classmethod
+ @transaction.atomic
def save(cls, info, instance, cleaned_input):
# FIXME: save address in user.addresses as well
default_shipping_address = cleaned_input.get(SHIPPING_ADDRESS_FIELD)
diff --git a/saleor/graphql/account/mutations/deprecated_account.py b/saleor/graphql/account/mutations/deprecated_account.py
index d531a05961..2824d2fdb5 100644
--- a/saleor/graphql/account/mutations/deprecated_account.py
+++ b/saleor/graphql/account/mutations/deprecated_account.py
@@ -38,7 +38,7 @@ class CustomerRegister(ModelMutation):
class LoggedUserUpdate(BaseCustomerCreate):
class Arguments:
input = UserAddressInput(
- description="Fields required to update logged in user.", required=True
+ description="Fields required to update a logged in user.", required=True
)
class Meta:
@@ -158,7 +158,10 @@ class CustomerPasswordReset(BaseMutation):
)
class Meta:
- description = "Resets the customer's password."
+ description = (
+ "DEPRECATED: Use RequestPasswordReset instead."
+ "Resets the customer's password."
+ )
@classmethod
def perform_mutation(cls, _root, info, **data):
diff --git a/saleor/graphql/account/mutations/deprecated_staff.py b/saleor/graphql/account/mutations/deprecated_staff.py
index 8fdef4e667..7a0f5e732e 100644
--- a/saleor/graphql/account/mutations/deprecated_staff.py
+++ b/saleor/graphql/account/mutations/deprecated_staff.py
@@ -12,7 +12,7 @@ class PasswordReset(BaseMutation):
class Meta:
description = (
- "DEPRECATED: Use CustomerRequestPasswordReset instead."
+ "DEPRECATED: Use RequestPasswordReset instead."
"Sends an email with the account password change link to customer."
)
permissions = ("account.manage_users",)
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 35539de224..2e69ebc07d 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -43,7 +43,7 @@ class StaffInput(UserInput):
class StaffCreateInput(StaffInput):
send_password_email = graphene.Boolean(
- description="Send an email with a link to set a password"
+ description="Send an email with a link to set the password"
)
@@ -97,6 +97,7 @@ class CustomerUpdate(CustomerCreate):
It overrides the `perform_mutation` base method of ModelMutation.
"""
+
# Retrieve the data
original_instance = cls.get_instance(info, **data)
data = data.get("input")
@@ -299,7 +300,7 @@ class AddressSetDefault(BaseMutation):
)
user = cls.get_node_or_error(info, user_id, field="user_id", only_type=User)
- if address not in user.addresses.all():
+ if not user.addresses.filter(pk=address.pk).exists():
raise ValidationError(
{"address_id": "The address doesn't belong to that user."}
)
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index c5d3a7c496..6c34243eae 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1011,7 +1011,7 @@ def test_account_delete(user_api_client):
response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["accountDelete"]
- assert data["errors"] == []
+ assert not data["errors"]
assert not User.objects.filter(pk=user.id).exists()
@@ -1022,7 +1022,7 @@ def test_account_delete_invalid_token(user_api_client):
response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["accountDelete"]
- assert not data["errors"] == []
+ assert len(data["errors"]) == 1
assert data["errors"][0]["message"] == "Invalid or expired token."
assert User.objects.filter(pk=user.id).exists()
@@ -1041,7 +1041,7 @@ def test_account_delete_staff_user(staff_api_client):
response = staff_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["accountDelete"]
- assert not data["errors"] == []
+ assert len(data["errors"]) == 1
assert data["errors"][0]["message"] == "Cannot delete a staff account."
assert User.objects.filter(pk=user.id).exists()
@@ -1054,7 +1054,7 @@ def test_account_delete_other_customer_token(user_api_client):
response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["accountDelete"]
- assert not data["errors"] == []
+ assert len(data["errors"]) == 1
assert data["errors"][0]["message"] == "Invalid or expired token."
assert User.objects.filter(pk=user.id).exists()
assert User.objects.filter(pk=other_user.id).exists()
@@ -1989,17 +1989,16 @@ mutation($id: ID!, $type: AddressTypeEnum!) {
(ACCOUNT_SET_DEFAULT_ADDRESS_MUTATION, "accountSetDefaultAddress"),
],
)
-def test_customer_set_address_as_default(
- user_api_client, address, query, mutation_name
-):
+def test_customer_set_address_as_default(user_api_client, query, mutation_name):
user = user_api_client.user
user.default_billing_address = None
user.default_shipping_address = None
user.save()
assert not user.default_billing_address
assert not user.default_shipping_address
+ assert user.addresses.exists()
- assert address in user.addresses.all()
+ address = user.addresses.first()
variables = {
"id": graphene.Node.to_global_id("Address", address.id),