Skip to content

feat(auth): Added InsufficientPermissionError type #354

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 2 commits into from
Oct 10, 2019
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
13 changes: 13 additions & 0 deletions firebase_admin/_auth_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ def __init__(self, message, cause, http_response):
exceptions.AlreadyExistsError.__init__(self, message, cause, http_response)


class InsufficientPermissionError(exceptions.PermissionDeniedError):
"""The credential used to initialize the SDK lacks required permissions."""

default_message = ('The credential used to initialize the SDK has insufficient '
'permissions to perform the requested operation. See '
'https://firebase.google.com/docs/admin/setup for details '
'on how to initialize the Admin SDK with appropriate permissions')

def __init__(self, message, cause, http_response):
exceptions.PermissionDeniedError.__init__(self, message, cause, http_response)


class InvalidDynamicLinkDomainError(exceptions.InvalidArgumentError):
"""Dynamic link domain in ActionCodeSettings is not authorized."""

Expand Down Expand Up @@ -258,6 +270,7 @@ def __init__(self, message, cause=None, http_response=None):
'DUPLICATE_EMAIL': EmailAlreadyExistsError,
'DUPLICATE_LOCAL_ID': UidAlreadyExistsError,
'EMAIL_EXISTS': EmailAlreadyExistsError,
'INSUFFICIENT_PERMISSION': InsufficientPermissionError,
'INVALID_DYNAMIC_LINK_DOMAIN': InvalidDynamicLinkDomainError,
'INVALID_ID_TOKEN': InvalidIdTokenError,
'PHONE_NUMBER_EXISTS': PhoneNumberAlreadyExistsError,
Expand Down
2 changes: 2 additions & 0 deletions firebase_admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'ExpiredSessionCookieError',
'ExportedUserRecord',
'ImportUserRecord',
'InsufficientPermissionError',
'InvalidDynamicLinkDomainError',
'InvalidIdTokenError',
'InvalidSessionCookieError',
Expand Down Expand Up @@ -89,6 +90,7 @@
ExpiredSessionCookieError = _token_gen.ExpiredSessionCookieError
ExportedUserRecord = _user_mgt.ExportedUserRecord
ImportUserRecord = _user_import.ImportUserRecord
InsufficientPermissionError = _auth_utils.InsufficientPermissionError
InvalidDynamicLinkDomainError = _auth_utils.InvalidDynamicLinkDomainError
InvalidIdTokenError = _auth_utils.InvalidIdTokenError
InvalidSessionCookieError = _token_gen.InvalidSessionCookieError
Expand Down
15 changes: 15 additions & 0 deletions tests/test_user_mgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,21 @@ def test_list_users_error(self, user_mgt_app):
auth.list_users(app=user_mgt_app)
assert str(excinfo.value) == 'Unexpected error response: {"error":"test"}'

def test_permission_error(self, user_mgt_app):
_instrument_user_manager(
user_mgt_app, 400, '{"error": {"message": "INSUFFICIENT_PERMISSION"}}')
with pytest.raises(auth.InsufficientPermissionError) as excinfo:
auth.list_users(app=user_mgt_app)
assert isinstance(excinfo.value, exceptions.PermissionDeniedError)
msg = ('The credential used to initialize the SDK has insufficient '
'permissions to perform the requested operation. See '
'https://firebase.google.com/docs/admin/setup for details '
'on how to initialize the Admin SDK with appropriate permissions '
'(INSUFFICIENT_PERMISSION).')
assert str(excinfo.value) == msg
assert excinfo.value.http_response is not None
assert excinfo.value.cause is not None

def _check_page(self, page):
assert isinstance(page, auth.ListUsersPage)
index = 0
Expand Down