This commit is contained in:
+24
-34
@@ -1,15 +1,14 @@
|
||||
import os
|
||||
import tempfile
|
||||
import requests
|
||||
import getpass
|
||||
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
|
||||
|
||||
# To havec the found password also and test it
|
||||
|
||||
# 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"]
|
||||
@@ -27,9 +26,11 @@ def get_default_elog_password(url, **kwargs):
|
||||
return kwargs["password"]
|
||||
|
||||
|
||||
# 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"
|
||||
@@ -40,18 +41,15 @@ def test_get_default_elog_instance_with_direct_password_and_real_check():
|
||||
assert returned_user == user
|
||||
assert hasattr(elog_instance, "post")
|
||||
|
||||
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))
|
||||
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")
|
||||
mock_home.return_value = Path("/does/not/exist") # Fausse home → lecture échoue
|
||||
mock_getpass.return_value = "testpassword"
|
||||
password = mock_getpass.return_value
|
||||
|
||||
url = "http://localhost:8080/demo"
|
||||
user = "robot"
|
||||
@@ -61,54 +59,49 @@ def test_get_default_elog_instance_asks_password_and_opens(mock_home, mock_getpa
|
||||
assert returned_user == user
|
||||
assert hasattr(elog_instance, "post")
|
||||
|
||||
pw = get_default_elog_password(url, user=user)
|
||||
|
||||
r = requests.get(url, auth=(user, pw))
|
||||
assert r.status_code == 200
|
||||
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):
|
||||
# Setup
|
||||
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
|
||||
|
||||
# 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:
|
||||
elog_instance, returned_user = get_default_elog_instance(url)
|
||||
|
||||
pw = get_default_elog_password(url)
|
||||
assert returned_user == fake_user
|
||||
assert hasattr(elog_instance, "post")
|
||||
|
||||
r = requests.get(url, auth=(user, pw))
|
||||
assert r.status_code == 200
|
||||
r = requests.get(url, auth=(fake_user, fake_pw))
|
||||
assert r.status_code == 200
|
||||
|
||||
# Cleanup
|
||||
pw_file.unlink()
|
||||
tmp_home.rmdir()
|
||||
finally:
|
||||
pw_file.unlink(missing_ok=True)
|
||||
tmp_home.rmdir()
|
||||
|
||||
|
||||
def test_post():
|
||||
elog = get_test_elog()
|
||||
|
||||
title = "AUTHOR_OVERRIDE_TEST"
|
||||
message = "This is a message"
|
||||
text = "This is a message"
|
||||
author = "robot"
|
||||
|
||||
resp = elog.post(message, Title=title, Author=author, Category="General", Type="Test")
|
||||
resp = elog.post(text, Title=title, Author=author, Category="General", Type="Test")
|
||||
assert resp is not None
|
||||
|
||||
url = f"http://localhost:8080/demo/{resp}"
|
||||
@@ -121,22 +114,19 @@ def test_post():
|
||||
|
||||
@patch("slic.utils.elog.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)
|
||||
entry_id = elog.screenshot(message=test_msg)
|
||||
|
||||
assert entry_id is not None, "No entry ID returned from screenshot()"
|
||||
assert entry_id is not None
|
||||
|
||||
url = f"http://localhost:8080/demo/{entry_id}"
|
||||
html = requests.get(url).text
|
||||
|
||||
Reference in New Issue
Block a user