124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
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 message1"
|
|
|
|
elog = Elog("http://localhost:8080/demo", user=user, password=password)
|
|
|
|
try:
|
|
msg_id = elog.post(text)
|
|
except Exception as e:
|
|
pytest.fail(f"elog.post() raised an unexpected exception: {e}")
|
|
|
|
message, attributes, attachments = elog.read(msg_id)
|
|
|
|
assert text in message
|
|
assert attributes.get("Author") == user
|
|
|
|
|
|
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(Exception):
|
|
elog.post("Should fail because 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 message2"
|
|
url = "http://localhost:8080/demo"
|
|
|
|
elog = Elog("http://localhost:8080/demo", user=user)
|
|
|
|
try:
|
|
msd_id = elog.post(text)
|
|
except Exception as e:
|
|
pytest.fail(f"elog.post() raised an unexpected exception: {e}")
|
|
|
|
try:
|
|
msg_id = elog.post(text)
|
|
except Exception as e:
|
|
pytest.fail(f"elog.post() raised an unexpected exception: {e}")
|
|
|
|
message, attributes, attachments = elog.read(msg_id)
|
|
|
|
assert text in message
|
|
assert attributes.get("Author") == user
|
|
|
|
|
|
@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 message3"
|
|
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:
|
|
msg_id = elog.post(text)
|
|
except Exception as e:
|
|
pytest.fail(f"elog.post() raised an unexpected exception: {e}")
|
|
|
|
message, attributes, attachments = elog.read(msg_id)
|
|
|
|
assert text in message
|
|
assert attributes.get("Author") == fake_user
|
|
|
|
finally:
|
|
pw_file.unlink(missing_ok=True)
|
|
tmp_home.rmdir()
|
|
|
|
|
|
@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("http://localhost:8080/demo", user="robot", password="testpassword")
|
|
|
|
test_msg = "SCREENSHOT_INTEGRATION_TEST_MSG_456"
|
|
msg_id = elog.screenshot(message=test_msg)
|
|
|
|
filename = os.path.basename(fake_path)
|
|
message, attributes, attachments = elog.read(msg_id)
|
|
|
|
assert test_msg in message
|
|
assert any(a.endswith(filename) for a in [os.path.basename(att) for att in attachments])
|
|
|
|
os.remove(fake_path) |