Skip to content

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

Fix missing account_errors in CreateToken mutation

Michał Sipa82b81b3a4807
diff --git a/saleor/account/error_codes.py b/saleor/account/error_codes.py
index 872303d901..35989449d8 100644
--- a/saleor/account/error_codes.py
+++ b/saleor/account/error_codes.py
@@ -14,6 +14,7 @@ class AccountErrorCode(Enum):
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
INVALID_PASSWORD = "invalid_password"
+ INVALID_CREDENTIALS = "invalid_credentials"
NOT_FOUND = "not_found"
PASSWORD_ENTIRELY_NUMERIC = "password_entirely_numeric"
PASSWORD_TOO_COMMON = "password_too_common"
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 7905e91cb6..71711bfa02 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -18,6 +18,7 @@ from graphql_jwt.exceptions import JSONWebTokenError, PermissionDenied
from ...account import models
from ..account.types import User
+from ...account.error_codes import AccountErrorCode
from ..utils import get_nodes
from .types import Error, Upload
from .types.common import AccountError
@@ -601,7 +602,15 @@ class CreateToken(ObtainJSONWebToken):
try:
result = super().mutate(root, info, **kwargs)
except JSONWebTokenError as e:
- return CreateToken(errors=[Error(message=str(e))])
+ errors = [Error(message=str(e))]
+ account_errors = [
+ AccountError(
+ field="email",
+ message="Please, enter valid credentials",
+ code=AccountErrorCode.INVALID_CREDENTIALS,
+ )
+ ]
+ return CreateToken(errors=errors, account_errors=account_errors)
except ValidationError as e:
errors = validation_error_to_error_type(e)
return cls.handle_typed_errors(errors)
diff --git a/tests/api/test_core.py b/tests/api/test_core.py
index b0bf58680c..610b1a609a 100644
--- a/tests/api/test_core.py
+++ b/tests/api/test_core.py
@@ -24,6 +24,7 @@ from saleor.graphql.utils import (
reporting_period_to_date,
)
from saleor.product.models import Category, Product
+from saleor.account.error_codes import AccountErrorCode
from tests.api.utils import _get_graphql_content_from_response, get_graphql_content
@@ -288,6 +289,68 @@ def test_mutation_invalid_permission_in_meta(_mocked, should_fail, permissions_v
assert exc.value.args[0] == "Permissions should be a tuple or a string in Meta"
+MUTATION_CREATE_TOKEN = """
+ mutation tokenCreate($email: String!, $password: String!){
+ tokenCreate(email: $email, password: $password) {
+ token
+ user {
+ email
+ }
+ errors {
+ field
+ message
+ }
+ accountErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+"""
+
+
+def test_create_token(api_client, customer_user):
+ variables = {"email": customer_user.email, "password": customer_user._password}
+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)
+ content = get_graphql_content(response)
+ user_email = content["data"]["tokenCreate"]["user"]["email"]
+ assert customer_user.email == user_email
+ assert content["data"]["tokenCreate"]["token"]
+ assert content["data"]["tokenCreate"]["errors"] == []
+ assert content["data"]["tokenCreate"]["accountErrors"] == []
+
+
+def test_create_token_invalid_password(api_client, customer_user):
+ variables = {"email": customer_user.email, "password": "wrongpassword"}
+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)
+ content = get_graphql_content(response)
+ assert (
+ content["data"]["tokenCreate"]["errors"][0]["message"]
+ == "Please, enter valid credentials"
+ )
+ assert (
+ content["data"]["tokenCreate"]["accountErrors"][0]["code"]
+ == AccountErrorCode.INVALID_CREDENTIALS.value.upper()
+ )
+ assert content["data"]["tokenCreate"]["accountErrors"][0]["field"] == "email"
+
+
+def test_create_token_invalid_email(api_client, customer_user):
+ variables = {"email": "wrongemail", "password": "wrongpassword"}
+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)
+ content = get_graphql_content(response)
+ assert (
+ content["data"]["tokenCreate"]["errors"][0]["message"]
+ == "Please, enter valid credentials"
+ )
+ assert (
+ content["data"]["tokenCreate"]["accountErrors"][0]["code"]
+ == AccountErrorCode.INVALID_CREDENTIALS.value.upper()
+ )
+ assert content["data"]["tokenCreate"]["accountErrors"][0]["field"] == "email"
+
+
MUTATION_TOKEN_VERIFY = """
mutation tokenVerify($token: String!){
tokenVerify(token: $token){