Coverage for kwai/modules/identity/reset_password.py: 92%
26 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module that implements the reset password use case."""
2from dataclasses import dataclass
4from kwai.core.domain.value_objects.password import Password
5from kwai.core.domain.value_objects.unique_id import UniqueId
6from kwai.modules.identity.user_recoveries.user_recovery_repository import (
7 UserRecoveryRepository,
8)
9from kwai.modules.identity.users.user_account_repository import UserAccountRepository
12class UserRecoveryExpiredException(Exception):
13 """Raised when the user recovery is expired."""
16class UserRecoveryConfirmedException(Exception):
17 """Raised when the user recovery was already used."""
20@dataclass(frozen=True, kw_only=True)
21class ResetPasswordCommand:
22 """Command for the reset password use case.
24 Attributes:
25 uuid: The unique id of the user recovery
26 password: The new password.
27 """
29 uuid: str
30 password: str
33class ResetPassword:
34 """Reset password use case.
36 This use case will try to reset the password of a user. A user can reset the
37 password with a unique id. This unique id is linked to a user recovery.
38 """
40 def __init__(
41 self,
42 user_account_repo: UserAccountRepository,
43 user_recovery_repo: UserRecoveryRepository,
44 ):
45 """Initialize the use case.
47 Args:
48 user_account_repo (UserAccountRepository): The repository for getting the
49 user account.
50 user_recovery_repo (UserRecoveryRepository): The repository for getting and
51 updating the user recovery.
52 """
53 self._user_account_repo = user_account_repo
54 self._user_recovery_repo = user_recovery_repo
56 async def execute(self, command: ResetPasswordCommand) -> None:
57 """Execute the use case.
59 Args:
60 command: The input for this use case.
62 Raises:
63 UserRecoveryNotFoundException: Raised when the user recovery with the
64 given uuid does not exist.
65 UserRecoveryExpiredException: Raised when the user recovery is expired.
66 UserRecoveryConfirmedException: Raised when the user recovery is already
67 used.
68 UserAccountNotFoundException: Raised when the user with the email address
69 that belongs to the user recovery, does not exist.
70 NotAllowedException: Raised when the user is revoked.
71 """
72 user_recovery = await self._user_recovery_repo.get_by_uuid(
73 UniqueId.create_from_string(command.uuid)
74 )
75 if user_recovery.is_expired:
76 raise UserRecoveryExpiredException()
77 if user_recovery.confirmed:
78 raise UserRecoveryConfirmedException()
80 user_account = await self._user_account_repo.get_user_by_email(
81 user_recovery.user.email
82 )
84 user_account.reset_password(Password.create_from_string(command.password))
85 await self._user_account_repo.update(user_account)
87 user_recovery.confirm()
88 await self._user_recovery_repo.update(user_recovery)