Skip to content

Commit cc2b586

Browse files
authored
Add users show-risk-profile and list-risk-profiles commands (#388)
* Add users show-risk-profile and list-risk-profiles commands * update changelog
1 parent 74d4184 commit cc2b586

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1212

1313
### Added
1414

15+
- New commands to view details for user risk profiles:
16+
- `code42 users list-risk-profiles`
17+
- `code42 users show-risk-profile`
1518
- Proxy support via `HTTPS_PROXY` environment variable.
1619

1720
## 1.15.0 - 2022-08-23

src/code42cli/cmds/users.py

+65
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,71 @@ def show_user(state, username, include_legal_hold_membership, format):
131131
formatter.echo_formatted_dataframes(df)
132132

133133

134+
@users.command(name="list-risk-profiles")
135+
@active_option
136+
@inactive_option
137+
@click.option(
138+
"--manager-id",
139+
help="Matches users whose manager has the given Code42 user ID.",
140+
)
141+
@click.option("--department", help="Matches users in the given department.")
142+
@click.option("--employment-type", help="Matches users with the given employment type.")
143+
@click.option("-r", "--region", help="Matches users the given region (state).")
144+
@format_option
145+
@sdk_options()
146+
def list_user_risk_profiles(
147+
state,
148+
active,
149+
inactive,
150+
manager_id,
151+
department,
152+
employment_type,
153+
region,
154+
format,
155+
):
156+
"""List users in your Code42 environment."""
157+
if inactive:
158+
active = False
159+
columns = (
160+
[
161+
"userId",
162+
"username",
163+
"active",
164+
"department",
165+
"employmentType",
166+
"region",
167+
"endDate",
168+
]
169+
if format == OutputFormat.TABLE
170+
else None
171+
)
172+
users_generator = state.sdk.userriskprofile.get_all(
173+
active=active,
174+
manager_id=manager_id,
175+
department=department,
176+
employment_type=employment_type,
177+
region=region,
178+
)
179+
users_list = []
180+
for page in users_generator:
181+
users_list.extend(page["userRiskProfiles"])
182+
183+
df = DataFrame.from_records(users_list, columns=columns)
184+
formatter = DataFrameOutputFormatter(format)
185+
formatter.echo_formatted_dataframes(df)
186+
187+
188+
@users.command("show-risk-profile")
189+
@username_arg
190+
@format_option
191+
@sdk_options()
192+
def show_user_risk_profile(state, username, format):
193+
"""Show user risk profile details."""
194+
formatter = OutputFormatter(format)
195+
response = state.sdk.userriskprofile.get_by_username(username)
196+
formatter.echo_formatted_list([response.data])
197+
198+
134199
@users.command()
135200
@username_option("Username of the target user.")
136201
@role_name_option("Name of role to add.")

tests/cmds/test_users.py

+79
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@
6363
"country": "US",
6464
"riskFactors": ["FLIGHT_RISK", "HIGH_IMPACT_EMPLOYEE"],
6565
}
66+
TEST_PROFILE_RESPONSE = {
67+
"userId": "12345-42",
68+
"tenantId": "SampleTenant1",
69+
"username": "[email protected]",
70+
"displayName": "Foo Bar",
71+
"notes": "",
72+
"managerId": "123-42",
73+
"managerUsername": "[email protected]",
74+
"managerDisplayName": "",
75+
"title": "Engineer",
76+
"division": "Engineering",
77+
"department": "RDO",
78+
"employmentType": "Remote",
79+
"country": "USA",
80+
"region": "Minnesota",
81+
"locality": "Minneapolis",
82+
"active": True,
83+
"deleted": False,
84+
"supportUser": False,
85+
"startDate": {"year": 2020, "month": 8, "day": 10},
86+
"endDate": {"year": 2021, "month": 5, "day": 1},
87+
"cloudAliases": ["[email protected]", "[email protected]"],
88+
}
89+
6690
TEST_MATTER_RESPONSE = {
6791
"legalHolds": [
6892
{"legalHoldUid": "123456789", "name": "Legal Hold #1", "active": True},
@@ -615,6 +639,61 @@ def test_show_include_legal_hold_membership_merges_in_and_concats_legal_hold_inf
615639
assert "123456789,987654321" in result.output
616640

617641

642+
def test_list_risk_profiles_calls_get_all_user_risk_profiles_with_default_parameters(
643+
runner, cli_state
644+
):
645+
runner.invoke(
646+
cli,
647+
["users", "list-risk-profiles"],
648+
obj=cli_state,
649+
)
650+
cli_state.sdk.userriskprofile.get_all.assert_called_once_with(
651+
active=None, manager_id=None, department=None, employment_type=None, region=None
652+
)
653+
654+
655+
def test_list_risk_profiles_calls_get_all_user_risk_profiles_with_correct_parameters(
656+
runner, cli_state
657+
):
658+
r = runner.invoke(
659+
cli,
660+
[
661+
"users",
662+
"list-risk-profiles",
663+
"--active",
664+
"--manager-id",
665+
"123-42",
666+
"--department",
667+
"Engineering",
668+
"--employment-type",
669+
"Remote",
670+
"--region",
671+
"Minnesota",
672+
],
673+
obj=cli_state,
674+
)
675+
print(r.output)
676+
cli_state.sdk.userriskprofile.get_all.assert_called_once_with(
677+
active=True,
678+
manager_id="123-42",
679+
department="Engineering",
680+
employment_type="Remote",
681+
region="Minnesota",
682+
)
683+
684+
685+
def test_show_risk_profile_calls_user_risk_profile_get_by_username_with(
686+
runner, cli_state, get_users_response
687+
):
688+
runner.invoke(
689+
cli,
690+
["users", "show-risk-profile", "[email protected]"],
691+
obj=cli_state,
692+
)
693+
694+
cli_state.sdk.userriskprofile.get_by_username.assert_called_once_with("[email protected]")
695+
696+
618697
def test_add_user_role_adds(
619698
runner, cli_state, get_user_id_success, get_available_roles_success
620699
):

0 commit comments

Comments
 (0)