diff --git a/neon_utils/hana_utils.py b/neon_utils/hana_utils.py index b4d16666..d2edb345 100644 --- a/neon_utils/hana_utils.py +++ b/neon_utils/hana_utils.py @@ -25,6 +25,8 @@ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +from _socket import gethostname +from datetime import datetime import requests import json @@ -33,6 +35,7 @@ from os import makedirs from os.path import join, isfile, isdir, dirname from time import time + from ovos_utils.log import LOG from ovos_utils.xdg_utils import xdg_cache_home @@ -91,20 +94,22 @@ def _init_client(backend_address: str): _headers = {"Authorization": f"Bearer {_client_config['access_token']}"} -def _get_token(backend_address: str, username: str = "guest", - password: str = "password"): +def _get_token(backend_address: str): """ Get new auth tokens from the specified server. This will cache the returned token, overwriting any previous data at the cache path. @param backend_address: Hana server URL to connect to - @param username: Username to authorize - @param password: Password for specified username """ + from ovos_config.config import Configuration global _client_config - # TODO: username/password from configuration + hana_config = Configuration().get('hana', {}) + username = hana_config.get("username") or "guest" + password = hana_config.get("password") or "password" + token_name = f"{gethostname()} {datetime.utcnow().isoformat()}" resp = requests.post(f"{backend_address}/auth/login", json={"username": username, - "password": password}) + "password": password, + "token_name": token_name}) if not resp.ok: raise ServerException(f"Error logging into {backend_address}. " f"{resp.status_code}: {resp.text}") diff --git a/tests/hana_util_tests.py b/tests/hana_util_tests.py index 2944c046..25f50384 100644 --- a/tests/hana_util_tests.py +++ b/tests/hana_util_tests.py @@ -96,18 +96,26 @@ def test_request_backend_refresh_token(self, refresh_token, config_path): neon_utils.hana_utils._client_config = real_client_config @patch("neon_utils.hana_utils._get_client_config_path") - def test_00_get_token(self, config_path): + @patch("ovos_config.configuration.Configuration") + def test_00_get_token(self, config, config_path): + config.return_value = {} config_path.return_value = self.test_path from neon_utils.hana_utils import _get_token - # Test valid request + # Test valid default request _get_token(self.test_server) from neon_utils.hana_utils import _client_config self.assertTrue(isfile(self.test_path)) with open(self.test_path) as f: credentials_on_disk = json.load(f) self.assertEqual(credentials_on_disk, _client_config) - # TODO: Test invalid request, rate-limited request + + # Test with configured invalid login + config.return_value = {"hana": {"username": "guest", + "password": "password"}} + from neon_utils.hana_utils import ServerException + with self.assertRaises(ServerException): + _get_token(self.test_server) @patch("neon_utils.hana_utils._get_client_config_path") @patch("neon_utils.hana_utils._get_token")