Skip to content

Commit eba3af3

Browse files
authored
handle detection list removal errors (#153)
* handle detectionlist removal errors * also handle alert-rules removal error * style * add tests * style
1 parent 16f27c4 commit eba3af3

File tree

6 files changed

+125
-3
lines changed

6 files changed

+125
-3
lines changed

src/code42cli/cmds/alert_rules.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import click
44
from click import echo
5+
from py42.exceptions import Py42BadRequestError
56
from py42.util import format_json
67

78
from code42cli import PRODUCT_NAME
@@ -68,7 +69,12 @@ def add_user(state, rule_id, username):
6869
@sdk_options()
6970
def remove_user(state, rule_id, username):
7071
"""Remove a user from an alert rule."""
71-
_remove_user(state.sdk, rule_id, username)
72+
try:
73+
_remove_user(state.sdk, rule_id, username)
74+
except Py42BadRequestError:
75+
raise Code42CLIError(
76+
"User {} is not currently assigned to rule-id {}.".format(username, rule_id)
77+
)
7278

7379

7480
@alert_rules.command("list")

src/code42cli/cmds/departing_employee.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import click
2+
from py42.exceptions import Py42NotFoundError
23

34
from code42cli.bulk import generate_template_cmd_factory
45
from code42cli.bulk import run_bulk_process
@@ -46,7 +47,14 @@ def add(state, username, cloud_alias, departure_date, notes):
4647
@sdk_options()
4748
def remove(state, username):
4849
"""Remove a user from the departing-employee detection list."""
49-
_remove_departing_employee(state.sdk, username)
50+
try:
51+
_remove_departing_employee(state.sdk, username)
52+
except Py42NotFoundError:
53+
raise Code42CLIError(
54+
"User {} is not currently on the departing-employee detection list.".format(
55+
username
56+
)
57+
)
5058

5159

5260
@departing_employee.group(cls=OrderedGroup)

src/code42cli/cmds/high_risk_employee.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import click
22
from py42.clients.detectionlists import RiskTags
3+
from py42.exceptions import Py42NotFoundError
34

45
from code42cli.bulk import generate_template_cmd_factory
56
from code42cli.bulk import run_bulk_process
@@ -11,6 +12,7 @@
1112
from code42cli.cmds.detectionlists.options import notes_option
1213
from code42cli.cmds.detectionlists.options import username_arg
1314
from code42cli.cmds.shared import get_user_id
15+
from code42cli.errors import Code42CLIError
1416
from code42cli.file_readers import read_csv_arg
1517
from code42cli.file_readers import read_flat_file_arg
1618
from code42cli.options import OrderedGroup
@@ -48,7 +50,14 @@ def add(state, username, cloud_alias, risk_tag, notes):
4850
@sdk_options()
4951
def remove(state, username):
5052
"""Remove a user from the high risk employees detection list."""
51-
_remove_high_risk_employee(state.sdk, username)
53+
try:
54+
_remove_high_risk_employee(state.sdk, username)
55+
except Py42NotFoundError:
56+
raise Code42CLIError(
57+
"User {} is not currently on the high-risk-employee detection list.".format(
58+
username
59+
)
60+
)
5261

5362

5463
@high_risk_employee.command()

tests/cmds/test_alert_rules.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33

44
import pytest
5+
from py42.exceptions import Py42BadRequestError
56
from py42.exceptions import Py42InternalServerError
67
from py42.exceptions import Py42InvalidRuleOperationError
78
from py42.response import Py42Response
@@ -55,6 +56,17 @@ def side_effect(*args, **kwargs):
5556
return side_effect
5657

5758

59+
def get_user_not_on_alert_rule_side_effect(mocker):
60+
def side_effect(*args, **kwargs):
61+
err = mocker.MagicMock(spec=HTTPError)
62+
resp = mocker.MagicMock(spec=Response)
63+
resp.text = "TEST_ERR"
64+
err.response = resp
65+
raise Py42BadRequestError(err)
66+
67+
return side_effect
68+
69+
5870
def create_invalid_rule_type_side_effect(mocker):
5971
def side_effect(*args, **kwargs):
6072
err = mocker.MagicMock(spec=HTTPError)
@@ -312,3 +324,22 @@ def test_list_cmd_formats_to_csv_when_format_is_passed(runner, cli_state):
312324
assert "ruleSource" in result.output
313325
assert "name" in result.output
314326
assert "severity" in result.output
327+
328+
329+
def test_remove_when_user_not_on_rule_raises_expected_error(runner, cli_state, mocker):
330+
cli_state.sdk.alerts.rules.remove_user.side_effect = get_user_not_on_alert_rule_side_effect(
331+
mocker
332+
)
333+
test_username = "test@example.com"
334+
test_rule_id = "101010"
335+
result = runner.invoke(
336+
cli,
337+
["alert-rules", "remove-user", "-u", test_username, "--rule-id", test_rule_id],
338+
obj=cli_state,
339+
)
340+
assert (
341+
"User {} is not currently assigned to rule-id {}.".format(
342+
test_username, test_rule_id
343+
)
344+
in result.output
345+
)

tests/cmds/test_departing_employee.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
from py42.exceptions import Py42NotFoundError
2+
from requests import HTTPError
3+
from requests import Request
4+
from requests import Response
15
from tests.cmds.conftest import thread_safe_side_effect
26
from tests.conftest import TEST_ID
37

48
from .conftest import TEST_EMPLOYEE
59
from code42cli.main import cli
610

711

12+
def get_user_not_on_departing_employee_list_side_effect(mocker):
13+
def side_effect(*args, **kwargs):
14+
err = mocker.MagicMock(spec=HTTPError)
15+
resp = mocker.MagicMock(spec=Response)
16+
resp.text = "TEST_ERR"
17+
err.response = resp
18+
err.response.request = mocker.MagicMock(spec=Request)
19+
raise Py42NotFoundError(err)
20+
21+
return side_effect
22+
23+
824
def test_add_departing_employee_when_given_cloud_alias_adds_alias(
925
runner, cli_state_with_user
1026
):
@@ -192,3 +208,21 @@ def test_add_departing_employee_when_invalid_date_format_validation_raises_error
192208
assert (
193209
"Invalid value for '--departure-date': invalid datetime format" in result.output
194210
)
211+
212+
213+
def test_remove_departing_employee_when_user_not_on_list_prints_expected_error(
214+
mocker, runner, cli_state
215+
):
216+
cli_state.sdk.detectionlists.departing_employee.remove.side_effect = get_user_not_on_departing_employee_list_side_effect(
217+
mocker
218+
)
219+
test_username = "test@example.com"
220+
result = runner.invoke(
221+
cli, ["departing-employee", "remove", test_username], obj=cli_state
222+
)
223+
assert (
224+
"User {} is not currently on the departing-employee detection list.".format(
225+
test_username
226+
)
227+
in result.output
228+
)

tests/cmds/test_high_risk_employee.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from py42.exceptions import Py42NotFoundError
2+
from requests import HTTPError
3+
from requests import Request
4+
from requests import Response
15
from tests.cmds.conftest import TEST_EMPLOYEE
26
from tests.cmds.conftest import thread_safe_side_effect
37
from tests.conftest import TEST_ID
@@ -7,6 +11,18 @@
711
_NAMESPACE = "code42cli.cmds.high_risk_employee"
812

913

14+
def get_user_not_on_high_risk_employee_list_side_effect(mocker):
15+
def side_effect(*args, **kwargs):
16+
err = mocker.MagicMock(spec=HTTPError)
17+
resp = mocker.MagicMock(spec=Response)
18+
resp.text = "TEST_ERR"
19+
err.response = resp
20+
err.response.request = mocker.MagicMock(spec=Request)
21+
raise Py42NotFoundError(err)
22+
23+
return side_effect
24+
25+
1026
def test_add_high_risk_employee_adds(runner, cli_state_with_user):
1127
runner.invoke(
1228
cli, ["high-risk-employee", "add", TEST_EMPLOYEE], obj=cli_state_with_user
@@ -219,3 +235,21 @@ def test_bulk_remove_risk_tags_uses_expected_arguments(runner, cli_state, mocker
219235
{"username": "test@example.com", "tag": "tag1"},
220236
{"username": "test2@example.com", "tag": "tag2"},
221237
]
238+
239+
240+
def test_remove_high_risk_employee_when_user_not_on_list_prints_expected_error(
241+
mocker, runner, cli_state
242+
):
243+
cli_state.sdk.detectionlists.high_risk_employee.remove.side_effect = get_user_not_on_high_risk_employee_list_side_effect(
244+
mocker
245+
)
246+
test_username = "test@example.com"
247+
result = runner.invoke(
248+
cli, ["high-risk-employee", "remove", test_username], obj=cli_state
249+
)
250+
assert (
251+
"User {} is not currently on the high-risk-employee detection list.".format(
252+
test_username
253+
)
254+
in result.output
255+
)

0 commit comments

Comments
 (0)