Skip to content

Commit 527a824

Browse files
fix(auth): adds missing EMAIL_NOT_FOUND error code (#550)
* fix(auth): adds missing EMAIL_NOT_FOUND error code Catch EMAIL_NOT_FOUND and translate to EmailNotFoundError when /accounts:sendOobCode is called for password reset on a user that does not exist.
1 parent ebf0961 commit 527a824

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

firebase_admin/_auth_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def get_user_by_email(self, email):
181181
182182
Raises:
183183
ValueError: If the email is None, empty or malformed.
184-
UserNotFoundError: If no user exists by the specified email address.
184+
UserNotFoundError: If no user exists for the specified email address.
185185
FirebaseError: If an error occurs while retrieving the user.
186186
"""
187187
response = self._user_manager.get_user(email=email)
@@ -198,7 +198,7 @@ def get_user_by_phone_number(self, phone_number):
198198
199199
Raises:
200200
ValueError: If the phone number is ``None``, empty or malformed.
201-
UserNotFoundError: If no user exists by the specified phone number.
201+
UserNotFoundError: If no user exists for the specified phone number.
202202
FirebaseError: If an error occurs while retrieving the user.
203203
"""
204204
response = self._user_manager.get_user(phone_number=phone_number)
@@ -444,6 +444,7 @@ def generate_password_reset_link(self, email, action_code_settings=None):
444444
445445
Raises:
446446
ValueError: If the provided arguments are invalid
447+
EmailNotFoundError: If no user exists for the specified email address.
447448
FirebaseError: If an error occurs while generating the link
448449
"""
449450
return self._user_manager.generate_email_action_link(
@@ -464,6 +465,7 @@ def generate_email_verification_link(self, email, action_code_settings=None):
464465
465466
Raises:
466467
ValueError: If the provided arguments are invalid
468+
UserNotFoundError: If no user exists for the specified email address.
467469
FirebaseError: If an error occurs while generating the link
468470
"""
469471
return self._user_manager.generate_email_action_link(

firebase_admin/_auth_utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ def __init__(self, message, cause=None, http_response=None):
351351
exceptions.NotFoundError.__init__(self, message, cause, http_response)
352352

353353

354+
class EmailNotFoundError(exceptions.NotFoundError):
355+
"""No user record found for the specified email."""
356+
357+
default_message = 'No user record found for the given email'
358+
359+
def __init__(self, message, cause=None, http_response=None):
360+
exceptions.NotFoundError.__init__(self, message, cause, http_response)
361+
362+
354363
class TenantNotFoundError(exceptions.NotFoundError):
355364
"""No tenant found for the specified identifier."""
356365

@@ -381,6 +390,7 @@ def __init__(self, message, cause=None, http_response=None):
381390
'DUPLICATE_EMAIL': EmailAlreadyExistsError,
382391
'DUPLICATE_LOCAL_ID': UidAlreadyExistsError,
383392
'EMAIL_EXISTS': EmailAlreadyExistsError,
393+
'EMAIL_NOT_FOUND': EmailNotFoundError,
384394
'INSUFFICIENT_PERMISSION': InsufficientPermissionError,
385395
'INVALID_DYNAMIC_LINK_DOMAIN': InvalidDynamicLinkDomainError,
386396
'INVALID_ID_TOKEN': InvalidIdTokenError,

firebase_admin/auth.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'ConfigurationNotFoundError',
4040
'DELETE_ATTRIBUTE',
4141
'EmailAlreadyExistsError',
42+
'EmailNotFoundError',
4243
'ErrorInfo',
4344
'ExpiredIdTokenError',
4445
'ExpiredSessionCookieError',
@@ -112,6 +113,7 @@
112113
DELETE_ATTRIBUTE = _user_mgt.DELETE_ATTRIBUTE
113114
DeleteUsersResult = _user_mgt.DeleteUsersResult
114115
EmailAlreadyExistsError = _auth_utils.EmailAlreadyExistsError
116+
EmailNotFoundError = _auth_utils.EmailNotFoundError
115117
ErrorInfo = _user_import.ErrorInfo
116118
ExpiredIdTokenError = _token_gen.ExpiredIdTokenError
117119
ExpiredSessionCookieError = _token_gen.ExpiredSessionCookieError

tests/test_user_mgt.py

+11
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,17 @@ def test_api_call_failure(self, user_mgt_app, func):
14461446
assert excinfo.value.http_response is not None
14471447
assert excinfo.value.cause is not None
14481448

1449+
def test_password_reset_non_existing(self, user_mgt_app):
1450+
_instrument_user_manager(user_mgt_app, 400, '{"error":{"message": "EMAIL_NOT_FOUND"}}')
1451+
with pytest.raises(auth.EmailNotFoundError) as excinfo:
1452+
auth.generate_password_reset_link(
1453+
'nonexistent@user', MOCK_ACTION_CODE_SETTINGS, app=user_mgt_app)
1454+
error_msg = 'No user record found for the given email (EMAIL_NOT_FOUND).'
1455+
assert excinfo.value.code == exceptions.NOT_FOUND
1456+
assert str(excinfo.value) == error_msg
1457+
assert excinfo.value.http_response is not None
1458+
assert excinfo.value.cause is not None
1459+
14491460
@pytest.mark.parametrize('func', [
14501461
auth.generate_sign_in_with_email_link,
14511462
auth.generate_email_verification_link,

0 commit comments

Comments
 (0)