diff --git a/alice/onboarding/onboarding.py b/alice/onboarding/onboarding.py index d83aa70..3778d69 100644 --- a/alice/onboarding/onboarding.py +++ b/alice/onboarding/onboarding.py @@ -1678,6 +1678,40 @@ def get_duplicates_searches( ) ) + def get_user_state( + self, + user_id: str, + verbose: bool = False, + ) -> Result[UserState, OnboardingError]: + """ + Retrieves the state of a user + Parameters + ---------- + user_id + User identifier + + verbose + Used for print service response as well as the time elapsed + Returns + ------- + A Result where if the operation is successful it returns the user state. + Otherwise, it returns an OnboardingError. + """ + verbose = self.verbose or verbose + response = self.onboarding_client.get_user_state( + user_id=user_id, + verbose=verbose, + ).unwrap_or_return() + + if response.status_code == 200: + return Success(UserState(response.json())) + else: + return Failure( + OnboardingError.from_response( + operation="get_user_state", response=response + ) + ) + def update_user_state( self, user_id: str, diff --git a/alice/onboarding/onboarding_client.py b/alice/onboarding/onboarding_client.py index 3ba8954..52e5d20 100644 --- a/alice/onboarding/onboarding_client.py +++ b/alice/onboarding/onboarding_client.py @@ -1920,6 +1920,48 @@ def get_duplicates_searches(self, verbose: bool = False) -> Result[Response, Err return Success(response) + @timeit + def get_user_state( + self, + user_id: str, + verbose: bool = False, + ) -> Result[Response, Error]: + """ + Retrieves the state of a user + + Parameters + ---------- + user_id + User identifier + verbose + Used for print service response as well as the time elapsed + + + Returns + ------- + A Response object [requests library] + """ + print_intro("get_user_state", verbose=verbose) + + backend_user_token = self.auth.create_backend_token( + user_id=user_id + ).unwrap_or_return() + print_token("backend_token_with_user", backend_user_token, verbose=verbose) + + headers = self._auth_headers(backend_user_token) + try: + response = requests.get( + f"{self.url}/user/state", + headers=headers, + timeout=self.timeout, + ) + except requests.exceptions.Timeout: + return Failure(OnboardingError.timeout(operation="get_user_state")) + + print_response(response=response, verbose=verbose) + + return Success(response) + @timeit def update_user_state( self, diff --git a/tests/test_integration_flows.py b/tests/test_integration_flows.py index d268f35..b318755 100644 --- a/tests/test_integration_flows.py +++ b/tests/test_integration_flows.py @@ -49,7 +49,7 @@ def do_complete_flow() -> Result[bool, Error]: flow_id = onboarding.create_flow( steps=[OnboardingSteps.SELFIE], - default=True, + default=False, name="alice-flow-test-onboarding-python", ).unwrap_or_return() diff --git a/tests/test_integration_onboarding.py b/tests/test_integration_onboarding.py index 875e48c..6701982 100644 --- a/tests/test_integration_onboarding.py +++ b/tests/test_integration_onboarding.py @@ -90,22 +90,25 @@ def do_complete_onboarding() -> Result[dict, Error]: user_id=user_id ).unwrap_or_return() - _ = onboarding.retrieve_certificate( + onboarding.retrieve_certificate( user_id=user_id, certificate_id=certificate_id ).unwrap_or_return() - _ = onboarding.retrieve_certificates(user_id=user_id).unwrap_or_return() + onboarding.retrieve_certificates(user_id=user_id).unwrap_or_return() - _ = onboarding.update_user_state( + onboarding.update_user_state( user_id=user_id, user_state=UserState.TO_REVIEW ).unwrap_or_return() - _ = onboarding.update_user_state( + onboarding.update_user_state( user_id=user_id, user_state=UserState.ACCEPTED ).unwrap_or_return() - _ = onboarding.update_user_state( + onboarding.update_user_state( user_id=user_id, user_state=UserState.REJECTED ).unwrap_or_return() + user_state = onboarding.get_user_state(user_id=user_id).unwrap_or_return() + assert user_state == UserState.REJECTED + onboarding.delete_user(user_id).unwrap_or_return() return Success(report)