Skip to content

Commit d88d764

Browse files
author
Juliya Smith
authored
Chore/user already added error (#55)
1 parent 99bf3ca commit d88d764

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

src/code42cli/cmds/detectionlists/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
)
1010

1111

12+
class UserAlreadyAddedError(Exception):
13+
def __init__(self, username, list_name):
14+
msg = u"'{}' is already on the {} list.".format(username, list_name)
15+
super(UserAlreadyAddedError, self).__init__(msg)
16+
17+
18+
def handle_bad_request_during_add(bad_request_err, username_tried_adding, list_name):
19+
if _error_is_user_already_added(bad_request_err.response.text):
20+
logger = get_main_cli_logger()
21+
new_err = UserAlreadyAddedError(username_tried_adding, list_name)
22+
logger.print_and_log_error(new_err)
23+
return True
24+
return False
25+
26+
27+
def _error_is_user_already_added(bad_request_error_text):
28+
return u"User already on list" in bad_request_error_text
29+
30+
1231
class UserDoesNotExistError(Exception):
1332
"""An error to represent a username that is not in our system. The CLI shows this error when
1433
the user tries to add or remove a user that does not exist. This error is not shown during

src/code42cli/cmds/detectionlists/departing_employee.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
load_user_descriptions,
55
get_user_id,
66
update_user,
7+
handle_bad_request_during_add,
78
)
9+
from code42cli.cmds.detectionlists.enums import DetectionLists
10+
11+
from py42.exceptions import Py42BadRequestError
812

913

1014
def load_subcommands():
@@ -33,8 +37,13 @@ def add_departing_employee(
3337
notes: (str): Notes about the employee.
3438
"""
3539
user_id = get_user_id(sdk, username)
36-
sdk.detectionlists.departing_employee.add(user_id, departure_date)
37-
update_user(sdk, user_id, cloud_alias, notes=notes)
40+
41+
try:
42+
sdk.detectionlists.departing_employee.add(user_id, departure_date)
43+
update_user(sdk, user_id, cloud_alias, notes=notes)
44+
except Py42BadRequestError as err:
45+
if not handle_bad_request_during_add(err, username, DetectionLists.DEPARTING_EMPLOYEE):
46+
raise
3847

3948

4049
def remove_departing_employee(sdk, profile, username):

src/code42cli/cmds/detectionlists/high_risk_employee.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
load_username_description,
66
get_user_id,
77
update_user,
8+
handle_bad_request_during_add,
89
)
10+
from code42cli.cmds.detectionlists.enums import DetectionLists
911
from code42cli.cmds.detectionlists.enums import DetectionListUserKeys
1012
from code42cli.commands import Command
1113

14+
from py42.exceptions import Py42BadRequestError
15+
1216

1317
def load_subcommands():
1418

@@ -65,8 +69,13 @@ def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=No
6569
"""
6670
risk_tag = _handle_list_args(risk_tag)
6771
user_id = get_user_id(sdk, username)
68-
sdk.detectionlists.high_risk_employee.add(user_id)
69-
update_user(sdk, user_id, cloud_alias, risk_tag, notes)
72+
73+
try:
74+
sdk.detectionlists.high_risk_employee.add(user_id)
75+
update_user(sdk, user_id, cloud_alias, risk_tag, notes)
76+
except Py42BadRequestError as err:
77+
if not handle_bad_request_during_add(err, username, DetectionLists.HIGH_RISK_EMPLOYEE):
78+
raise
7079

7180

7281
def remove_high_risk_employee(sdk, profile, username):

tests/cmds/detectionlists/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import pytest
2+
from requests import Response, HTTPError
3+
4+
from py42.exceptions import Py42BadRequestError
25

36

47
TEST_ID = "TEST_ID"
@@ -14,3 +17,23 @@ def sdk_with_user(sdk):
1417
def sdk_without_user(sdk):
1518
sdk.users.get_by_username.return_value = {"users": []}
1619
return sdk
20+
21+
22+
@pytest.fixture
23+
def bad_request_for_user_already_added(mocker):
24+
resp = mocker.MagicMock(spec=Response)
25+
resp.text = "User already on list"
26+
return _create_bad_request_mock(resp)
27+
28+
29+
@pytest.fixture
30+
def bad_request_for_other_reasons(mocker):
31+
resp = mocker.MagicMock(spec=Response)
32+
resp.text = "Some other, non-supported reason for a bad request"
33+
return _create_bad_request_mock(resp)
34+
35+
36+
def _create_bad_request_mock(resp):
37+
base_err = HTTPError()
38+
base_err.response = resp
39+
return Py42BadRequestError(base_err)

tests/cmds/detectionlists/test_departing_employee.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
add_departing_employee,
77
remove_departing_employee,
88
)
9+
910
from .conftest import TEST_ID
1011

12+
from py42.exceptions import Py42BadRequestError
13+
1114

1215
_EMPLOYEE = "departing employee"
1316

@@ -46,6 +49,27 @@ def test_add_departing_employee_when_user_does_not_exist_prints_error(
4649
assert str(UserDoesNotExistError(_EMPLOYEE)) in caplog.text
4750

4851

52+
def test_add_departing_employee_when_user_already_added_prints_error(
53+
sdk_with_user, profile, bad_request_for_user_already_added, caplog
54+
):
55+
sdk_with_user.detectionlists.departing_employee.add.side_effect = (
56+
bad_request_for_user_already_added
57+
)
58+
add_departing_employee(sdk_with_user, profile, _EMPLOYEE)
59+
with caplog.at_level(logging.ERROR):
60+
assert _EMPLOYEE in caplog.text
61+
assert "already on the" in caplog.text
62+
assert "departing-employee" in caplog.text
63+
64+
65+
def test_add_departing_employee_when_bad_request_but_not_user_already_added_raises_Py42BadRequestError(
66+
sdk_with_user, profile, bad_request_for_other_reasons, caplog
67+
):
68+
sdk_with_user.detectionlists.departing_employee.add.side_effect = bad_request_for_other_reasons
69+
with pytest.raises(Py42BadRequestError):
70+
add_departing_employee(sdk_with_user, profile, _EMPLOYEE)
71+
72+
4973
def test_remove_departing_employee_calls_remove(sdk_with_user, profile):
5074
remove_departing_employee(sdk_with_user, profile, _EMPLOYEE)
5175
sdk_with_user.detectionlists.departing_employee.remove.assert_called_once_with(TEST_ID)

tests/cmds/detectionlists/test_high_risk_employee.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
)
1111
from .conftest import TEST_ID
1212

13+
from py42.exceptions import Py42BadRequestError
14+
1315

1416
_EMPLOYEE = "risky employee"
1517

@@ -59,6 +61,27 @@ def test_add_high_risk_employee_when_user_does_not_exist_prints_error(
5961
assert str(UserDoesNotExistError(_EMPLOYEE)) in caplog.text
6062

6163

64+
def test_add_high_risk_employee_when_user_already_added_prints_error(
65+
sdk_with_user, profile, bad_request_for_user_already_added, caplog
66+
):
67+
sdk_with_user.detectionlists.high_risk_employee.add.side_effect = (
68+
bad_request_for_user_already_added
69+
)
70+
add_high_risk_employee(sdk_with_user, profile, _EMPLOYEE)
71+
with caplog.at_level(logging.ERROR):
72+
assert _EMPLOYEE in caplog.text
73+
assert "already on the" in caplog.text
74+
assert "high-risk-employee" in caplog.text
75+
76+
77+
def test_add_high_risk_employee_when_bad_request_but_not_user_already_added_raises_Py42BadRequestError(
78+
sdk_with_user, profile, bad_request_for_other_reasons, caplog
79+
):
80+
sdk_with_user.detectionlists.high_risk_employee.add.side_effect = bad_request_for_other_reasons
81+
with pytest.raises(Py42BadRequestError):
82+
add_high_risk_employee(sdk_with_user, profile, _EMPLOYEE)
83+
84+
6285
def test_remove_high_risk_employee_calls_remove(sdk_with_user, profile):
6386
remove_high_risk_employee(sdk_with_user, profile, _EMPLOYEE)
6487
sdk_with_user.detectionlists.high_risk_employee.remove.assert_called_once_with(TEST_ID)

0 commit comments

Comments
 (0)