|
| 1 | +from argparse import Namespace |
| 2 | +import pytest |
| 3 | + |
| 4 | +from compiler_admin import RESULT_FAILURE, RESULT_SUCCESS |
| 5 | +from compiler_admin.commands.user.alumni import alumni, __name__ as MODULE |
| 6 | +from compiler_admin.services.google import OU_ALUMNI |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mock_commands_reset(mock_commands_reset): |
| 11 | + return mock_commands_reset(MODULE) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_input_yes(mock_input): |
| 16 | + fix = mock_input(MODULE) |
| 17 | + fix.return_value = "y" |
| 18 | + return fix |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_input_no(mock_input): |
| 23 | + fix = mock_input(MODULE) |
| 24 | + fix.return_value = "n" |
| 25 | + return fix |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def mock_google_CallGAMCommand(mock_google_CallGAMCommand): |
| 30 | + return mock_google_CallGAMCommand(MODULE) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def mock_google_move_user_ou(mock_google_move_user_ou): |
| 35 | + return mock_google_move_user_ou(MODULE) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def mock_google_remove_user_from_group(mock_google_remove_user_from_group): |
| 40 | + return mock_google_remove_user_from_group(MODULE) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def mock_google_user_exists(mock_google_user_exists): |
| 45 | + return mock_google_user_exists(MODULE) |
| 46 | + |
| 47 | + |
| 48 | +def test_alumni_username_required(): |
| 49 | + args = Namespace() |
| 50 | + |
| 51 | + with pytest.raises(ValueError, match="username is required"): |
| 52 | + alumni(args) |
| 53 | + |
| 54 | + |
| 55 | +def test_alumni_user_does_not_exists(mock_google_user_exists, mock_google_CallGAMCommand): |
| 56 | + mock_google_user_exists.return_value = False |
| 57 | + |
| 58 | + args = Namespace(username="username") |
| 59 | + res = alumni(args) |
| 60 | + |
| 61 | + assert res == RESULT_FAILURE |
| 62 | + mock_google_CallGAMCommand.assert_not_called() |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.usefixtures("mock_input_yes") |
| 66 | +def test_alumni_confirm_yes( |
| 67 | + mock_google_user_exists, mock_google_CallGAMCommand, mock_commands_reset, mock_google_move_user_ou |
| 68 | +): |
| 69 | + mock_google_user_exists.return_value = True |
| 70 | + |
| 71 | + args = Namespace(username="username", force=False) |
| 72 | + res = alumni(args) |
| 73 | + |
| 74 | + assert res == RESULT_SUCCESS |
| 75 | + mock_google_CallGAMCommand.assert_called() |
| 76 | + mock_google_move_user_ou.assert_called_once_with("username@compiler.la", OU_ALUMNI) |
| 77 | + mock_commands_reset.assert_called_once_with(args) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.usefixtures("mock_input_no") |
| 81 | +def test_alumni_confirm_no(mock_google_user_exists, mock_google_CallGAMCommand, mock_commands_reset, mock_google_move_user_ou): |
| 82 | + mock_google_user_exists.return_value = True |
| 83 | + |
| 84 | + args = Namespace(username="username", force=False) |
| 85 | + res = alumni(args) |
| 86 | + |
| 87 | + assert res == RESULT_SUCCESS |
| 88 | + mock_google_CallGAMCommand.assert_not_called() |
| 89 | + mock_commands_reset.assert_not_called() |
| 90 | + mock_google_move_user_ou.assert_not_called() |
| 91 | + |
| 92 | + |
| 93 | +def test_alumni_force(mock_google_user_exists, mock_google_CallGAMCommand, mock_commands_reset, mock_google_move_user_ou): |
| 94 | + mock_google_user_exists.return_value = True |
| 95 | + |
| 96 | + args = Namespace(username="username", force=True) |
| 97 | + res = alumni(args) |
| 98 | + |
| 99 | + assert res == RESULT_SUCCESS |
| 100 | + mock_google_CallGAMCommand.assert_called() |
| 101 | + mock_google_move_user_ou.assert_called_once_with("username@compiler.la", OU_ALUMNI) |
| 102 | + mock_commands_reset.assert_called_once_with(args) |
0 commit comments