|
| 1 | +import os |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from exchange.exchange import Exchange |
| 5 | +from exchange.invalid_choice_error import InvalidChoiceError |
| 6 | +from exchange.providers.base import MissingProviderEnvVariableError |
| 7 | +import pytest |
| 8 | + |
| 9 | +from goose.notifier import Notifier |
| 10 | +from goose.profile import Profile |
| 11 | +from goose.utils._create_exchange import create_exchange |
| 12 | + |
| 13 | +TEST_PROFILE = MagicMock(spec=Profile) |
| 14 | +TEST_EXCHANGE = MagicMock(spec=Exchange) |
| 15 | +TEST_NOTIFIER = MagicMock(spec=Notifier) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_print(): |
| 20 | + with patch("goose.utils._create_exchange.print") as mock_print: |
| 21 | + yield mock_print |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_prompt(): |
| 26 | + with patch("goose.utils._create_exchange.prompt") as mock_prompt: |
| 27 | + yield mock_prompt |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mock_confirm(): |
| 32 | + with patch("goose.utils._create_exchange.confirm") as mock_confirm: |
| 33 | + yield mock_confirm |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def mock_sys_exit(): |
| 38 | + with patch("sys.exit") as mock_exit: |
| 39 | + yield mock_exit |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def mock_keyring_get_password(): |
| 44 | + with patch("keyring.get_password") as mock_get_password: |
| 45 | + yield mock_get_password |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def mock_keyring_set_password(): |
| 50 | + with patch("keyring.set_password") as mock_set_password: |
| 51 | + yield mock_set_password |
| 52 | + |
| 53 | + |
| 54 | +def test_create_exchange_success(mock_print): |
| 55 | + with patch("goose.utils._create_exchange.build_exchange", return_value=TEST_EXCHANGE): |
| 56 | + assert create_exchange(profile=TEST_PROFILE, notifier=TEST_NOTIFIER) == TEST_EXCHANGE |
| 57 | + |
| 58 | + |
| 59 | +def test_create_exchange_fail_with_invalid_choice_error(mock_print, mock_sys_exit): |
| 60 | + expected_error = InvalidChoiceError( |
| 61 | + attribute_name="provider", attribute_value="wrong_provider", available_values=["openai"] |
| 62 | + ) |
| 63 | + with patch("goose.utils._create_exchange.build_exchange", side_effect=expected_error): |
| 64 | + create_exchange(profile=TEST_PROFILE, notifier=TEST_NOTIFIER) |
| 65 | + |
| 66 | + assert "Unknown provider: wrong_provider. Available providers: openai" in mock_print.call_args_list[0][0][0] |
| 67 | + mock_sys_exit.assert_called_once_with(1) |
| 68 | + |
| 69 | + |
| 70 | +class TestWhenProviderEnvVarNotFound: |
| 71 | + API_KEY_ENV_VAR = "OPENAI_API_KEY" |
| 72 | + API_KEY_ENV_VALUE = "api_key_value" |
| 73 | + PROVIDER_NAME = "openai" |
| 74 | + SERVICE_NAME = "goose" |
| 75 | + EXPECTED_ERROR = MissingProviderEnvVariableError(env_variable=API_KEY_ENV_VAR, provider=PROVIDER_NAME) |
| 76 | + |
| 77 | + def test_create_exchange_get_api_key_from_keychain( |
| 78 | + self, mock_print, mock_sys_exit, mock_keyring_get_password, mock_keyring_set_password |
| 79 | + ): |
| 80 | + self._clean_env() |
| 81 | + with patch("goose.utils._create_exchange.build_exchange", side_effect=[self.EXPECTED_ERROR, TEST_EXCHANGE]): |
| 82 | + mock_keyring_get_password.return_value = self.API_KEY_ENV_VALUE |
| 83 | + |
| 84 | + assert create_exchange(profile=TEST_PROFILE, notifier=TEST_NOTIFIER) == TEST_EXCHANGE |
| 85 | + |
| 86 | + assert os.environ[self.API_KEY_ENV_VAR] == self.API_KEY_ENV_VALUE |
| 87 | + mock_keyring_get_password.assert_called_once_with(self.SERVICE_NAME, self.API_KEY_ENV_VAR) |
| 88 | + mock_print.assert_called_once_with( |
| 89 | + f"Using {self.API_KEY_ENV_VAR} value for {self.PROVIDER_NAME} from your keychain" |
| 90 | + ) |
| 91 | + mock_sys_exit.assert_not_called() |
| 92 | + mock_keyring_set_password.assert_not_called() |
| 93 | + |
| 94 | + def test_create_exchange_ask_api_key_and_user_set_in_keychain( |
| 95 | + self, mock_prompt, mock_confirm, mock_sys_exit, mock_keyring_get_password, mock_keyring_set_password, mock_print |
| 96 | + ): |
| 97 | + self._clean_env() |
| 98 | + with patch("goose.utils._create_exchange.build_exchange", side_effect=[self.EXPECTED_ERROR, TEST_EXCHANGE]): |
| 99 | + mock_keyring_get_password.return_value = None |
| 100 | + mock_prompt.return_value = self.API_KEY_ENV_VALUE |
| 101 | + mock_confirm.return_value = True |
| 102 | + |
| 103 | + assert create_exchange(profile=TEST_NOTIFIER, notifier=TEST_NOTIFIER) == TEST_EXCHANGE |
| 104 | + |
| 105 | + assert os.environ[self.API_KEY_ENV_VAR] == self.API_KEY_ENV_VALUE |
| 106 | + mock_keyring_set_password.assert_called_once_with( |
| 107 | + self.SERVICE_NAME, self.API_KEY_ENV_VAR, self.API_KEY_ENV_VALUE |
| 108 | + ) |
| 109 | + mock_confirm.assert_called_once_with( |
| 110 | + f"Would you like to save the {self.API_KEY_ENV_VAR} value to your keychain?" |
| 111 | + ) |
| 112 | + mock_print.assert_called_once_with( |
| 113 | + f"Saved {self.API_KEY_ENV_VAR} to your key_chain. " |
| 114 | + + f"service_name: goose, user_name: {self.API_KEY_ENV_VAR}" |
| 115 | + ) |
| 116 | + mock_sys_exit.assert_not_called() |
| 117 | + |
| 118 | + def test_create_exchange_ask_api_key_and_user_not_set_in_keychain( |
| 119 | + self, mock_prompt, mock_confirm, mock_sys_exit, mock_keyring_get_password, mock_keyring_set_password |
| 120 | + ): |
| 121 | + self._clean_env() |
| 122 | + with patch("goose.utils._create_exchange.build_exchange", side_effect=[self.EXPECTED_ERROR, TEST_EXCHANGE]): |
| 123 | + mock_keyring_get_password.return_value = None |
| 124 | + mock_prompt.return_value = self.API_KEY_ENV_VALUE |
| 125 | + mock_confirm.return_value = False |
| 126 | + |
| 127 | + assert create_exchange(profile=TEST_NOTIFIER, notifier=TEST_NOTIFIER) == TEST_EXCHANGE |
| 128 | + |
| 129 | + assert os.environ[self.API_KEY_ENV_VAR] == self.API_KEY_ENV_VALUE |
| 130 | + mock_keyring_set_password.assert_not_called() |
| 131 | + mock_sys_exit.assert_not_called() |
| 132 | + |
| 133 | + def test_create_exchange_fails_when_user_not_provide_api_key( |
| 134 | + self, mock_prompt, mock_confirm, mock_sys_exit, mock_keyring_get_password, mock_print |
| 135 | + ): |
| 136 | + self._clean_env() |
| 137 | + with patch("goose.utils._create_exchange.build_exchange", side_effect=self.EXPECTED_ERROR): |
| 138 | + mock_keyring_get_password.return_value = None |
| 139 | + mock_prompt.return_value = None |
| 140 | + mock_confirm.return_value = False |
| 141 | + |
| 142 | + create_exchange(profile=TEST_NOTIFIER, notifier=TEST_NOTIFIER) |
| 143 | + |
| 144 | + assert ( |
| 145 | + "Please set the required environment variable to continue." |
| 146 | + in mock_print.call_args_list[0][0][0].renderable |
| 147 | + ) |
| 148 | + mock_sys_exit.assert_called_once_with(1) |
| 149 | + |
| 150 | + def _clean_env(self): |
| 151 | + os.environ.pop(self.API_KEY_ENV_VAR, None) |
0 commit comments