|
| 1 | +from argparse import Namespace |
| 2 | +import pytest |
| 3 | + |
| 4 | +from compiler_admin.commands import RESULT_FAILURE, RESULT_SUCCESS |
| 5 | +from compiler_admin.commands.reset_password import reset_password, __name__ as MODULE |
| 6 | +from compiler_admin.services.google import USER_HELLO |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mock_google_user_exists(mock_google_user_exists): |
| 11 | + return mock_google_user_exists(MODULE) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_commands_signout(mock_commands_signout): |
| 16 | + return mock_commands_signout(MODULE) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_google_CallGAMCommand(mock_google_CallGAMCommand): |
| 21 | + return mock_google_CallGAMCommand(MODULE) |
| 22 | + |
| 23 | + |
| 24 | +def test_reset_password_user_username_required(): |
| 25 | + args = Namespace() |
| 26 | + |
| 27 | + with pytest.raises(ValueError, match="username is required"): |
| 28 | + reset_password(args) |
| 29 | + |
| 30 | + |
| 31 | +def test_reset_password_user_does_not_exist(mock_google_user_exists): |
| 32 | + mock_google_user_exists.return_value = False |
| 33 | + |
| 34 | + args = Namespace(username="username") |
| 35 | + res = reset_password(args) |
| 36 | + |
| 37 | + assert res == RESULT_FAILURE |
| 38 | + |
| 39 | + |
| 40 | +def test_reset_password_user_exists(mock_google_user_exists, mock_google_CallGAMCommand, mock_commands_signout): |
| 41 | + mock_google_user_exists.return_value = True |
| 42 | + |
| 43 | + args = Namespace(username="username") |
| 44 | + res = reset_password(args) |
| 45 | + |
| 46 | + assert res == RESULT_SUCCESS |
| 47 | + |
| 48 | + mock_google_CallGAMCommand.assert_called_once() |
| 49 | + call_args = " ".join(mock_google_CallGAMCommand.call_args[0][0]) |
| 50 | + assert "update user" in call_args |
| 51 | + assert "password random changepassword" in call_args |
| 52 | + |
| 53 | + mock_commands_signout.assert_called_once_with(args) |
| 54 | + |
| 55 | + |
| 56 | +def test_reset_password_notify(mock_google_user_exists, mock_google_CallGAMCommand, mock_commands_signout): |
| 57 | + mock_google_user_exists.return_value = True |
| 58 | + |
| 59 | + args = Namespace( username="username", notify="[email protected]") |
| 60 | + res = reset_password(args) |
| 61 | + |
| 62 | + assert res == RESULT_SUCCESS |
| 63 | + |
| 64 | + mock_google_CallGAMCommand.assert_called_once() |
| 65 | + call_args = " ".join(mock_google_CallGAMCommand.call_args[0][0]) |
| 66 | + assert "update user" in call_args |
| 67 | + assert "password random changepassword" in call_args |
| 68 | + assert f"notify [email protected] from {USER_HELLO}" in call_args |
| 69 | + |
| 70 | + mock_commands_signout.assert_called_once_with(args) |
0 commit comments