8000 Fixed some lint errors · rinlevan/firebase-admin-python@5c2e8da · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c2e8da

Browse files
committed
Fixed some lint errors
1 parent 5e848af commit 5c2e8da

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

firebase_admin/_auth_utils.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818
import re
1919

20+
import requests
2021
import six
2122
from six.moves import urllib
2223

@@ -215,22 +216,63 @@ def auth_error_code(self):
215216

216217
_ERROR_CODE_MAPPINGS = {
217218
'CLAIMS_TOO_LARGE': exceptions.INVALID_ARGUMENT,
219+
'INVALID_CONTINUE_URL': exceptions.INVALID_ARGUMENT,
220+
'INVALID_DYNAMIC_LINK_DOMAIN': exceptions.INVALID_ARGUMENT,
221+
'FORBIDDEN_CLAIM': exceptions.INVALID_ARGUMENT,
222+
'INVALID_CLAIMS': exceptions.INVALID_ARGUMENT,
223+
'INVALID_DURATION': exceptions.INVALID_ARGUMENT,
218224
'INVALID_EMAIL': exceptions.INVALID_ARGUMENT,
225+
'INVALID_ID_TOKEN': exceptions.INVALID_ARGUMENT,
226+
'INVALID_PAGE_SELECTION': exceptions.INVALID_ARGUMENT,
227+
'INVALID_PHONE_NUMBER': exceptions.INVALID_ARGUMENT,
228+
'MISSING_ANDROID_PACKAGE_NAME': exceptions.INVALID_ARGUMENT,
229+
'MISSING_IOS_BUNDLE_ID': exceptions.INVALID_ARGUMENT,
230+
'MISSING_LOCAL_ID': exceptions.INVALID_ARGUMENT,
231+
'MISSING_USER_ACCOUNT': exceptions.INVALID_ARGUMENT,
232+
'UNAUTHORIZED_DOMAIN': exceptions.INVALID_ARGUMENT,
233+
'WEAK_PASSWORD': exceptions.INVALID_ARGUMENT,
234+
219235
'INSUFFICIENT_PERMISSION': exceptions.PERMISSION_DENIED,
220236
'OPERATION_NOT_ALLOWED': exceptions.PERMISSION_DENIED,
221237
'PERMISSION_DENIED': exceptions.PERMISSION_DENIED,
238+
239+
'CONFIGURATION_NOT_FOUND': exceptions.NOT_FOUND,
240+
'PROJECT_NOT_FOUND': exceptions.NOT_FOUND,
222241
'USER_NOT_FOUND': exceptions.NOT_FOUND,
223-
'DUPLICATE_LOCAL_ID': exceptions.ALREADY_EXISTS,
242+
224243
'DUPLICATE_EMAIL': exceptions.ALREADY_EXISTS,
244+
'DUPLICATE_LOCAL_ID': exceptions.ALREADY_EXISTS,
245+
'EMAIL_EXISTS': exceptions.ALREADY_EXISTS,
225246
}
226247

227248

228249
def handle_http_error(msg, error):
229-
response_payload = {}
230-
if error.response is not None:
231-
response_payload = error.response.json()
232-
msg += '\n Server response: {0}'.format(error.response.content.decode())
233-
server_code = response_payload.get('error', {}).get('message')
234-
canonical_code = _ERROR_CODE_MAPPINGS.get(server_code, exceptions.UNKNOWN)
250+
"""Creates and raises a FirebaseAuthError from a requests exception."""
251+
if isinstance(error, requests.exceptions.Timeout):
252+
raise FirebaseAuthError(
253+
exceptions.DEADLINE_EXCEEDED,
254+
'{0}; Timed out while making an API call: {1}'.format(msg, error),
255+
cause=error)
256+
elif isinstance(error, requests.exceptions.ConnectionError):
257+
raise FirebaseAuthError(
258+
exceptions.UNAVAILABLE,
259+
'{0}; Network or service unavailable: {1}'.format(msg, error),
260+
cause=error)
261+
elif error.response is None:
262+
raise FirebaseAuthError(
263+
exceptions.UNKNOWN,
264+
'{0}; Unknown error while making remote service call: {1}'.format(msg, error),
265+
cause=error)
266+
267+
server_code, canonical_code = _get_error_codes(error)
268+
msg += '\n Server response: {0}'.format(error.response.content.decode())
235269
raise FirebaseAuthError(
236270
canonical_code, msg, cause=error, http_response=error.response, auth_error_code=server_code)
271+
272+
def _get_error_codes(error):
273+
response_payload = error.response.json()
274+
error_obj = response_payload.get('error', {})
275+
server_code = None
276+
if isinstance(error_obj, dict):
277+
server_code = error_obj.get('message')
278+
return server_code, _ERROR_CODE_MAPPINGS.get(server_code, exceptions.UNKNOWN)

tests/test_user_mgt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ def test_get_user_http_error(self, user_mgt_app):
237237
assert excinfo.value.http_response.status_code == 500
238238
assert error_payload in str(excinfo.value)
239239

240+
def test_get_user_malformed_http_error(self, user_mgt_app):
241+
error_payload = '{"error": "INSUFFICIENT_PERMISSION"}'
242+
_instrument_user_manager(user_mgt_app, 500, error_payload)
243+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
244+
auth.get_user('testuser', user_mgt_app)
245+
assert excinfo.value.code == exceptions.UNKNOWN
246+
assert excinfo.value.auth_error_code is None
247+
assert isinstance(excinfo.value.cause, requests.exceptions.RequestException)
248+
assert excinfo.value.http_response.status_code == 500
249+
assert error_payload in str(excinfo.value)
250+
240251
def test_get_user_by_email_http_error(self, user_mgt_app):
241252
_instrument_user_manager(user_mgt_app, 500, MOCK_ERROR_RESPONSE)
242253
with pytest.raises(auth.FirebaseAuthError) as excinfo:

0 commit comments

Comments
 (0)
0