Skip to content

Commit

Permalink
adjust error handling when adding user cloud aliases (#407)
Browse files Browse the repository at this point in the history
* adjust error handling when adding user cloud aliases

* update changelog
  • Loading branch information
tora-kozic committed Feb 18, 2022
1 parent e7f9ce6 commit e51215d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
- `DestinationCategory`
- `DestinationName`

### Fixed

- Improved error handling for `sdk.detectionlists.add_user_cloud_alias()`

## 1.20.0 - 2022-01-10

### Added
Expand Down
13 changes: 12 additions & 1 deletion src/py42/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,21 @@ class Py42CloudAliasLimitExceededError(Py42BadRequestError):
already has the max amount of supported cloud aliases."""

def __init__(self, exception, message=None):
message = message or "Cloud alias limit exceeded."
message = (
message
or "Cloud alias limit exceeded. A max of 2 cloud aliases are allowed."
)
super(Py42BadRequestError, self).__init__(exception, message)


class Py42CloudAliasCharacterLimitExceededError(Py42Error):
"""An exception raised when trying to add a cloud alias to a user that exceeds the max character limit."""

def __init__(self):
message = "Cloud alias character limit exceeded. Max 50 characters."
super().__init__(message)


class Py42BadRestoreRequestError(Py42BadRequestError):
"""An error raised when the given restore arguments are not compatible and cause
a bad request."""
Expand Down
9 changes: 8 additions & 1 deletion src/py42/services/detectionlists/user_profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42CloudAliasCharacterLimitExceededError
from py42.exceptions import Py42CloudAliasLimitExceededError
from py42.services import BaseService

Expand Down Expand Up @@ -124,6 +125,12 @@ def add_cloud_alias(self, user_id, alias):
Returns:
:class:`py42.response.Py42Response`
"""

# check if alias > 50 characters
# this error checking is handled by the frontend of the console
if len(alias) > 50:
raise Py42CloudAliasCharacterLimitExceededError

data = {
"tenantId": self._user_context.get_current_tenant_id(),
"userId": user_id,
Expand All @@ -133,7 +140,7 @@ def add_cloud_alias(self, user_id, alias):
try:
return self._connection.post(uri, json=data)
except Py42BadRequestError as err:
if "Cloud usernames must be less than or equal to" in err.response.text:
if "A max of 2 cloud aliases are allowed" in err.response.text:
raise Py42CloudAliasLimitExceededError(err)
raise err

Expand Down
37 changes: 20 additions & 17 deletions tests/services/detectionlists/test_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tests.conftest import create_mock_response

from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42CloudAliasCharacterLimitExceededError
from py42.exceptions import Py42CloudAliasLimitExceededError
from py42.services.detectionlists.user_profile import DetectionListUserService
from py42.services.users import UserService
Expand Down Expand Up @@ -43,16 +44,6 @@ def mock_user_client_raises_exception(
)
return user_client

@pytest.fixture
def mock_user_client_error_on_adding_cloud_aliases(
self, mocker, mock_connection, user_context,
):
user_client = UserService(mock_connection)
mock_connection.post.side_effect = create_mock_error(
Py42BadRequestError, mocker, CLOUD_ALIAS_LIMIT_EXCEEDED_ERROR_MESSAGE
)
return user_client

def test_get_posts_expected_data(
self, mock_connection, user_context, mock_user_client
):
Expand Down Expand Up @@ -189,16 +180,28 @@ def test_refresh_posts_expected_data(
)

def test_add_cloud_alias_limit_raises_custom_error_on_limit(
self,
mock_connection,
user_context,
mock_user_client_error_on_adding_cloud_aliases,
self, mocker, mock_connection, user_context, mock_user_client
):
detection_list_user_client = DetectionListUserService(
mock_connection,
user_context,
mock_user_client_error_on_adding_cloud_aliases,
mock_connection, user_context, mock_user_client
)
text = "AddCloudAliases: A max of 2 cloud aliases are allowed"
mock_connection.post.side_effect = create_mock_error(
Py42BadRequestError, mocker, text
)
with pytest.raises(Py42CloudAliasLimitExceededError) as err:
detection_list_user_client.add_cloud_alias("942897397520289999", "Test")
assert "Cloud alias limit exceeded." in str(err.value)

def test_add_cloud_alias_when_over_character_limit_raises_custom_error(
self, mock_connection, user_context, mock_user_client
):
detection_list_user_client = DetectionListUserService(
mock_connection, user_context, mock_user_client
)
with pytest.raises(Py42CloudAliasCharacterLimitExceededError) as err:
detection_list_user_client.add_cloud_alias(
"942897397520289999",
"a-very-long-cloud-alias-which-exceeds-the-character-limit-of-fifty-characters-per-alias",
)
assert "Cloud alias character limit exceeded." in str(err.value)

0 comments on commit e51215d

Please sign in to comment.