Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add proper login error handlingfor unactive user (#8269)
Jakub0c8644b150ff
diff --git a/saleor/account/error_codes.py b/saleor/account/error_codes.pyindex c89ecd752b..9f01cda753 100644--- a/saleor/account/error_codes.py+++ b/saleor/account/error_codes.py@@ -34,6 +34,7 @@ class AccountErrorCode(Enum):JWT_INVALID_CSRF_TOKEN = "invalid_csrf_token"CHANNEL_INACTIVE = "channel_inactive"MISSING_CHANNEL_SLUG = "missing_channel_slug"+ ACCOUNT_NOT_CONFIRMED = "account_not_confirmed"class PermissionGroupErrorCode(Enum):diff --git a/saleor/graphql/account/mutations/authentication.py b/saleor/graphql/account/mutations/authentication.pyindex 6fd575c32b..745838b179 100644--- a/saleor/graphql/account/mutations/authentication.py+++ b/saleor/graphql/account/mutations/authentication.py@@ -82,7 +82,7 @@ class CreateToken(BaseMutation):@classmethoddef _retrieve_user_from_credentials(cls, email, password) -> Optional[models.User]:- user = models.User.objects.filter(email=email, is_active=True).first()+ user = models.User.objects.filter(email=email).first()if user and user.check_password(password):return userreturn None@@ -99,6 +99,25 @@ class CreateToken(BaseMutation):)})+ if not user.is_active and not user.last_login:+ raise ValidationError(+ {+ "email": ValidationError(+ "Account needs to be confirmed via email.",+ code=AccountErrorCode.ACCOUNT_NOT_CONFIRMED.value,+ )+ }+ )++ if not user.is_active and user.last_login:+ raise ValidationError(+ {+ "email": ValidationError(+ "Account inactive.",+ code=AccountErrorCode.INACTIVE.value,+ )+ }+ )return user@classmethoddiff --git a/saleor/graphql/account/tests/mutations/test_token_create.py b/saleor/graphql/account/tests/mutations/test_token_create.pyindex d884a8cdf2..e9c1d91fcf 100644--- a/saleor/graphql/account/tests/mutations/test_token_create.py+++ b/saleor/graphql/account/tests/mutations/test_token_create.py@@ -1,4 +1,4 @@-from datetime import datetime, timedelta+from datetime import datetime, timedelta, timezoneimport graphenefrom django.middleware.csrf import _get_new_csrf_token@@ -109,3 +109,72 @@ def test_create_token_invalid_email(api_client, customer_user):response_error = content["data"]["tokenCreate"]["errors"][0]assert response_error["code"] == expected_error_codeassert response_error["field"] == "email"+++def test_create_token_unconfirmed_email(api_client, customer_user):+ # given+ variables = {"email": customer_user.email, "password": customer_user._password}+ customer_user.is_active = False+ customer_user.save()+ expected_error_code = AccountErrorCode.ACCOUNT_NOT_CONFIRMED.value.upper()++ # when+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)+ content = get_graphql_content(response)+ response_error = content["data"]["tokenCreate"]["errors"][0]++ # then+ assert response_error["code"] == expected_error_code+ assert response_error["field"] == "email"+++def test_create_token_deactivated_user(api_client, customer_user):+ # given+ variables = {"email": customer_user.email, "password": customer_user._password}+ customer_user.is_active = False+ customer_user.last_login = datetime(2020, 3, 18, tzinfo=timezone.utc)+ customer_user.save()+ expected_error_code = AccountErrorCode.INACTIVE.value.upper()++ # when+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)+ content = get_graphql_content(response)+ response_error = content["data"]["tokenCreate"]["errors"][0]++ # then+ assert response_error["code"] == expected_error_code+ assert response_error["field"] == "email"+++@freeze_time("2020-03-18 12:00:00")+def test_create_token_active_user_logged_before(api_client, customer_user, settings):+ variables = {"email": customer_user.email, "password": customer_user._password}+ customer_user.last_login = datetime(2020, 3, 18, tzinfo=timezone.utc)+ customer_user.save()+ response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)+ content = get_graphql_content(response)++ data = content["data"]["tokenCreate"]++ user_email = data["user"]["email"]+ assert customer_user.email == user_email+ assert content["data"]["tokenCreate"]["errors"] == []++ token = data["token"]+ refreshToken = data["refreshToken"]++ payload = jwt_decode(token)+ assert payload["email"] == customer_user.email+ assert payload["user_id"] == graphene.Node.to_global_id("User", customer_user.id)+ assert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()+ expected_expiration_datetime = datetime.utcnow() + settings.JWT_TTL_ACCESS+ assert datetime.fromtimestamp(payload["exp"]) == expected_expiration_datetime+ assert payload["type"] == JWT_ACCESS_TYPE++ payload = jwt_decode(refreshToken)+ assert payload["email"] == customer_user.email+ assert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()+ expected_expiration_datetime = datetime.utcnow() + settings.JWT_TTL_REFRESH+ assert datetime.fromtimestamp(payload["exp"]) == expected_expiration_datetime+ assert payload["type"] == JWT_REFRESH_TYPE+ assert payload["token"] == customer_user.jwt_token_keydiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 3510cbb5c6..bb1a2d4a5b 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -70,6 +70,7 @@ enum AccountErrorCode {JWT_INVALID_CSRF_TOKENCHANNEL_INACTIVEMISSING_CHANNEL_SLUG+ ACCOUNT_NOT_CONFIRMED}input AccountInput {