|
1 | 1 | import pytest |
| 2 | +from py42.exceptions import Py42ActiveLegalHoldError |
2 | 3 | from py42.exceptions import Py42InvalidEmailError |
3 | 4 | from py42.exceptions import Py42InvalidPasswordError |
4 | 5 | from py42.exceptions import Py42InvalidUsernameError |
@@ -130,6 +131,23 @@ def update_user_success(cli_state, update_user_response): |
130 | 131 | cli_state.sdk.users.update_user.return_value = update_user_response |
131 | 132 |
|
132 | 133 |
|
| 134 | +@pytest.fixture |
| 135 | +def deactivate_user_success(mocker, cli_state): |
| 136 | + cli_state.sdk.users.deactivate.return_value = create_mock_response(mocker) |
| 137 | + |
| 138 | + |
| 139 | +@pytest.fixture |
| 140 | +def deactivate_user_legal_hold_failure(mocker, cli_state): |
| 141 | + cli_state.sdk.users.deactivate.side_effect = Py42ActiveLegalHoldError( |
| 142 | + create_mock_http_error(mocker, status=400), "user", TEST_USER_ID |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.fixture |
| 147 | +def reactivate_user_success(mocker, cli_state): |
| 148 | + cli_state.sdk.users.deactivate.return_value = create_mock_response(mocker) |
| 149 | + |
| 150 | + |
133 | 151 | @pytest.fixture |
134 | 152 | def change_org_success(cli_state, change_org_response): |
135 | 153 | cli_state.sdk.users.change_org_assignment.return_value = change_org_response |
@@ -433,6 +451,33 @@ def test_update_when_py42_raises_invalid_password_outputs_error_message( |
433 | 451 | assert "Error: Invalid password." in result.output |
434 | 452 |
|
435 | 453 |
|
| 454 | +def test_deactivate_calls_deactivate_with_correct_parameters( |
| 455 | + runner, cli_state, get_user_id_success, deactivate_user_success |
| 456 | +): |
| 457 | + command = ["users", "deactivate", "test@example.com"] |
| 458 | + runner.invoke(cli, command, obj=cli_state) |
| 459 | + cli_state.sdk.users.deactivate.assert_called_once_with(TEST_USER_ID) |
| 460 | + |
| 461 | + |
| 462 | +def test_deactivate_when_user_on_legal_hold_outputs_expected_error_text( |
| 463 | + runner, cli_state, get_user_id_success, deactivate_user_legal_hold_failure |
| 464 | +): |
| 465 | + command = ["users", "deactivate", "test@example.com"] |
| 466 | + result = runner.invoke(cli, command, obj=cli_state) |
| 467 | + assert ( |
| 468 | + "Error: Cannot deactivate the user with ID 1234 as the user is involved in a legal hold matter." |
| 469 | + in result.output |
| 470 | + ) |
| 471 | + |
| 472 | + |
| 473 | +def test_reactivate_calls_reactivate_with_correct_parameters( |
| 474 | + runner, cli_state, get_user_id_success, deactivate_user_success |
| 475 | +): |
| 476 | + command = ["users", "reactivate", "test@example.com"] |
| 477 | + runner.invoke(cli, command, obj=cli_state) |
| 478 | + cli_state.sdk.users.reactivate.assert_called_once_with(TEST_USER_ID) |
| 479 | + |
| 480 | + |
436 | 481 | def test_bulk_update_uses_expected_arguments_when_only_some_are_passed( |
437 | 482 | runner, mocker, cli_state |
438 | 483 | ): |
@@ -655,3 +700,120 @@ def test_bulk_move_uses_handle_than_when_called_and_row_has_missing_username_err |
655 | 700 | assert worker_stats.increment_total_errors.call_count == 1 |
656 | 701 | # Ensure it does not try to get the username for the None user. |
657 | 702 | assert not cli_state.sdk.users.get_by_username.call_count |
| 703 | + |
| 704 | + |
| 705 | +def test_bulk_deactivate_uses_expected_arguments(runner, mocker, cli_state): |
| 706 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 707 | + with runner.isolated_filesystem(): |
| 708 | + with open("test_bulk_deactivate.csv", "w") as csv: |
| 709 | + csv.writelines(["username\n", f"{TEST_USERNAME}\n"]) |
| 710 | + runner.invoke( |
| 711 | + cli, |
| 712 | + ["users", "bulk", "deactivate", "test_bulk_deactivate.csv"], |
| 713 | + obj=cli_state, |
| 714 | + ) |
| 715 | + assert bulk_processor.call_args[0][1] == [ |
| 716 | + {"username": TEST_USERNAME, "deactivated": "False"} |
| 717 | + ] |
| 718 | + bulk_processor.assert_called_once() |
| 719 | + |
| 720 | + |
| 721 | +def test_bulk_deactivate_ignores_blank_lines(runner, mocker, cli_state): |
| 722 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 723 | + with runner.isolated_filesystem(): |
| 724 | + with open("test_bulk_deactivate.csv", "w") as csv: |
| 725 | + csv.writelines(["username\n\n\n", f"{TEST_USERNAME}\n\n\n"]) |
| 726 | + runner.invoke( |
| 727 | + cli, |
| 728 | + ["users", "bulk", "deactivate", "test_bulk_deactivate.csv"], |
| 729 | + obj=cli_state, |
| 730 | + ) |
| 731 | + assert bulk_processor.call_args[0][1] == [ |
| 732 | + {"username": TEST_USERNAME, "deactivated": "False"} |
| 733 | + ] |
| 734 | + bulk_processor.assert_called_once() |
| 735 | + |
| 736 | + |
| 737 | +def test_bulk_deactivate_uses_handler_that_when_encounters_error_increments_total_errors( |
| 738 | + runner, mocker, cli_state, worker_stats, get_users_response |
| 739 | +): |
| 740 | + lines = ["username\n", f"{TEST_USERNAME}\n"] |
| 741 | + |
| 742 | + def _get(username, *args, **kwargs): |
| 743 | + if username == "test@example.com": |
| 744 | + raise Exception("TEST") |
| 745 | + return get_users_response |
| 746 | + |
| 747 | + cli_state.sdk.users.get_by_username.side_effect = _get |
| 748 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 749 | + with runner.isolated_filesystem(): |
| 750 | + with open("test_bulk_deactivate.csv", "w") as csv: |
| 751 | + csv.writelines(lines) |
| 752 | + runner.invoke( |
| 753 | + cli, |
| 754 | + ["users", "bulk", "deactivate", "test_bulk_deactivate.csv"], |
| 755 | + obj=cli_state, |
| 756 | + ) |
| 757 | + handler = bulk_processor.call_args[0][0] |
| 758 | + handler(username="test@example.com") |
| 759 | + handler(username="not.test@example.com") |
| 760 | + assert worker_stats.increment_total_errors.call_count == 1 |
| 761 | + |
| 762 | + |
| 763 | +def test_bulk_reactivate_uses_expected_arguments(runner, mocker, cli_state): |
| 764 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 765 | + with runner.isolated_filesystem(): |
| 766 | + with open("test_bulk_reactivate.csv", "w") as csv: |
| 767 | + csv.writelines(["username\n", f"{TEST_USERNAME}\n"]) |
| 768 | + runner.invoke( |
| 769 | + cli, |
| 770 | + ["users", "bulk", "reactivate", "test_bulk_reactivate.csv"], |
| 771 | + obj=cli_state, |
| 772 | + ) |
| 773 | + assert bulk_processor.call_args[0][1] == [ |
| 774 | + {"username": TEST_USERNAME, "reactivated": "False"} |
| 775 | + ] |
| 776 | + bulk_processor.assert_called_once() |
| 777 | + |
| 778 | + |
| 779 | +def test_bulk_reactivate_ignores_blank_lines(runner, mocker, cli_state): |
| 780 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 781 | + with runner.isolated_filesystem(): |
| 782 | + with open("test_bulk_reactivate.csv", "w") as csv: |
| 783 | + csv.writelines(["username\n\n\n", f"{TEST_USERNAME}\n\n\n"]) |
| 784 | + runner.invoke( |
| 785 | + cli, |
| 786 | + ["users", "bulk", "reactivate", "test_bulk_reactivate.csv"], |
| 787 | + obj=cli_state, |
| 788 | + ) |
| 789 | + assert bulk_processor.call_args[0][1] == [ |
| 790 | + {"username": TEST_USERNAME, "reactivated": "False"} |
| 791 | + ] |
| 792 | + bulk_processor.assert_called_once() |
| 793 | + |
| 794 | + |
| 795 | +def test_bulk_reactivate_uses_handler_that_when_encounters_error_increments_total_errors( |
| 796 | + runner, mocker, cli_state, worker_stats, get_users_response |
| 797 | +): |
| 798 | + lines = ["username\n", f"{TEST_USERNAME}\n"] |
| 799 | + |
| 800 | + def _get(username, *args, **kwargs): |
| 801 | + if username == "test@example.com": |
| 802 | + raise Exception("TEST") |
| 803 | + |
| 804 | + return get_users_response |
| 805 | + |
| 806 | + cli_state.sdk.users.get_by_username.side_effect = _get |
| 807 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 808 | + with runner.isolated_filesystem(): |
| 809 | + with open("test_bulk_reactivate.csv", "w") as csv: |
| 810 | + csv.writelines(lines) |
| 811 | + runner.invoke( |
| 812 | + cli, |
| 813 | + ["users", "bulk", "reactivate", "test_bulk_reactivate.csv"], |
| 814 | + obj=cli_state, |
| 815 | + ) |
| 816 | + handler = bulk_processor.call_args[0][0] |
| 817 | + handler(username="test@example.com") |
| 818 | + handler(username="not.test@example.com") |
| 819 | + assert worker_stats.increment_total_errors.call_count == 1 |
0 commit comments