Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/reset_password.py: 92%

24 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module that implements the reset password use case.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.value_objects.password import Password 

6from kwai_core.domain.value_objects.unique_id import UniqueId 

7 

8from kwai_bc_identity.user_recoveries.user_recovery_repository import ( 

9 UserRecoveryRepository, 

10) 

11from kwai_bc_identity.users.user_account_repository import UserAccountRepository 

12 

13 

14class UserRecoveryExpiredException(Exception): 

15 """Raised when the user recovery is expired.""" 

16 

17 

18class UserRecoveryConfirmedException(Exception): 

19 """Raised when the user recovery was already used.""" 

20 

21 

22@dataclass(frozen=True, kw_only=True) 

23class ResetPasswordCommand: 

24 """Command for the reset password use case. 

25 

26 Attributes: 

27 uuid: The unique id of the user recovery 

28 password: The new password. 

29 """ 

30 

31 uuid: str 

32 password: str 

33 

34 

35class ResetPassword: 

36 """Reset password use case. 

37 

38 This use case will try to reset the password of a user. A user can reset the 

39 password with a unique id. This unique id is linked to a user recovery. 

40 """ 

41 

42 def __init__( 

43 self, 

44 user_account_repo: UserAccountRepository, 

45 user_recovery_repo: UserRecoveryRepository, 

46 ): 

47 """Initialize the use case. 

48 

49 Args: 

50 user_account_repo (UserAccountRepository): The repository for getting the 

51 user account. 

52 user_recovery_repo (UserRecoveryRepository): The repository for getting and 

53 updating the user recovery. 

54 """ 

55 self._user_account_repo = user_account_repo 

56 self._user_recovery_repo = user_recovery_repo 

57 

58 async def execute(self, command: ResetPasswordCommand) -> None: 

59 """Execute the use case. 

60 

61 Args: 

62 command: The input for this use case. 

63 

64 Raises: 

65 UserRecoveryNotFoundException: Raised when the user recovery with the 

66 given uuid does not exist. 

67 UserRecoveryExpiredException: Raised when the user recovery is expired. 

68 UserRecoveryConfirmedException: Raised when the user recovery is already 

69 used. 

70 UserAccountNotFoundException: Raised when the user with the email address 

71 that belongs to the user recovery, does not exist. 

72 NotAllowedException: Raised when the user is revoked. 

73 """ 

74 user_recovery = await self._user_recovery_repo.get_by_uuid( 

75 UniqueId.create_from_string(command.uuid) 

76 ) 

77 if user_recovery.is_expired: 

78 raise UserRecoveryExpiredException() 

79 if user_recovery.confirmed: 

80 raise UserRecoveryConfirmedException() 

81 

82 user_account = await self._user_account_repo.get_user_by_email( 

83 user_recovery.user.email 

84 ) 

85 

86 user_account = user_account.reset_password( 

87 Password.create_from_string(command.password) 

88 ) 

89 await self._user_account_repo.update(user_account) 

90 

91 user_recovery = user_recovery.confirm() 

92 await self._user_recovery_repo.update(user_recovery)