This commit is contained in:
+129
-13
@@ -1,18 +1,134 @@
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import requests
|
||||
import getpass
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from slic.utils.elog import Elog, get_default_elog_instance
|
||||
from unittest.mock import patch
|
||||
|
||||
# ✅ Forcer l'ajout de ~/.local/bin dans le PATH pour ce test
|
||||
os.environ["PATH"] = os.path.expanduser("~/.local/bin") + ":" + os.environ["PATH"]
|
||||
|
||||
def test_elogd_is_available():
|
||||
path = os.environ["PATH"]
|
||||
elogd_path = os.path.expanduser("~/.local/bin/elogd")
|
||||
which = shutil.which("elogd")
|
||||
def get_test_elog():
|
||||
return Elog("http://localhost:8080/demo", user="robot", password="testpassword")
|
||||
|
||||
# ✅ Impressions visibles dans les logs GitHub Actions
|
||||
sys.stderr.write(f"\n[DEBUG] PATH = {path}\n")
|
||||
sys.stderr.write(f"[DEBUG] elogd exists: {os.path.exists(elogd_path)}\n")
|
||||
sys.stderr.write(f"[DEBUG] which elogd = {which}\n")
|
||||
def test_get_default_elog_instance_with_direct_password_and_real_check():
|
||||
url = "http://localhost:8080/demo"
|
||||
user = "robot"
|
||||
password = "testpassword"
|
||||
|
||||
assert which is not None, "elogd is not installed or not in PATH"
|
||||
elog_instance, returned_user = get_default_elog_instance(url, user=user, password=password)
|
||||
|
||||
assert returned_user == user
|
||||
assert hasattr(elog_instance, "post")
|
||||
|
||||
# Perform a real GET request to confirm it's accessible with this password
|
||||
session = elog_instance.session # The elog instance should have an HTTP session
|
||||
r = session.get(url)
|
||||
assert r.status_code == 200
|
||||
assert "demo" in r.text.lower()
|
||||
|
||||
@patch("your_module.elog_wrapper.getpass")
|
||||
@patch("your_module.elog_wrapper.Path.home")
|
||||
def test_get_default_elog_instance_asks_password_and_opens(mock_home, mock_getpass):
|
||||
|
||||
mock_home.return_value = Path("/does/not/exist")
|
||||
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")
|
||||
|
||||
# Try a real GET to verify access works
|
||||
session = elog_instance.session
|
||||
r = session.get(url)
|
||||
assert r.status_code == 200
|
||||
assert "demo" in r.text.lower()
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from your_module.elog_wrapper import get_default_elog_instance
|
||||
|
||||
|
||||
@patch("your_module.elog_wrapper.getuser")
|
||||
@patch("your_module.elog_wrapper.Path.home")
|
||||
def test_get_default_elog_with_path_home(mock_home, mock_getuser):
|
||||
# Setup
|
||||
fake_user = "robot"
|
||||
fake_pw = "testpassword"
|
||||
mock_getuser.return_value = fake_user
|
||||
|
||||
# Create fake home dir with .elog_psi
|
||||
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
|
||||
|
||||
# Call function
|
||||
url = "http://localhost:8080/demo"
|
||||
elog_instance, returned_user = get_default_elog_instance(url)
|
||||
|
||||
# Assert user and password used correctly
|
||||
assert returned_user == fake_user
|
||||
assert hasattr(elog_instance, "post")
|
||||
|
||||
# Try a real GET request
|
||||
r = elog_instance.session.get(url)
|
||||
assert r.status_code == 200
|
||||
assert "demo" in r.text.lower()
|
||||
|
||||
# Cleanup
|
||||
pw_file.unlink()
|
||||
tmp_home.rmdir()
|
||||
|
||||
|
||||
def test_post():
|
||||
elog = get_test_elog()
|
||||
|
||||
title = "AUTHOR_OVERRIDE_TEST"
|
||||
text = "This entry should use the custom author"
|
||||
author = "robot"
|
||||
|
||||
resp = elog.post(Title=title, Text=text, Author=author)
|
||||
assert resp is not None
|
||||
|
||||
url = f"http://localhost:8080/demo/{resp}"
|
||||
html = requests.get(url).text
|
||||
|
||||
assert title in html, "Title 2 not found"
|
||||
assert text in html, "Text 2 not found"
|
||||
assert author in html, f"Custom author '{custom_author}' not found"
|
||||
|
||||
|
||||
@patch("your_module.elog_wrapper.Screenshot")
|
||||
def test_screenshot(mock_screenshot_class):
|
||||
# Create fake screenshot file
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
||||
fake_path = tmp.name
|
||||
tmp.write(b"fake image data")
|
||||
|
||||
# Configure mock to return our fake file
|
||||
mock_instance = mock_screenshot_class.return_value
|
||||
mock_instance.shoot.return_value = [fake_path]
|
||||
|
||||
elog = get_test_elog()
|
||||
|
||||
# Use a fixed message and run screenshot()
|
||||
test_msg = "SCREENSHOT_INTEGRATION_TEST_MSG_456"
|
||||
entry_id = elog.screenshot(message=test_msg)
|
||||
|
||||
assert entry_id is not None, "No entry ID returned from screenshot()"
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user