Skip to content

Commit

Permalink
Chore/users-get-all-org-does-not-exist-custom-error (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliya Smith committed Apr 23, 2021
1 parent 110c8fb commit 0fdab8f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
- Issue when calling `sdk.alerts.update_state()` without specifying a `note` parameter
would set the existing alert's note's message to the empty string.

### Added

- Custom exception `Py42OrgNotFoundError`.

### Changed

- `sdk.users.get_all()` now raises `Py42OrgNotFoundError` when the given `org_uid`
was not found.

- `sdk.users.get_page()` now raises `Py42OrgNotFoundError` when the given `org_uid`
was not found.

## 1.14.0 - 2021-04-20

### Added
Expand Down
9 changes: 9 additions & 0 deletions src/py42/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ class Py42TooManyRequestsError(Py42HTTPError):
"""A wrapper to represent an HTTP 429 error."""


class Py42OrgNotFoundError(Py42BadRequestError):
"""An exception raised when a 400 HTTP error message indicates that an
organization was not found."""

def __init__(self, exception, org_uid):
msg = u"The organization with UID '{}' was not found.".format(org_uid)
super(Py42OrgNotFoundError, self).__init__(exception, msg)


class Py42ActiveLegalHoldError(Py42BadRequestError):
"""An exception raised when attempting to deactivate a user or device that is in an
active legal hold."""
Expand Down
9 changes: 7 additions & 2 deletions src/py42/services/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from py42._compat import quote
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42InternalServerError
from py42.exceptions import Py42OrgNotFoundError
from py42.exceptions import Py42UserAlreadyExistsError
from py42.exceptions import Py42UsernameMustBeEmailError
from py42.services import BaseService
Expand Down Expand Up @@ -151,8 +152,12 @@ def get_page(
q=q,
**kwargs
)

return self._connection.get(uri, params=params)
try:
return self._connection.get(uri, params=params)
except Py42BadRequestError as err:
if u"Organization was not found" in str(err.response.text):
raise Py42OrgNotFoundError(err, org_uid)
raise

def get_all(
self, active=None, email=None, org_uid=None, role_id=None, q=None, **kwargs
Expand Down
37 changes: 35 additions & 2 deletions tests/services/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from py42.exceptions import Py42ActiveLegalHoldError
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42InternalServerError
from py42.exceptions import Py42OrgNotFoundError
from py42.exceptions import Py42UserAlreadyExistsError
from py42.exceptions import Py42UsernameMustBeEmailError
from py42.response import Py42Response
Expand All @@ -23,8 +24,8 @@
"pgSize": 500,
"q": None,
}
MOCK_GET_USER_RESPONSE = """{"totalCount": 3000, "users": ["foo"]}"""
MOCK_EMPTY_GET_USER_RESPONSE = """{"totalCount": 3000, "users": []}"""
MOCK_GET_USER_RESPONSE = '{"totalCount": 3000, "users": ["foo"]}'
MOCK_EMPTY_GET_USER_RESPONSE = '{"totalCount": 3000, "users": []}'
MOCK_text = '{"item_list_key": [{"foo": "foo_val"}, {"bar": "bar_val"}]}'
MOCK_USER_DUPLICATE_ERROR_TEXT = '{"body": "USER_DUPLICATE"}'
MOCK_USERNAME_MUST_BE_EMAIL_TEXT = '{"data": [{"name": "USERNAME_NOT_AN_EMAIL"}]}'
Expand Down Expand Up @@ -224,6 +225,38 @@ def test_get_page_calls_get_with_expected_url_and_params(self, mock_connection):
},
)

def test_get_page_when_org_not_found_raises_expected_error(
self, mocker, mock_connection
):
def side_effect(*args, **kwargs):
base_err = mocker.MagicMock(spec=HTTPError)
base_err.response = mocker.MagicMock(spec=Response)
base_err.response.text = (
'[{"name":"SYSTEM","description":"Organization was not found"}]'
)
raise Py42BadRequestError(base_err)

mock_connection.get.side_effect = side_effect
service = UserService(mock_connection)

with pytest.raises(Py42OrgNotFoundError) as err:
service.get_page(1, org_uid="TestOrgUid")

assert str(err.value) == "The organization with UID 'TestOrgUid' was not found."

def test_get_page_when_bad_request_raises(self, mocker, mock_connection):
def side_effect(*args, **kwargs):
base_err = mocker.MagicMock(spec=HTTPError)
base_err.response = mocker.MagicMock(spec=Response)
base_err.response.text = "BAD REQUEST"
raise Py42BadRequestError(base_err)

mock_connection.get.side_effect = side_effect
service = UserService(mock_connection)

with pytest.raises(Py42BadRequestError):
service.get_page(1, org_uid="TestOrgUid")

def test_deactivate_when_user_in_legal_hold_raises_active_legal_hold_error(
self, mocker, mock_connection
):
Expand Down

0 comments on commit 0fdab8f

Please sign in to comment.