Skip to content

fix(auth): adds missing EMAIL_NOT_FOUND error code #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions firebase_admin/_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def get_user_by_email(self, email):

Raises:
ValueError: If the email is None, empty or malformed.
UserNotFoundError: If no user exists by the specified email address.
UserNotFoundError: If no user exists for the specified email address.
FirebaseError: If an error occurs while retrieving the user.
"""
response = self._user_manager.get_user(email=email)
Expand All @@ -198,7 +198,7 @@ def get_user_by_phone_number(self, phone_number):

Raises:
ValueError: If the phone number is ``None``, empty or malformed.
UserNotFoundError: If no user exists by the specified phone number.
UserNotFoundError: If no user exists for the specified phone number.
FirebaseError: If an error occurs while retrieving the user.
"""
response = self._user_manager.get_user(phone_number=phone_number)
Expand Down Expand Up @@ -444,6 +444,7 @@ def generate_password_reset_link(self, email, action_code_settings=None):

Raises:
ValueError: If the provided arguments are invalid
EmailNotFoundError: If no user exists for the specified email address.
FirebaseError: If an error occurs while generating the link
"""
return self._user_manager.generate_email_action_link(
Expand All @@ -464,6 +465,7 @@ def generate_email_verification_link(self, email, action_code_settings=None):

Raises:
ValueError: If the provided arguments are invalid
UserNotFoundError: If no user exists for the specified email address.
FirebaseError: If an error occurs while generating the link
"""
return self._user_manager.generate_email_action_link(
Expand Down
10 changes: 10 additions & 0 deletions firebase_admin/_auth_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ def __init__(self, message, cause=None, http_response=None):
exceptions.NotFoundError.__init__(self, message, cause, http_response)


class EmailNotFoundError(exceptions.NotFoundError):
"""No user record found for the specified email."""

default_message = 'No user record found for the given email'

def __init__(self, message, cause=None, http_response=None):
exceptions.NotFoundError.__init__(self, message, cause, http_response)


class TenantNotFoundError(exceptions.NotFoundError):
"""No tenant found for the specified identifier."""

Expand Down Expand Up @@ -381,6 +390,7 @@ def __init__(self, message, cause=None, http_response=None):
'DUPLICATE_EMAIL': EmailAlreadyExistsError,
'DUPLICATE_LOCAL_ID': UidAlreadyExistsError,
'EMAIL_EXISTS': EmailAlreadyExistsError,
'EMAIL_NOT_FOUND': EmailNotFoundError,
'INSUFFICIENT_PERMISSION': InsufficientPermissionError,
'INVALID_DYNAMIC_LINK_DOMAIN': InvalidDynamicLinkDomainError,
'INVALID_ID_TOKEN': InvalidIdTokenError,
Expand Down
2 changes: 2 additions & 0 deletions firebase_admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'ConfigurationNotFoundError',
'DELETE_ATTRIBUTE',
'EmailAlreadyExistsError',
'EmailNotFoundError',
'ErrorInfo',
'ExpiredIdTokenError',
'ExpiredSessionCookieError',
Expand Down Expand Up @@ -112,6 +113,7 @@
DELETE_ATTRIBUTE = _user_mgt.DELETE_ATTRIBUTE
DeleteUsersResult = _user_mgt.DeleteUsersResult
EmailAlreadyExistsError = _auth_utils.EmailAlreadyExistsError
EmailNotFoundError = _auth_utils.EmailNotFoundError
ErrorInfo = _user_import.ErrorInfo
ExpiredIdTokenError = _token_gen.ExpiredIdTokenError
ExpiredSessionCookieError = _token_gen.ExpiredSessionCookieError
Expand Down
11 changes: 11 additions & 0 deletions tests/test_user_mgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,17 @@ def test_api_call_failure(self, user_mgt_app, func):
assert excinfo.value.http_response is not None
assert excinfo.value.cause is not None

def test_password_reset_non_existing(self, user_mgt_app):
_instrument_user_manager(user_mgt_app, 400, '{"error":{"message": "EMAIL_NOT_FOUND"}}')
with pytest.raises(auth.EmailNotFoundError) as excinfo:
auth.generate_password_reset_link(
'nonexistent@user', MOCK_ACTION_CODE_SETTINGS, app=user_mgt_app)
error_msg = 'No user record found for the given email (EMAIL_NOT_FOUND).'
assert excinfo.value.code == exceptions.NOT_FOUND
assert str(excinfo.value) == error_msg
assert excinfo.value.http_response is not None
assert excinfo.value.cause is not None

@pytest.mark.parametrize('func', [
auth.generate_sign_in_with_email_link,
auth.generate_email_verification_link,
Expand Down