8000 Fixed all lint errors · ZachOrr/firebase-admin-python@216a3ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 216a3ce

Browse files
committed
Fixed all lint errors
1 parent 1c093e2 commit 216a3ce

File tree

7 files changed

+57
-46
lines changed

7 files changed

+57
-46
lines changed

firebase_admin/_auth_utils.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,15 @@ def auth_error_code(self):
205205

206206

207207
_ERROR_CODE_MAPPINGS = {
208-
'CLAIMS_TOO_LARGE': exceptions.INVALID_ARGUMENT,
209-
'INVALID_EMAIL': exceptions.INVALID_ARGUMENT,
210-
'INSUFFICIENT_PERMISSION': exceptions.PERMISSION_DENIED,
211-
'OPERATION_NOT_ALLOWED': exceptions.PERMISSION_DENIED,
212-
'PERMISSION_DENIED': exceptions.PERMISSION_DENIED,
213-
'USER_NOT_FOUND': exceptions.NOT_FOUND,
214-
'DUPLICATE_EMAIL': exceptions.ALREADY_EXISTS,
215-
}
208+
'CLAIMS_TOO_LARGE': exceptions.INVALID_ARGUMENT,
209+
'INVALID_EMAIL': exceptions.INVALID_ARGUMENT,
210+
'INSUFFICIENT_PERMISSION': exceptions.PERMISSION_DENIED,
211+
'OPERATION_NOT_ALLOWED': exceptions.PERMISSION_DENIED,
212+
'PERMISSION_DENIED': exceptions.PERMISSION_DENIED,
213+
'USER_NOT_FOUND': exceptions.NOT_FOUND,
214+
'DUPLICATE_LOCAL_ID': exceptions.ALREADY_EXISTS,
215+
'DUPLICATE_EMAIL': exceptions.ALREADY_EXISTS,
216+
}
216217

217218

218219
def handle_http_error(msg, error):

firebase_admin/_token_gen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def create_session_cookie(self, id_token, expires_in):
203203
'validDuration': expires_in,
204204
}
205205
try:
206-
body, response = self.client.body_and_response('post', ':createSessionCookie', json=payload)
206+
body, response = self.client.body_and_response(
207+
'post', ':createSessionCookie', json=payload)
207208
except requests.exceptions.RequestException as error:
208209
_auth_utils.handle_http_error('Failed to create session cookie', error)
209210
else:
@@ -340,4 +341,3 @@ def verify(self, token, request):
340341
'Failed to fetch public key certificates. {0}'.format(error),
341342
cause=error,
342343
auth_error_code=CERTIFICATE_FETCH_FAILED)
343-

firebase_admin/_user_mgt.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ def get_user(self, **kwargs):
466466
raise TypeError('Unsupported keyword arguments: {0}.'.format(kwargs))
467467

468468
try:
469-
body, response = self._client.body_and_response('post', '/accounts:lookup', json=payload)
469+
body, response = self._client.body_and_response(
470+
'post', '/accounts:lookup', json=payload)
470471
except requests.exceptions.RequestException as error:
471472
msg = 'Failed to get user by {0}: {1}.'.format(key_type, key)
472473
_auth_utils.handle_http_error(msg, error)
@@ -568,7 +569,8 @@ def update_user(self, uid, display_name=_UNSPECIFIED, email=None, phone_number=_
568569

569570
payload = {k: v for k, v in payload.items() if v is not None}
570571
try:
571-
body, response = self._client.body_and_response('post', '/accounts:update', json=payload)
572+
body, response = self._client.body_and_response(
573+
'post', '/accounts:update', json=payload)
572574
except requests.exceptions.RequestException as error:
573575
_auth_utils.handle_http_error('Failed to update user: {0}.'.format(uid), error)
574576
else:
@@ -584,7 +586,8 @@ def delete_user(self, uid):
584586
"""Deletes the user identified by the specified user ID."""
585587
_auth_utils.validate_uid(uid, required=True)
586588
try:
587-
body, response = self._client.body_and_response('post', '/accounts:delete', json={'localId' : uid})
589+
body, response = self._client.body_and_response(
590+
'post', '/accounts:delete', json={'localId' : uid})
588591
except requests.exceptions.RequestException as error:
589592
_auth_utils.handle_http_error('Failed to delete user: {0}.'.format(uid), error)
590593
else:
@@ -613,7 +616,8 @@ def import_users(self, users, hash_alg=None):
613616
raise ValueError('A UserImportHash is required to import users with passwords.')
614617
payload.update(hash_alg.to_dict())
615618
try:
616-
body, response = self._client.body_and_response('post', '/accounts:batchCreate', json=payload)
619+
body, response = self._client.body_and_response(
620+
'post', '/accounts:batchCreate', json=payload)
617621
except requests.exceptions.RequestException as error:
618622
_auth_utils.handle_http_error('Failed to import users.', error)
619623
else:
@@ -651,7 +655,8 @@ def generate_email_action_link(self, action_type, email, action_code_settings=No
651655
payload.update(encode_action_code_settings(action_code_settings))
652656

653657
try:
654-
body, response = self._client.body_and_response('post', '/accounts:sendOobCode', json=payload)
658+
body, response = self._client.body_and_response(
659+
'post', '/accounts:sendOobCode', json=payload)
655660
except requests.exceptions.RequestException as error:
656661
_auth_utils.handle_http_error('Failed to generate link.', error)
657662
else:

firebase_admin/auth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
__all__ = [
4242
'ActionCodeSettings',
43-
'AuthError',
4443
'ErrorInfo',
4544
'ExportedUserRecord',
4645
'FirebaseAuthError',

firebase_admin/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# limitations under the License.
1414

1515

16+
"""Firebase exceptions module.
17+
18+
Defines the top-level error codes and exception base classes.
19+
"""
20+
1621
ALREADY_EXISTS = 'ALREADY_EXISTS'
1722
INVALID_ARGUMENT = 'INVALID_ARGUMENT'
1823
FAILED_PRECONDITION = 'FAILED_PRECONDITION'
@@ -26,13 +31,13 @@
2631

2732

2833
class FirebaseError(Exception):
34+
"""Base class for all errors raised by the Admin SDK."""
2935

3036
def __init__(self, code, message, cause=None, http_response=None):
3137
Exception.__init__(self, message)
3238
self._code = code
3339
self._cause = cause
3440
self._http_response = http_response
35-
print('CONST', self._http_response)
3641

3742
@property
3843
def code(self):

integration/test_auth.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import firebase_admin
2727
from firebase_admin import auth
2828
from firebase_admin import credentials
29+
from firebase_admin import exceptions
2930
import google.oauth2.credentials
3031
from google.auth import transport
3132

@@ -130,24 +131,24 @@ def test_session_cookies(api_key):
130131
assert abs(claims['exp'] - estimated_exp) < 5
131132

132133
def test_get_non_existing_user():
133-
with pytest.raises(auth.AuthError) as excinfo:
134+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
134135
auth.get_user('non.existing')
135-
assert 'USER_NOT_FOUND_ERROR' in str(excinfo.value.code)
136+
assert excinfo.value.code == exceptions.NOT_FOUND
136137

137138
def test_get_non_existing_user_by_email():
138-
with pytest.raises(auth.AuthError) as excinfo:
139+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
139140
auth.get_user_by_email('non.existing@definitely.non.existing')
140-
assert 'USER_NOT_FOUND_ERROR' in str(excinfo.value.code)
141+
assert excinfo.value.code == exceptions.NOT_FOUND
141142

142143
def test_update_non_existing_user():
143-
with pytest.raises(auth.AuthError) as excinfo:
144+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
144145
auth.update_user('non.existing')
145-
assert 'USER_UPDATE_ERROR' in str(excinfo.value.code)
146+
assert excinfo.value.code == exceptions.NOT_FOUND
146147

147148
def test_delete_non_existing_user():
148-
with pytest.raises(auth.AuthError) as excinfo:
149+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
149150
auth.delete_user('non.existing')
150-
assert 'USER_DELETE_ERROR' in str(excinfo.value.code)
151+
assert excinfo.value.code == exceptions.NOT_FOUND
151152

152153
@pytest.fixture
153154
def new_user():
@@ -250,9 +251,9 @@ def test_create_user(new_user):
250251
assert user.user_metadata.creation_timestamp > 0
251252
assert user.user_metadata.last_sign_in_timestamp is None
252253
assert len(user.provider_data) is 0
253-
with pytest.raises(auth.AuthError) as excinfo:
254+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
254255
auth.create_user(uid=new_user.uid)
255-
assert excinfo.value.code == 'USER_CREATE_ERROR'
256+
assert excinfo.value.code == exceptions.ALREADY_EXISTS
256257

257258
def test_update_user(new_user):
258259
_, email = _random_id()
@@ -321,9 +322,9 @@ def test_disable_user(new_user_with_params):
321322
def test_delete_user():
322323
user = auth.create_user()
323324
auth.delete_user(user.uid)
324-
with pytest.raises(auth.AuthError) as excinfo:
325+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
325326
auth.get_user(user.uid)
326-
assert excinfo.value.code == 'USER_NOT_FOUND_ERROR'
327+
assert excinfo.value.code == exceptions.NOT_FOUND
327328

328329
def test_revoke_refresh_tokens(new_user):
329330
user = auth.get_user(new_user.uid)
@@ -347,9 +348,9 @@ def test_verify_id_token_revoked(new_user, api_key):
347348
# verify_id_token succeeded because it didn't check revoked.
348349
assert claims['iat'] * 1000 < user.tokens_valid_after_timestamp
349350

350-
with pytest.raises(auth.AuthError) as excinfo:
351+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
351352
claims = auth.verify_id_token(id_token, check_revoked=True)
352-
assert excinfo.value.code == auth._ID_TOKEN_REVOKED
353+
assert excinfo.value.auth_error_code == auth.ID_TOKEN_REVOKED
353354
assert str(excinfo.value) == 'The Firebase ID token has been revoked.'
354355

355356
# Sign in again, verify works.
@@ -369,9 +370,9 @@ def test_verify_session_cookie_revoked(new_user, api_key):
369370
# verify_session_cookie succeeded because it didn't check revoked.
370371
assert claims['iat'] * 1000 < user.tokens_valid_after_timestamp
371372

372-
with pytest.raises(auth.AuthError) as excinfo:
373+
with pytest.raises(auth.FirebaseAuthError) as excinfo:
373374
claims = auth.verify_session_cookie(session_cookie, check_revoked=True)
374-
assert excinfo.value.code == auth._SESSION_COOKIE_REVOKED
375+
assert excinfo.value.auth_error_code == auth.SESSION_COOKIE_REVOKED
375376
assert str(excinfo.value) == 'The Firebase session cookie has been revoked.'
376377

377378
# Sign in again, verify works.

snippets/auth/index.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def verify_token_uid_check_revoke(id_token):
144144
decoded_token = auth.verify_id_token(id_token, check_revoked=True)
145145
# Token is valid and not revoked.
146146
uid = decoded_token['uid']
147-
except auth.AuthError as exc:
148-
if exc.code == 'ID_TOKEN_REVOKED':
147+
except auth.FirebaseAuthError as exc:
148+
if exc.auth_error_code == 'ID_TOKEN_REVOKED':
149149
# Token revoked, inform the user to reauthenticate or signOut().
150150
pass
151151
else:
@@ -322,7 +322,7 @@ def session_login():
322322
response.set_cookie(
323323
'session', session_cookie, expires=expires, httponly=True, secure=True)
324324
return response
325-
except auth.AuthError:
325+
except auth.FirebaseAuthError:
326326
return flask.abort(401, 'Failed to create a session cookie')
327327
# [END session_login]
328328

@@ -346,7 +346,7 @@ def check_auth_time(id_token, flask):
346346
return flask.abort(401, 'Recent sign in required')
347347
except ValueError:
348348
return flask.abort(401, 'Invalid ID token')
349-
except auth.AuthError:
349+
except auth.FirebaseAuthError:
350350
return flask.abort(401, 'Failed to create a session cookie')
351351
# [END check_auth_time]
352352

@@ -367,7 +367,7 @@ def access_restricted_content():
367367
except ValueError:
368368
# Session cookie is unavailable or invalid. Force user to login.
369369
return flask.redirect('/login')
370-
except auth.AuthError:
370+
except auth.FirebaseAuthError:
371371
# Session revoked. Force user to login.
372372
return flask.redirect('/login')
373373
# [END session_verify]
@@ -388,7 +388,7 @@ def serve_content_for_admin(decoded_claims):
388388
except ValueError:
389389
# Session cookie is unavailable or invalid. Force user to login.
390390
return flask.redirect('/login')
391-
except auth.AuthError:
391+
except auth.FirebaseAuthError:
392392
# Session revoked. Force user to login.
393393
return flask.redirect('/login')
394394
# [END session_verify_with_permission_check]
@@ -444,7 +444,7 @@ def import_users():
444444
result.success_count, result.failure_count))
445445
for err in result.errors:
446446
print('Failed to import {0} due to {1}'.format(users[err.index].uid, err.reason))
447-
except auth.AuthError:
447+
except auth.FirebaseAuthError:
448448
# Some unrecoverable error occurred that prevented the operation from running.
449449
pass
450450
# [END import_users]
@@ -465,7 +465,7 @@ def import_with_hmac():
465465
result = auth.import_users(users, hash_alg=hash_alg)
466466
for err in result.errors:
467467
print('Failed to import user:', err.reason)
468-
except auth.AuthError as error:
468+
except auth.FirebaseAuthError as error:
469469
print('Error importing users:', error)
470470
# [END import_with_hmac]
471471

@@ -485,7 +485,7 @@ def import_with_pbkdf():
485485
result = auth.import_users(users, hash_alg=hash_alg)
486486
for err in result.errors:
487487
print('Failed to import user:', err.reason)
488-
except auth.AuthError as error:
488+
except auth.FirebaseAuthError as error:
489489
print('Error importing users:', error)
490490
# [END import_with_pbkdf]
491491

@@ -506,7 +506,7 @@ def import_with_standard_scrypt():
506506
result = auth.import_users(users, hash_alg=hash_alg)
507507
for err in result.errors:
508508
print('Failed to import user:', err.reason)
509-
except auth.AuthError as error:
509+
except auth.FirebaseAuthError as error:
510510
print('Error importing users:', error)
511511
# [END import_with_standard_scrypt]
512512

@@ -526,7 +526,7 @@ def import_with_bcrypt():
526526
result = auth.import_users(users, hash_alg=hash_alg)
527527
for err in result.errors:
528528
print('Failed to import user:', err.reason)
529-
except auth.AuthError as error:
529+
except auth.FirebaseAuthError as error:
530530
print('Error importing users:', error)
531531
# [END import_with_bcrypt]
532532

@@ -553,7 +553,7 @@ def import_with_scrypt():
553553
result = auth.import_users(users, hash_alg=hash_alg)
554554
for err in result.errors:
555555
print('Failed to import user:', err.reason)
556-
except auth.AuthError as error:
556+
except auth.FirebaseAuthError as error:
557557
print('Error importing users:', error)
558558
# [END import_with_scrypt]
559559

@@ -583,7 +583,7 @@ def import_without_password():
583583
result = auth.import_users(users)
584584
for err in result.errors:
585585
print('Failed to import user:', err.reason)
586-
except auth.AuthError as error:
586+
except auth.FirebaseAuthError as error:
587587
print('Error importing users:', error)
588588
# [END import_without_password]
589589

0 commit comments

Comments
 (0)
0