Coverage for src/kwai/modules/identity/reset_password.py: 92%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.10, 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 

7from kwai.modules.identity.user_recoveries.user_recovery_repository import ( 

8 UserRecoveryRepository, 

9) 

10from kwai.modules.identity.users.user_account_repository import UserAccountRepository 

11 

12 

13class UserRecoveryExpiredException(Exception): 

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

15 

16 

17class UserRecoveryConfirmedException(Exception): 

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

19 

20 

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

22class ResetPasswordCommand: 

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

24 

25 Attributes: 

26 uuid: The unique id of the user recovery 

27 password: The new password. 

28 """ 

29 

30 uuid: str 

31 password: str 

32 

33 

34class ResetPassword: 

35 """Reset password use case. 

36 

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

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

39 """ 

40 

41 def __init__( 

42 self, 

43 user_account_repo: UserAccountRepository, 

44 user_recovery_repo: UserRecoveryRepository, 

45 ): 

46 """Initialize the use case. 

47 

48 Args: 

49 user_account_repo (UserAccountRepository): The repository for getting the 

50 user account. 

51 user_recovery_repo (UserRecoveryRepository): The repository for getting and 

52 updating the user recovery. 

53 """ 

54 self._user_account_repo = user_account_repo 

55 self._user_recovery_repo = user_recovery_repo 

56 

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

58 """Execute the use case. 

59 

60 Args: 

61 command: The input for this use case. 

62 

63 Raises: 

64 UserRecoveryNotFoundException: Raised when the user recovery with the 

65 given uuid does not exist. 

66 UserRecoveryExpiredException: Raised when the user recovery is expired. 

67 UserRecoveryConfirmedException: Raised when the user recovery is already 

68 used. 

69 UserAccountNotFoundException: Raised when the user with the email address 

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

71 NotAllowedException: Raised when the user is revoked. 

72 """ 

73 user_recovery = await self._user_recovery_repo.get_by_uuid( 

74 UniqueId.create_from_string(command.uuid) 

75 ) 

76 if user_recovery.is_expired: 

77 raise UserRecoveryExpiredException() 

78 if user_recovery.confirmed: 

79 raise UserRecoveryConfirmedException() 

80 

81 user_account = await self._user_account_repo.get_user_by_email( 

82 user_recovery.user.email 

83 ) 

84 

85 user_account.reset_password(Password.create_from_string(command.password)) 

86 await self._user_account_repo.update(user_account) 

87 

88 user_recovery.confirm() 

89 await self._user_recovery_repo.update(user_recovery)