|
| 1 | +from unittest.mock import call |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from compiler_admin import RESULT_FAILURE, RESULT_SUCCESS |
| 6 | +from compiler_admin.commands.user.backupcodes import backupcodes |
| 7 | +from compiler_admin.commands.user.reactivate import __name__ as MODULE |
| 8 | +from compiler_admin.commands.user.reactivate import reactivate |
| 9 | +from compiler_admin.commands.user.reset import reset |
| 10 | +from compiler_admin.services.google import GROUP_STAFF, GROUP_TEAM, OU_CONTRACTORS, OU_STAFF |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_ctx_forward(mocker): |
| 15 | + """Mock click.Context.forward.""" |
| 16 | + return mocker.patch("click.Context.forward") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_input_yes(mock_input_yes): |
| 21 | + return mock_input_yes(MODULE) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_input_no(mock_input_no): |
| 26 | + return mock_input_no(MODULE) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_google_CallGAMCommand(mock_google_CallGAMCommand): |
| 31 | + return mock_google_CallGAMCommand(MODULE) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def mock_google_add_user_to_group(mock_google_add_user_to_group): |
| 36 | + return mock_google_add_user_to_group(MODULE) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def mock_google_move_user_ou(mock_google_move_user_ou): |
| 41 | + return mock_google_move_user_ou(MODULE) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def mock_google_user_exists(mock_google_user_exists): |
| 46 | + return mock_google_user_exists(MODULE) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def mock_google_user_is_deactivated(mock_module_name): |
| 51 | + return mock_module_name("user_is_deactivated")(MODULE) |
| 52 | + |
| 53 | + |
| 54 | +def test_reactivate_user_does_not_exist(cli_runner, mock_google_user_exists, mock_google_CallGAMCommand): |
| 55 | + mock_google_user_exists.return_value = False |
| 56 | + |
| 57 | + result = cli_runner.invoke(reactivate, ["username"]) |
| 58 | + |
| 59 | + assert result.exit_code == RESULT_FAILURE |
| 60 | + assert result.exception |
| 61 | + assert "User does not exist: username@compiler.la" in result.output |
| 62 | + mock_google_CallGAMCommand.assert_not_called() |
| 63 | + |
| 64 | + |
| 65 | +def test_reactivate_user_is_not_deactivated(cli_runner, mock_google_user_exists, mock_google_user_is_deactivated): |
| 66 | + mock_google_user_exists.return_value = True |
| 67 | + mock_google_user_is_deactivated.return_value = False |
| 68 | + |
| 69 | + result = cli_runner.invoke(reactivate, ["username"]) |
| 70 | + |
| 71 | + assert result.exit_code == RESULT_FAILURE |
| 72 | + assert result.exception |
| 73 | + assert "User is not deactivated, cannot reactivate" in result.output |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.usefixtures("mock_input_no") |
| 77 | +def test_reactivate_confirm_no(cli_runner, mock_google_user_exists, mock_google_user_is_deactivated): |
| 78 | + mock_google_user_exists.return_value = True |
| 79 | + mock_google_user_is_deactivated.return_value = True |
| 80 | + |
| 81 | + result = cli_runner.invoke(reactivate, ["username"]) |
| 82 | + |
| 83 | + assert result.exit_code == RESULT_SUCCESS |
| 84 | + assert not result.exception |
| 85 | + assert "Aborting reactivation" in result.output |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.usefixtures("mock_input_yes") |
| 89 | +def test_reactivate_confirm_yes( |
| 90 | + cli_runner, |
| 91 | + mock_google_user_exists, |
| 92 | + mock_google_user_is_deactivated, |
| 93 | + mock_google_add_user_to_group, |
| 94 | + mock_google_move_user_ou, |
| 95 | + mock_ctx_forward, |
| 96 | +): |
| 97 | + mock_google_user_exists.return_value = True |
| 98 | + mock_google_user_is_deactivated.return_value = True |
| 99 | + |
| 100 | + result = cli_runner.invoke(reactivate, ["username"]) |
| 101 | + |
| 102 | + assert result.exit_code == RESULT_SUCCESS |
| 103 | + assert not result.exception |
| 104 | + mock_google_add_user_to_group.assert_called_once_with("username@compiler.la", GROUP_TEAM) |
| 105 | + mock_google_move_user_ou.assert_called_once_with("username@compiler.la", OU_CONTRACTORS) |
| 106 | + mock_ctx_forward.assert_has_calls([call(reset), call(backupcodes)]) |
| 107 | + |
| 108 | + |
| 109 | +def test_reactivate_force( |
| 110 | + cli_runner, |
| 111 | + mock_google_user_exists, |
| 112 | + mock_google_user_is_deactivated, |
| 113 | + mock_google_add_user_to_group, |
| 114 | + mock_google_move_user_ou, |
| 115 | + mock_ctx_forward, |
| 116 | +): |
| 117 | + mock_google_user_exists.return_value = True |
| 118 | + mock_google_user_is_deactivated.return_value = True |
| 119 | + |
| 120 | + result = cli_runner.invoke(reactivate, ["--force", "username"]) |
| 121 | + |
| 122 | + assert result.exit_code == RESULT_SUCCESS |
| 123 | + assert not result.exception |
| 124 | + mock_google_add_user_to_group.assert_called_once_with("username@compiler.la", GROUP_TEAM) |
| 125 | + mock_google_move_user_ou.assert_called_once_with("username@compiler.la", OU_CONTRACTORS) |
| 126 | + mock_ctx_forward.assert_has_calls([call(reset), call(backupcodes)]) |
| 127 | + |
| 128 | + |
| 129 | +def test_reactivate_staff( |
| 130 | + cli_runner, |
| 131 | + mock_google_user_exists, |
| 132 | + mock_google_user_is_deactivated, |
| 133 | + mock_google_add_user_to_group, |
| 134 | + mock_google_move_user_ou, |
| 135 | + mock_ctx_forward, |
| 136 | +): |
| 137 | + mock_google_user_exists.return_value = True |
| 138 | + mock_google_user_is_deactivated.return_value = True |
| 139 | + |
| 140 | + result = cli_runner.invoke(reactivate, ["--force", "--staff", "username"]) |
| 141 | + |
| 142 | + assert result.exit_code == RESULT_SUCCESS |
| 143 | + assert not result.exception |
| 144 | + mock_google_add_user_to_group.assert_has_calls( |
| 145 | + [call("username@compiler.la", GROUP_TEAM), call("username@compiler.la", GROUP_STAFF)] |
| 146 | + ) |
| 147 | + mock_google_move_user_ou.assert_called_once_with("username@compiler.la", OU_STAFF) |
| 148 | + mock_ctx_forward.assert_has_calls([call(reset), call(backupcodes)]) |
| 149 | + |
| 150 | + |
| 151 | +def test_reactivate_recovery_info( |
| 152 | + cli_runner, |
| 153 | + mock_google_user_exists, |
| 154 | + mock_google_user_is_deactivated, |
| 155 | + mock_google_CallGAMCommand, |
| 156 | + mock_ctx_forward, |
| 157 | +): |
| 158 | + mock_google_user_exists.return_value = True |
| 159 | + mock_google_user_is_deactivated.return_value = True |
| 160 | + |
| 161 | + result = cli_runner.invoke( |
| 162 | + reactivate, ["--force", "--recovery-email=foo@bar.com", "--recovery-phone=555-555-5555", "username"] |
| 163 | + ) |
| 164 | + |
| 165 | + assert result.exit_code == RESULT_SUCCESS |
| 166 | + assert not result.exception |
| 167 | + mock_google_CallGAMCommand.assert_has_calls( |
| 168 | + [ |
| 169 | + call(("update", "user", "username@compiler.la", "recoveryemail", "foo@bar.com")), |
| 170 | + call(("update", "user", "username@compiler.la", "recoveryphone", "555-555-5555")), |
| 171 | + ] |
| 172 | + ) |
| 173 | + mock_ctx_forward.assert_has_calls([call(reset), call(backupcodes)]) |
0 commit comments