Skip to content

Commit 3eb66a1

Browse files
author
Juliya Smith
authored
risk tags cmds (#41)
1 parent 2f67630 commit 3eb66a1

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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
11+
## 0.5.0 - Unreleased
1212

1313
### Changed
1414

@@ -36,6 +36,8 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
3636
- `remove`: that takes a list of users in a file.
3737
- `add` that takes parameters: `--username`, `--cloud-alias`, `--risk-factor`, and `--notes`.
3838
- `remove` that takes a username.
39+
- `add-risk-tags` that takes a username and risk tags.
40+
- `remove-risk-tags` that takes a username and risk tags.
3941
- `code42 departing-employee` commands:
4042
- `bulk` with subcommands:
4143
- `add`: that takes a csv file of users.

src/code42cli/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.4"
1+
__version__ = "0.5.0"

src/code42cli/cmds/detectionlists/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def load_subcommands(self):
8383
self.handlers.add_employee, self.handlers.load_add_description
8484
)
8585
remove = self.factory.create_remove_command(
86-
self.handlers.remove_employee, _load_username_description
86+
self.handlers.remove_employee, load_username_description
8787
)
8888
return [bulk, add, remove]
8989

@@ -148,7 +148,9 @@ def _remove_employee(self, sdk, profile, *args, **kwargs):
148148
self.handlers.remove_employee(sdk, profile, *args, **kwargs)
149149

150150

151-
def _load_username_description(argument_collection):
151+
152+
def load_username_description(argument_collection):
153+
"""Loads the arg descriptions for the `username` CLI parameter."""
152154
username = argument_collection.arg_configs[DetectionListUserKeys.USERNAME]
153155
username.set_help(u"A code42 username for an employee.")
154156

@@ -161,7 +163,7 @@ def load_user_descriptions(argument_collection):
161163
argument_collection (ArgConfigCollection): The arg configs off the command that needs its
162164
user descriptions loaded.
163165
"""
164-
_load_username_description(argument_collection)
166+
load_username_description(argument_collection)
165167
cloud_alias = argument_collection.arg_configs[DetectionListUserKeys.CLOUD_ALIAS]
166168
notes = argument_collection.arg_configs[DetectionListUserKeys.NOTES]
167169
cloud_alias.set_help(u"An alternative email address for another cloud service.")

src/code42cli/cmds/detectionlists/high_risk_employee.py

+52-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22
DetectionList,
33
DetectionListHandlers,
44
load_user_descriptions,
5+
load_username_description,
56
get_user_id,
67
update_user,
78
)
89
from code42cli.cmds.detectionlists.enums import DetectionListUserKeys
10+
from code42cli.commands import Command
911

1012

1113
def load_subcommands():
14+
1215
handlers = _create_handlers()
1316
detection_list = DetectionList.create_high_risk_employee_list(handlers)
14-
return detection_list.load_subcommands()
17+
cmd_list = detection_list.load_subcommands()
18+
cmd_list.extend(
19+
[
20+
Command(
21+
u"add-risk-tags",
22+
u"Associates risk tags with a user.",
23+
handler=add_risk_tags,
24+
arg_customizer=_load_risk_tag_mgmt_descriptions,
25+
),
26+
Command(
27+
u"remove-risk-tags",
28+
u"Disassociates risk tags from a user.",
29+
handler=remove_risk_tags,
30+
arg_customizer=_load_risk_tag_mgmt_descriptions,
31+
),
32+
]
33+
)
34+
return cmd_list
1535

1636

1737
def _create_handlers():
@@ -20,6 +40,17 @@ def _create_handlers():
2040
)
2141

2242

43+
def add_risk_tags(sdk, profile, username, risk_tag):
44+
risk_tag = _handle_list_args(risk_tag)
45+
user_id = get_user_id(sdk, username)
46+
sdk.detectionlists.add_user_risk_tags(user_id, risk_tag)
47+
48+
def remove_risk_tags(sdk, profile, username, risk_tag):
49+
risk_tag = _handle_list_args(risk_tag)
50+
user_id = get_user_id(sdk, username)
51+
sdk.detectionlists.remove_user_risk_tags(user_id, risk_tag)
52+
53+
2354
def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=None, notes=None):
2455
"""Adds an employee to the high risk employee detection list.
2556
@@ -31,9 +62,7 @@ def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=No
3162
risk_tag (iter[str]): Risk tags associated with the employee.
3263
notes: (str): Notes about the employee.
3364
"""
34-
if risk_tag and type(risk_tag) != list:
35-
risk_tag = risk_tag.split()
36-
65+
risk_tag = _handle_list_args(risk_tag)
3766
user_id = get_user_id(sdk, username)
3867
update_user(sdk, user_id, cloud_alias, risk_tag, notes)
3968
sdk.detectionlists.high_risk_employee.add(user_id)
@@ -51,8 +80,7 @@ def remove_high_risk_employee(sdk, profile, username):
5180
sdk.detectionlists.high_risk_employee.remove(user_id)
5281

5382

54-
def _load_add_description(argument_collection):
55-
load_user_descriptions(argument_collection)
83+
def _load_risk_tag_description(argument_collection):
5684
risk_tag = argument_collection.arg_configs[DetectionListUserKeys.RISK_TAG]
5785
risk_tag.as_multi_val_param()
5886
risk_tag.set_help(
@@ -66,3 +94,21 @@ def _load_add_description(argument_collection):
6694
u"POOR_SECURITY_PRACTICES, "
6795
u"CONTRACT_EMPLOYEE]"
6896
)
97+
98+
99+
def _load_add_description(argument_collection):
100+
load_user_descriptions(argument_collection)
101+
_load_risk_tag_description(argument_collection)
102+
103+
104+
def _load_risk_tag_mgmt_descriptions(argument_collection):
105+
load_username_description(argument_collection)
106+
_load_risk_tag_description(argument_collection)
107+
108+
109+
def _handle_list_args(list_arg):
110+
"""Converts str args to a list. Useful for `bulk` commands which don't use `argparse` but
111+
instead pass in values from files, such as in the form "item1 item2"."""
112+
if list_arg and type(list_arg) != list:
113+
return list_arg.split()
114+
return list_arg

tests/cmds/detectionlists/test_high_risk_employee.py

+56
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from code42cli.cmds.detectionlists.high_risk_employee import (
55
add_high_risk_employee,
66
remove_high_risk_employee,
7+
add_risk_tags,
8+
remove_risk_tags,
79
)
810
from .conftest import TEST_ID
911

@@ -74,3 +76,57 @@ def test_remove_high_risk_employee_when_user_does_not_exist_prints_error(
7476
except UserDoesNotExistError:
7577
capture = capsys.readouterr()
7678
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out
79+
80+
81+
def test_add_risk_tags_adds_tags(sdk_with_user, profile):
82+
add_risk_tags(sdk_with_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
83+
sdk_with_user.detectionlists.add_user_risk_tags.assert_called_once_with(
84+
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
85+
)
86+
87+
88+
def test_add_risk_tags_when_given_space_delimited_str_adds_expected_tags(sdk_with_user, profile):
89+
add_risk_tags(sdk_with_user, profile, _EMPLOYEE, "TAG_YOU_ARE_IT GROUND_IS_LAVA")
90+
sdk_with_user.detectionlists.add_user_risk_tags.assert_called_once_with(
91+
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
92+
)
93+
94+
95+
def test_add_risk_tags_when_user_does_not_exist_exits(sdk_without_user, profile):
96+
with pytest.raises(UserDoesNotExistError):
97+
add_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
98+
99+
100+
def test_add_risk_tags_when_user_does_not_exist_prints_error(sdk_without_user, profile, capsys):
101+
try:
102+
add_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
103+
except UserDoesNotExistError:
104+
capture = capsys.readouterr()
105+
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out
106+
107+
108+
def test_remove_risk_tags_adds_tags(sdk_with_user, profile):
109+
remove_risk_tags(sdk_with_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
110+
sdk_with_user.detectionlists.remove_user_risk_tags.assert_called_once_with(
111+
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
112+
)
113+
114+
115+
def test_remove_risk_tags_when_given_space_delimited_str_adds_expected_tags(sdk_with_user, profile):
116+
remove_risk_tags(sdk_with_user, profile, _EMPLOYEE, "TAG_YOU_ARE_IT GROUND_IS_LAVA")
117+
sdk_with_user.detectionlists.remove_user_risk_tags.assert_called_once_with(
118+
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
119+
)
120+
121+
122+
def test_remove_risk_tags_when_user_does_not_exist_exits(sdk_without_user, profile):
123+
with pytest.raises(UserDoesNotExistError):
124+
remove_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
125+
126+
127+
def test_remove_risk_tags_when_user_does_not_exist_prints_error(sdk_without_user, profile, capsys):
128+
try:
129+
remove_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
130+
except UserDoesNotExistError:
131+
capture = capsys.readouterr()
132+
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out

0 commit comments

Comments
 (0)