Update tests/test_utils_elog.py
Run CI Tests / test (push) Successful in 1m28s

This commit is contained in:
2025-08-06 21:44:04 +02:00
parent 06cf68aeae
commit b8e394b9f1
+30 -8
View File
@@ -7,6 +7,24 @@ from pathlib import Path
from slic.utils.elog import Elog, get_default_elog_instance
from unittest.mock import patch
# To havec the found password also and test it
def get_default_elog_password(url, **kwargs):
kwargs.setdefault("user", getuser())
user = kwargs["user"]
if "password" not in kwargs:
try:
home = Path.home()
fn = home / ".elog_psi"
with fn.open() as f:
pw = f.read().strip()
except Exception:
print(f"Enter elog password for user: {user}")
pw = getpass()
kwargs["password"] = pw
return kwargs["password"]
def get_test_elog():
return Elog("http://localhost:8080/demo", user="robot", password="testpassword")
@@ -21,8 +39,9 @@ def test_get_default_elog_instance_with_direct_password_and_real_check():
assert returned_user == user
assert hasattr(elog_instance, "post")
# Perform a real GET request to confirm it's accessible with this password
r = requests.get(url, auth=(user, password))
pw = get_default_elog_password(url, user=user, password=password)
# Perform a real GET request to confirm it's accessible with the found password
r = requests.get(url, auth=(user, pw))
assert r.status_code == 200
@patch("slic.utils.elog.getpass")
@@ -31,6 +50,7 @@ def test_get_default_elog_instance_asks_password_and_opens(mock_home, mock_getpa
mock_home.return_value = Path("/does/not/exist")
mock_getpass.return_value = "testpassword"
password = mock_getpass.return_value
url = "http://localhost:8080/demo"
user = "robot"
@@ -40,8 +60,9 @@ def test_get_default_elog_instance_asks_password_and_opens(mock_home, mock_getpa
assert returned_user == user
assert hasattr(elog_instance, "post")
# Try a real GET to verify access works
r = requests.get(url, auth=(user, password))
pw = get_default_elog_password(url, user=user)
r = requests.get(url, auth=(user, pw))
assert r.status_code == 200
@@ -69,9 +90,10 @@ def test_get_default_elog_with_path_home(mock_home, mock_getuser):
assert returned_user == fake_user
assert hasattr(elog_instance, "post")
# Try a real GET request
r = requests.get(url, auth=(user, password))
assert r.status_code == 200
pw = get_default_elog_password(url)
r = requests.get(url, auth=(user, pw))
assert r.status_code == 200
# Cleanup
pw_file.unlink()
@@ -85,7 +107,7 @@ def test_post():
message = "This is a message"
author = "robot"
resp = elog.post(message, Title=title, Author=author)
resp = elog.post(message, Title=title, Author=author, Category="General", Type="Test")
assert resp is not None
url = f"http://localhost:8080/demo/{resp}"