Skip to content

Add users show-risk-profile and list-risk-profiles commands #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta

### Added

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

## 1.15.0 - 2022-08-23
Expand Down
65 changes: 65 additions & 0 deletions src/code42cli/cmds/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,71 @@ def show_user(state, username, include_legal_hold_membership, format):
formatter.echo_formatted_dataframes(df)


@users.command(name="list-risk-profiles")
@active_option
@inactive_option
@click.option(
"--manager-id",
help="Matches users whose manager has the given Code42 user ID.",
)
@click.option("--department", help="Matches users in the given department.")
@click.option("--employment-type", help="Matches users with the given employment type.")
@click.option("-r", "--region", help="Matches users the given region (state).")
@format_option
@sdk_options()
def list_user_risk_profiles(
state,
active,
inactive,
manager_id,
department,
employment_type,
region,
format,
):
"""List users in your Code42 environment."""
if inactive:
active = False
columns = (
[
"userId",
"username",
"active",
"department",
"employmentType",
"region",
"endDate",
]
if format == OutputFormat.TABLE
else None
)
users_generator = state.sdk.userriskprofile.get_all(
active=active,
manager_id=manager_id,
department=department,
employment_type=employment_type,
region=region,
)
users_list = []
for page in users_generator:
users_list.extend(page["userRiskProfiles"])

df = DataFrame.from_records(users_list, columns=columns)
formatter = DataFrameOutputFormatter(format)
formatter.echo_formatted_dataframes(df)


@users.command("show-risk-profile")
@username_arg
@format_option
@sdk_options()
def show_user_risk_profile(state, username, format):
"""Show user risk profile details."""
formatter = OutputFormatter(format)
response = state.sdk.userriskprofile.get_by_username(username)
formatter.echo_formatted_list([response.data])


@users.command()
@username_option("Username of the target user.")
@role_name_option("Name of role to add.")
Expand Down
79 changes: 79 additions & 0 deletions tests/cmds/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@
"country": "US",
"riskFactors": ["FLIGHT_RISK", "HIGH_IMPACT_EMPLOYEE"],
}
TEST_PROFILE_RESPONSE = {
"userId": "12345-42",
"tenantId": "SampleTenant1",
"username": "foo@bar.com",
"displayName": "Foo Bar",
"notes": "",
"managerId": "123-42",
"managerUsername": "test@bar.com",
"managerDisplayName": "",
"title": "Engineer",
"division": "Engineering",
"department": "RDO",
"employmentType": "Remote",
"country": "USA",
"region": "Minnesota",
"locality": "Minneapolis",
"active": True,
"deleted": False,
"supportUser": False,
"startDate": {"year": 2020, "month": 8, "day": 10},
"endDate": {"year": 2021, "month": 5, "day": 1},
"cloudAliases": ["baz@bar.com", "foo@bar.com"],
}

TEST_MATTER_RESPONSE = {
"legalHolds": [
{"legalHoldUid": "123456789", "name": "Legal Hold #1", "active": True},
Expand Down Expand Up @@ -615,6 +639,61 @@ def test_show_include_legal_hold_membership_merges_in_and_concats_legal_hold_inf
assert "123456789,987654321" in result.output


def test_list_risk_profiles_calls_get_all_user_risk_profiles_with_default_parameters(
runner, cli_state
):
runner.invoke(
cli,
["users", "list-risk-profiles"],
obj=cli_state,
)
cli_state.sdk.userriskprofile.get_all.assert_called_once_with(
active=None, manager_id=None, department=None, employment_type=None, region=None
)


def test_list_risk_profiles_calls_get_all_user_risk_profiles_with_correct_parameters(
runner, cli_state
):
r = runner.invoke(
cli,
[
"users",
"list-risk-profiles",
"--active",
"--manager-id",
"123-42",
"--department",
"Engineering",
"--employment-type",
"Remote",
"--region",
"Minnesota",
],
obj=cli_state,
)
print(r.output)
cli_state.sdk.userriskprofile.get_all.assert_called_once_with(
active=True,
manager_id="123-42",
department="Engineering",
employment_type="Remote",
region="Minnesota",
)


def test_show_risk_profile_calls_user_risk_profile_get_by_username_with(
runner, cli_state, get_users_response
):
runner.invoke(
cli,
["users", "show-risk-profile", "foo@bar.com"],
obj=cli_state,
)

cli_state.sdk.userriskprofile.get_by_username.assert_called_once_with("foo@bar.com")


def test_add_user_role_adds(
runner, cli_state, get_user_id_success, get_available_roles_success
):
Expand Down