155 lines
4.4 KiB
Python
155 lines
4.4 KiB
Python
import os
|
|
import sys
|
|
import tempfile
|
|
import requests
|
|
import elog
|
|
import pytest
|
|
from getpass import getuser, getpass
|
|
from pathlib import Path
|
|
from slic.utils.elog import Elog, get_default_elog_instance
|
|
from unittest.mock import patch
|
|
'''
|
|
|
|
# Helper pour extraire uniquement le mot de passe comme dans la vraie fonction
|
|
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 test_post_local():
|
|
logbook = elog.open(
|
|
hostname="http://localhost",
|
|
port=8080,
|
|
user="robot",
|
|
password="testpassword",
|
|
use_ssl=False,
|
|
logbook="demo"
|
|
)
|
|
attributes = {
|
|
"Author": "robot",
|
|
"Subject": "Test simple",
|
|
"Category": "General",
|
|
"Type": "Note"
|
|
}
|
|
message = "Hello from local test"
|
|
msg_id = logbook.post(message, attributes=attributes, encoding="HTML")
|
|
print("✅ Posted with msg_id =", msg_id)
|
|
|
|
|
|
# Connexion basique avec credentials connus
|
|
def get_test_elog():
|
|
return Elog("http://localhost:8080/demo", user="robot", password="testpassword")
|
|
|
|
def test_get_default_elog_instance_with_direct_password_and_real_check():
|
|
url = "http://localhost:8080/demo"
|
|
user = "robot"
|
|
password = "testpassword"
|
|
|
|
elog_instance, returned_user = get_default_elog_instance(url, user=user, password=password)
|
|
|
|
assert returned_user == user
|
|
assert hasattr(elog_instance, "post")
|
|
|
|
r = requests.get(url, auth=(user, password))
|
|
assert r.status_code == 200
|
|
|
|
@patch("slic.utils.elog.getpass")
|
|
@patch("slic.utils.elog.Path.home")
|
|
def test_get_default_elog_instance_asks_password_and_opens(mock_home, mock_getpass):
|
|
mock_home.return_value = Path("/does/not/exist") # Fausse home → lecture échoue
|
|
mock_getpass.return_value = "testpassword"
|
|
|
|
url = "http://localhost:8080/demo"
|
|
user = "robot"
|
|
|
|
elog_instance, returned_user = get_default_elog_instance(url, user=user)
|
|
|
|
assert returned_user == user
|
|
assert hasattr(elog_instance, "post")
|
|
|
|
r = requests.get(url, auth=(user, mock_getpass.return_value))
|
|
assert r.status_code == 200
|
|
|
|
|
|
@patch("slic.utils.elog.getpass")
|
|
@patch("slic.utils.elog.getuser")
|
|
@patch("slic.utils.elog.Path.home")
|
|
def test_get_default_elog_with_path_home(mock_home, mock_getuser, mock_getpass):
|
|
fake_user = "robot"
|
|
fake_pw = "testpassword"
|
|
mock_getuser.return_value = fake_user
|
|
mock_getpass.return_value = fake_pw # fallback safety
|
|
|
|
tmp_home = Path("/tmp/fake_home_for_robot")
|
|
tmp_home.mkdir(parents=True, exist_ok=True)
|
|
pw_file = tmp_home / ".elog_psi"
|
|
pw_file.write_text(fake_pw)
|
|
mock_home.return_value = tmp_home
|
|
|
|
url = "http://localhost:8080/demo"
|
|
|
|
try:
|
|
elog_instance, returned_user = get_default_elog_instance(url)
|
|
|
|
assert returned_user == fake_user
|
|
assert hasattr(elog_instance, "post")
|
|
|
|
r = requests.get(url, auth=(fake_user, fake_pw))
|
|
assert r.status_code == 200
|
|
|
|
finally:
|
|
pw_file.unlink(missing_ok=True)
|
|
tmp_home.rmdir()
|
|
|
|
|
|
def test_post():
|
|
elog = get_test_elog()
|
|
|
|
title = "AUTHOR_OVERRIDE_TEST"
|
|
text = "This is a message"
|
|
author = "robot"
|
|
|
|
elog.post(text, Author=author)
|
|
|
|
url = "http://localhost:8080/demo"
|
|
html = requests.get(url).text
|
|
|
|
assert text in html, "Message not found"
|
|
assert author in html, f"Author not found"
|
|
|
|
|
|
@patch("slic.utils.elog.Screenshot")
|
|
def test_screenshot(mock_screenshot_class):
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
|
fake_path = tmp.name
|
|
tmp.write(b"fake image data")
|
|
|
|
mock_instance = mock_screenshot_class.return_value
|
|
mock_instance.shoot.return_value = [fake_path]
|
|
|
|
elog = get_test_elog()
|
|
|
|
test_msg = "SCREENSHOT_INTEGRATION_TEST_MSG_456"
|
|
entry_id = elog.screenshot(message=test_msg)
|
|
|
|
assert entry_id is not None
|
|
|
|
url = f"http://localhost:8080/demo/{entry_id}"
|
|
html = requests.get(url).text
|
|
|
|
filename = os.path.basename(fake_path)
|
|
assert filename in html, f"Attachment '{filename}' not found in post HTML"
|
|
|
|
os.remove(fake_path)
|
|
''' |