Skip to content

Commit

Permalink
fix: fixed variable name for inherited phone and email (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 authored Sep 5, 2024
1 parent 0bbe846 commit b9f276f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/apis/web/personal_center/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
GenerateCodeTooFrequently,
InvalidVerificationCode,
PhoneVerificationCodeManager,
RetryLimitExceeded,
VerificationCodeScene,
)
from bkuser.common.views import ExcludePatchAPIViewMixin
Expand Down Expand Up @@ -203,6 +204,8 @@ def _validate_verification_code(
PhoneVerificationCodeManager(phone, phone_country_code, scene).validate(code)
except InvalidVerificationCode:
raise error_codes.INVALID_VERIFICATION_CODE.f(_("验证码错误"))
except RetryLimitExceeded:
raise error_codes.VERIFY_VERIFICATION_CODE_FAILED.f(_("超过验证码重试次数"))
except Exception:
logger.exception("validate verification code for phone +%s %s failed", phone_country_code, phone)
raise error_codes.INVALID_VERIFICATION_CODE.f(_("验证码校验失败,请联系管理员处理"))
Expand Down Expand Up @@ -296,6 +299,8 @@ def _validate_verification_code(self, email: str, code: str, scene: Verification
EmailVerificationCodeManager(email, scene).validate(code)
except InvalidVerificationCode:
raise error_codes.INVALID_VERIFICATION_CODE.f(_("验证码错误"))
except RetryLimitExceeded:
raise error_codes.VERIFY_VERIFICATION_CODE_FAILED.f(_("超过验证码重试次数"))
except Exception:
logger.exception("validate verification code for email %s failed", email)
raise error_codes.INVALID_VERIFICATION_CODE.f(_("验证码校验失败,请联系管理员处理"))
Expand Down
1 change: 1 addition & 0 deletions src/bk-user/bkuser/common/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class ErrorCodes:
INVALID_VERIFICATION_CODE = ErrorCode(_("验证码无效"))
SEND_VERIFICATION_CODE_FAILED = ErrorCode(_("发送验证码失败"))
SEND_RESET_PASSWORD_EMAIL_FAILED = ErrorCode(_("发送重置密码邮件失败"))
VERIFY_VERIFICATION_CODE_FAILED = ErrorCode(_("验证失败"))


# 实例化一个全局对象
Expand Down
4 changes: 3 additions & 1 deletion src/bk-user/bkuser/common/verification_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

from .constants import VerificationCodeScene
from .exceptions import GenerateCodeTooFrequently, InvalidVerificationCode
from .exceptions import GenerateCodeTooFrequently, InvalidVerificationCode, RetryLimitExceeded
from .managers import EmailVerificationCodeManager, PhoneVerificationCodeManager

__all__ = [
Expand All @@ -23,4 +23,6 @@
"InvalidVerificationCode",
# 生成验证码过于频繁
"GenerateCodeTooFrequently",
# 重试次数过多
"RetryLimitExceeded",
]
4 changes: 4 additions & 0 deletions src/bk-user/bkuser/common/verification_code/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class InvalidVerificationCode(Exception):

class GenerateCodeTooFrequently(Exception):
"""生成验证码过于频繁"""


class RetryLimitExceeded(Exception):
"""超过验证码重试次数"""
8 changes: 6 additions & 2 deletions src/bk-user/bkuser/common/verification_code/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

from bkuser.common.cache import Cache, CacheEnum, CacheKeyPrefixEnum
from bkuser.common.verification_code.constants import VerificationCodeScene
from bkuser.common.verification_code.exceptions import GenerateCodeTooFrequently, InvalidVerificationCode
from bkuser.common.verification_code.exceptions import (
GenerateCodeTooFrequently,
InvalidVerificationCode,
RetryLimitExceeded,
)


class BaseVerificationCodeManager:
Expand All @@ -41,7 +45,7 @@ def __init__(self, contact_info_key: str, scene: VerificationCodeScene):
def validate(self, code: str):
"""校验验证码是否正确"""
if not self._can_retries():
raise InvalidVerificationCode(_("超过验证码重试次数"))
raise RetryLimitExceeded(_("超过验证码重试次数"))

if self.cache.get(self.code_cache_key) != code:
raise InvalidVerificationCode(_("验证码错误或已失效"))
Expand Down
42 changes: 42 additions & 0 deletions src/bk-user/tests/apis/web/personal_center/test_personal_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ def test_update_phone_success(self, api_client, tenant_user):
assert tenant_user.custom_phone_country_code == "86"
assert not tenant_user.is_inherited_phone

def test_update_with_inherited_blank_custom_phone(self, api_client, tenant_user):
tenant_user.custom_phone = "12345678901"
tenant_user.save(update_fields=["custom_phone"])

data = {
"is_inherited_phone": "True",
"custom_phone": "",
"custom_phone_country_code": "86",
}

with override_settings(
TENANT_PHONE_UPDATE_RESTRICTIONS={"default": PhoneOrEmailUpdateRestrictionEnum.EDITABLE_DIRECTLY}
):
resp = api_client.put(
reverse("personal_center.tenant_users.phone.update", kwargs={"id": tenant_user.id}), data=data
)
tenant_user.refresh_from_db()
assert resp.status_code == status.HTTP_204_NO_CONTENT
assert tenant_user.custom_phone == "12345678901"
assert tenant_user.custom_phone_country_code == "86"
assert tenant_user.is_inherited_phone

def test_update_phone_not_editable(self, api_client, tenant_user):
data = {
"is_inherited_phone": "False",
Expand Down Expand Up @@ -233,6 +255,26 @@ def test_update_email_success(self, api_client, tenant_user):
assert tenant_user.custom_email == "123456@qq.com"
assert not tenant_user.is_inherited_email

def test_update_with_inherited_blank_custom_email(self, api_client, tenant_user):
tenant_user.custom_email = "123456@qq.com"
tenant_user.save(update_fields=["custom_email"])

data = {
"is_inherited_email": "True",
"custom_email": "",
}

with override_settings(
TENANT_EMAIL_UPDATE_RESTRICTIONS={"default": PhoneOrEmailUpdateRestrictionEnum.EDITABLE_DIRECTLY}
):
resp = api_client.put(
reverse("personal_center.tenant_users.email.update", kwargs={"id": tenant_user.id}), data=data
)
tenant_user.refresh_from_db()
assert resp.status_code == status.HTTP_204_NO_CONTENT
assert tenant_user.custom_email == "123456@qq.com"
assert tenant_user.is_inherited_email

def test_update_email_not_editable(self, api_client, tenant_user):
data = {
"is_inherited_email": "False",
Expand Down

0 comments on commit b9f276f

Please sign in to comment.