From f841f82f89d8acaa3bc28c9a6396ca1f6b34f0f6 Mon Sep 17 00:00:00 2001 From: Tora Kozic Date: Wed, 31 Aug 2022 13:01:37 -0500 Subject: [PATCH 1/2] Add users show-risk-profile and list-risk-profiles commands --- src/code42cli/cmds/users.py | 65 ++++++++++++++++++++++++++++++ tests/cmds/test_users.py | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/src/code42cli/cmds/users.py b/src/code42cli/cmds/users.py index 5f345c41..284d7b22 100644 --- a/src/code42cli/cmds/users.py +++ b/src/code42cli/cmds/users.py @@ -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.") diff --git a/tests/cmds/test_users.py b/tests/cmds/test_users.py index a9006f6b..32bcc3be 100644 --- a/tests/cmds/test_users.py +++ b/tests/cmds/test_users.py @@ -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}, @@ -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 ): From 997aa2ac400095722db0a2a42b36e33d235b3289 Mon Sep 17 00:00:00 2001 From: Tora Kozic Date: Tue, 13 Sep 2022 12:47:15 -0500 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f193e31a..23608d6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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