import os import sys import tempfile import requests from elog import LogbookAuthenticationError 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 from bs4 import BeautifulSoup def test_get_default_elog_instance_with_direct_password_and_real_check(): url = "http://localhost:8080/demo" user = "robot" password = "testpassword" text = "This is a message" elog = Elog("http://localhost:8080/demo", user=user, password=password) try: elog.post(text) except Exception as e: pytest.fail(f"elog.open() raised an unexpected exception: {e}") response = requests.get(url) print("Texte bruuuut :", response.content) soup = BeautifulSoup(response.content, 'html.parser') content = soup.get_text() print("Texte brut :", content) assert 'This\xa0is\xa0a\xa0message' in content, "Message not found" assert 'robot' in content, f"Author not found" def test_get_default_elog_instance_with_wrong_password_and_real_check(): url = "http://localhost:8080/demo" user = "robot" wrong_password = "wrongpassword" elog = Elog(url, user=user, password=wrong_password) with pytest.raises(LogbookAuthenticationError): elog.post("This should fail due to wrong password") @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" user = "robot" text = "This is a message" url = "http://localhost:8080/demo" elog = Elog("http://localhost:8080/demo", user=user) try: elog.post(text) except Exception as e: pytest.fail(f"elog.open() raised an unexpected exception: {e}") response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') content = soup.get_text() assert 'This\xa0is\xa0a\xa0message' in content, "Message not found" assert 'robot' in content, f"Author not found" @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 text = "This is a message" url = "http://localhost:8080/demo" 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 try: elog = Elog("http://localhost:8080/demo") try: elog.post(text) except Exception as e: pytest.fail(f"elog.open() raised an unexpected exception: {e}") response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') content = soup.get_text() assert 'This\xa0is\xa0a\xa0message' in content, "Message not found" assert 'robot' in content, f"Author not found" 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, attributes = {"Author": author}) url = "http://localhost:8080/demo" ''' @patch("slic.utils.elog.Screenshot") def test_screenshot(mock_screenshot_class): fake_path = "/tmp/fake_screenshot.png" with open(fake_path, "wb") as f: f.write(b"fake image data") mock_instance = mock_screenshot_class.return_value mock_instance.shoot.return_value = [fake_path] elog = elog = Elog("http://localhost:8080/demo", user="robot", password="testpassword") test_msg = "SCREENSHOT_INTEGRATION_TEST_MSG_456" elog.screenshot(message=test_msg) url = "http://localhost:8080/demo" response = requests.get(url) print("Texte bruuuut :", response.content) soup = BeautifulSoup(response.content, 'html.parser') content = soup.get_text() print("Texte brut :", content) filename = os.path.basename(fake_path) print("FILE NAME : ", filename) assert test_msg in content, "Message not found" assert filename in content, f"Attachment '{filename}' not found in post HTML" os.remove(fake_path)