Skip to content

Commit

Permalink
Add support for configured hana username/password
Browse files Browse the repository at this point in the history
Add unit test case for invalid login
  • Loading branch information
NeonDaniel committed Nov 11, 2024
1 parent 838ea14 commit e9e808e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
17 changes: 11 additions & 6 deletions neon_utils/hana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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}")
Expand Down
14 changes: 11 additions & 3 deletions tests/hana_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e9e808e

Please sign in to comment.