Skip to content

Commit ef4c8d9

Browse files
author
Juliya Smith
authored
Handle updating cloud aliases when adding to a list (#155)
1 parent d571763 commit ef4c8d9

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
## Unreleased
12+
13+
### Changed
14+
15+
- Now, when adding a cloud alias to a detection list user, such as during `departing-employee add`, it will remove the existing cloud alias if one exists.
16+
- Before, it would error and the cloud alias would not get added.
17+
1118
## 1.0.0 - 2020-08-31
1219

1320
### Fixed

src/code42cli/cmds/detectionlists/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@ def update_user(sdk, username, cloud_alias=None, risk_tag=None, notes=None):
1212
notes (str or unicode): Notes about the user.
1313
"""
1414
user_id = get_user_id(sdk, username)
15+
_update_cloud_alias(sdk, user_id, cloud_alias)
16+
_update_risk_tags(sdk, username, risk_tag)
17+
_update_notes(sdk, user_id, notes)
18+
19+
20+
def _update_cloud_alias(sdk, user_id, cloud_alias):
1521
if cloud_alias:
22+
profile = sdk.detectionlists.get_user_by_id(user_id)
23+
cloud_aliases = profile.data.get("cloudUsernames") or []
24+
for alias in cloud_aliases:
25+
if alias != profile["userName"]:
26+
sdk.detectionlists.remove_user_cloud_alias(user_id, alias)
1627
sdk.detectionlists.add_user_cloud_alias(user_id, cloud_alias)
28+
29+
30+
def _update_risk_tags(sdk, username, risk_tag):
1731
if risk_tag:
1832
add_risk_tags(sdk, username, risk_tag)
33+
34+
35+
def _update_notes(sdk, user_id, notes):
1936
if notes:
2037
sdk.detectionlists.update_user_notes(user_id, notes)
2138

src/code42cli/cmds/detectionlists/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"--cloud-alias",
66
help="If the employee has an email alias other than their Code42 username "
77
"that they use for cloud services such as Google Drive, OneDrive, or Box, "
8-
"add and monitor the alias.",
8+
"add and monitor the alias. WARNING: Adding a cloud alias will override any "
9+
"existing cloud alias for this user.",
910
)
1011
notes_option = click.option("--notes", help="Optional notes about the employee.")

tests/cmds/detectionlists/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
from py42.response import Py42Response
3+
from requests import Response
4+
5+
from code42cli.cmds.detectionlists import update_user
6+
7+
8+
MOCK_USER_ID = "USER-ID"
9+
MOCK_USER_NAME = "test@example.com"
10+
MOCK_ALIAS = "alias@example"
11+
MOCK_USER_PROFILE_RESPONSE = """
12+
{{
13+
"type$": "USER_V2",
14+
"tenantId": "TENANT-ID",
15+
"userId": "{0}",
16+
"userName": "{1}",
17+
"displayName": "Test",
18+
"notes": "Notes",
19+
"cloudUsernames": ["{2}", "{1}"],
20+
"riskFactors": ["HIGH_IMPACT_EMPLOYEE"]
21+
}}
22+
""".format(
23+
MOCK_USER_ID, MOCK_USER_NAME, MOCK_ALIAS
24+
)
25+
26+
27+
@pytest.fixture
28+
def user_response_with_cloud_aliases(mocker):
29+
response = mocker.MagicMock(spec=Response)
30+
response.text = MOCK_USER_PROFILE_RESPONSE
31+
return Py42Response(response)
32+
33+
34+
@pytest.fixture
35+
def mock_user_id(mocker):
36+
mock = mocker.patch("code42cli.cmds.detectionlists.get_user_id")
37+
mock.return_value = MOCK_USER_ID
38+
return mock
39+
40+
41+
def test_update_user_when_given_cloud_alias_add_cloud_alias(
42+
sdk, user_response_with_cloud_aliases, mock_user_id
43+
):
44+
sdk.detectionlists.get_user_by_id.return_value = user_response_with_cloud_aliases
45+
update_user(sdk, MOCK_USER_NAME, cloud_alias="new.alias@exaple.com")
46+
sdk.detectionlists.add_user_cloud_alias.assert_called_once_with(
47+
MOCK_USER_ID, "new.alias@exaple.com"
48+
)
49+
50+
51+
def test_update_user_when_given_cloud_alias_first_removes_old_alias(
52+
sdk, user_response_with_cloud_aliases, mock_user_id
53+
):
54+
sdk.detectionlists.get_user_by_id.return_value = user_response_with_cloud_aliases
55+
update_user(sdk, MOCK_USER_NAME, cloud_alias="new.alias@exaple.com")
56+
sdk.detectionlists.remove_user_cloud_alias.assert_called_once_with(
57+
MOCK_USER_ID, MOCK_ALIAS
58+
)

0 commit comments

Comments
 (0)