|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from py42.response import Py42Response |
| 5 | +from requests import Response |
| 6 | + |
| 7 | +from code42cli.main import cli |
| 8 | + |
| 9 | +TEST_ROLE_RETURN_DATA = { |
| 10 | + "data": [{"roleName": "Customer Cloud Admin", "roleId": "1234543"}] |
| 11 | +} |
| 12 | + |
| 13 | +TEST_USERS_RESPONSE = { |
| 14 | + "users": [ |
| 15 | + { |
| 16 | + "userId": 1234, |
| 17 | + "userUid": "997962681513153325", |
| 18 | + "status": "Active", |
| 19 | + |
| 20 | + "creationDate": "2021-03-12T20:07:40.898Z", |
| 21 | + "modificationDate": "2021-03-12T20:07:40.938Z", |
| 22 | + } |
| 23 | + ] |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +def _create_py42_response(mocker, text): |
| 28 | + response = mocker.MagicMock(spec=Response) |
| 29 | + response.text = text |
| 30 | + response._content_consumed = mocker.MagicMock() |
| 31 | + response.status_code = 200 |
| 32 | + return Py42Response(response) |
| 33 | + |
| 34 | + |
| 35 | +def get_all_users_generator(): |
| 36 | + yield TEST_USERS_RESPONSE |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def get_available_roles_response(mocker): |
| 41 | + return _create_py42_response(mocker, json.dumps(TEST_ROLE_RETURN_DATA)) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def get_all_users_success(cli_state): |
| 46 | + cli_state.sdk.users.get_all.return_value = get_all_users_generator() |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def get_available_roles_success(cli_state, get_available_roles_response): |
| 51 | + cli_state.sdk.users.get_available_roles.return_value = get_available_roles_response |
| 52 | + |
| 53 | + |
| 54 | +def test_list_outputs_appropriate_columns(runner, cli_state, get_all_users_success): |
| 55 | + result = runner.invoke(cli, ["users", "list"], obj=cli_state) |
| 56 | + assert "userId" in result.output |
| 57 | + assert "userUid" in result.output |
| 58 | + assert "status" in result.output |
| 59 | + assert "username" in result.output |
| 60 | + assert "creationDate" in result.output |
| 61 | + assert "modificationDate" in result.output |
| 62 | + |
| 63 | + |
| 64 | +def test_list_users_calls_users_get_all_with_expected_role_id( |
| 65 | + runner, cli_state, get_available_roles_success, get_all_users_success |
| 66 | +): |
| 67 | + ROLE_NAME = "Customer Cloud Admin" |
| 68 | + runner.invoke(cli, ["users", "list", "--role-name", ROLE_NAME], obj=cli_state) |
| 69 | + cli_state.sdk.users.get_all.assert_called_once_with( |
| 70 | + active=None, org_uid=None, role_id="1234543" |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def test_list_users_calls_get_all_users_with_correct_parameters( |
| 75 | + runner, cli_state, get_all_users_success |
| 76 | +): |
| 77 | + org_uid = "TEST_ORG_UID" |
| 78 | + runner.invoke( |
| 79 | + cli, ["users", "list", "--org-uid", org_uid, "--active"], obj=cli_state |
| 80 | + ) |
| 81 | + cli_state.sdk.users.get_all.assert_called_once_with( |
| 82 | + active=True, org_uid=org_uid, role_id=None |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def test_list_users_when_given_inactive_uses_active_equals_false( |
| 87 | + runner, cli_state, get_available_roles_success, get_all_users_success |
| 88 | +): |
| 89 | + runner.invoke(cli, ["users", "list", "--inactive"], obj=cli_state) |
| 90 | + cli_state.sdk.users.get_all.assert_called_once_with( |
| 91 | + active=False, org_uid=None, role_id=None |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_list_users_when_given_active_and_inactive_raises_error( |
| 96 | + runner, cli_state, get_available_roles_success, get_all_users_success |
| 97 | +): |
| 98 | + result = runner.invoke( |
| 99 | + cli, ["users", "list", "--active", "--inactive"], obj=cli_state |
| 100 | + ) |
| 101 | + assert "Error: --inactive can't be used with: --active" in result.output |
| 102 | + |
| 103 | + |
| 104 | +def test_list_users_when_given_excluding_active_and_inactive_uses_active_equals_none( |
| 105 | + runner, cli_state, get_available_roles_success, get_all_users_success |
| 106 | +): |
| 107 | + runner.invoke(cli, ["users", "list"], obj=cli_state) |
| 108 | + cli_state.sdk.users.get_all.assert_called_once_with( |
| 109 | + active=None, org_uid=None, role_id=None |
| 110 | + ) |
0 commit comments