78 lines
3.3 KiB
Python
78 lines
3.3 KiB
Python
|
|
"""Integration tests against a real (local) LimeSurvey instance.
|
||
|
|
|
||
|
|
Skipped by default (pyproject: addopts = -m "not integration").
|
||
|
|
Run them manually with the local endpoint via environment variables:
|
||
|
|
|
||
|
|
LIMESURVEY_BASE_URL='https://localhost/limesurvey/index.php?r=admin/remotecontrol' \
|
||
|
|
LIMESURVEY_USERNAME='...' LIMESURVEY_PASSWORD='...' \
|
||
|
|
.venv/bin/python -m pytest -m integration -v
|
||
|
|
|
||
|
|
Without those variables the tests are skipped.
|
||
|
|
Optional: LIMESURVEY_VERIFY_SSL=false for a test instance with a wrong certificate.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import os
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from pylimerpc import LsClient, LsConfig
|
||
|
|
from pylimerpc.errors import LsApiError
|
||
|
|
from pylimerpc.utils import parse_responses_json
|
||
|
|
|
||
|
|
pytestmark = pytest.mark.integration
|
||
|
|
|
||
|
|
|
||
|
|
def _config_from_env() -> LsConfig:
|
||
|
|
base_url = os.environ.get("LIMESURVEY_BASE_URL", "").strip()
|
||
|
|
username = os.environ.get("LIMESURVEY_USERNAME", "").strip()
|
||
|
|
password = os.environ.get("LIMESURVEY_PASSWORD", "").strip()
|
||
|
|
if not (base_url and username and password):
|
||
|
|
pytest.skip(
|
||
|
|
"Set LIMESURVEY_BASE_URL, LIMESURVEY_USERNAME and LIMESURVEY_PASSWORD "
|
||
|
|
"(local LimeSurvey endpoint) to run the integration tests"
|
||
|
|
)
|
||
|
|
timeout = int(os.environ.get("LIMESURVEY_TIMEOUT_SECONDS", "30") or 30)
|
||
|
|
verify_ssl = os.environ.get("LIMESURVEY_VERIFY_SSL", "true").strip().lower() not in ("0", "false", "no", "off")
|
||
|
|
return LsConfig(base_url=base_url, username=username, password=password, timeout=timeout, verify_ssl=verify_ssl)
|
||
|
|
|
||
|
|
|
||
|
|
def test_sessie_openen_list_surveys_eigenschappen_en_sluiten():
|
||
|
|
cfg = _config_from_env()
|
||
|
|
with LsClient(cfg) as ls:
|
||
|
|
assert ls.session_key, "no session key received"
|
||
|
|
surveys = ls.list_surveys()
|
||
|
|
assert isinstance(surveys, list)
|
||
|
|
print(f"\n{len(surveys)} surveys gevonden:")
|
||
|
|
for s in surveys[:10]:
|
||
|
|
print(f" sid={s.get('sid')} | active={s.get('active')} | {s.get('surveyls_title')}")
|
||
|
|
if surveys:
|
||
|
|
sid = surveys[0].get("sid")
|
||
|
|
props = ls.get_survey_properties(sid)
|
||
|
|
assert isinstance(props, dict)
|
||
|
|
assert props.get("sid") in (sid, str(sid), int(str(sid)) if str(sid).isdigit() else sid)
|
||
|
|
print(f" properties van sid={sid}: {sorted(props)[:10]} ...")
|
||
|
|
# context manager released the session cleanly
|
||
|
|
assert ls.session_key is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_export_responses_en_statistieken_readonly():
|
||
|
|
"""Read-only export against the real instance (no data modified)."""
|
||
|
|
cfg = _config_from_env()
|
||
|
|
with LsClient(cfg) as ls:
|
||
|
|
surveys = ls.list_surveys()
|
||
|
|
if not surveys:
|
||
|
|
pytest.skip("no surveys on this instance")
|
||
|
|
sid = surveys[0]["sid"]
|
||
|
|
data = ls.export_responses(sid, doc_type="json")
|
||
|
|
assert isinstance(data, bytes)
|
||
|
|
rows = parse_responses_json(data.decode("utf-8")) if data else []
|
||
|
|
print(f"\nexport_responses sid={sid}: {len(rows)} response-rows, {len(data)} bytes")
|
||
|
|
try:
|
||
|
|
stats = ls.export_statistics(sid, doc_type="json")
|
||
|
|
except LsApiError as exc:
|
||
|
|
# server may return an empty statistics export (statistics off / no permission)
|
||
|
|
pytest.skip(f"export_statistics returned an empty export on this instance: {exc}")
|
||
|
|
assert isinstance(stats, bytes)
|
||
|
|
print(f"export_statistics sid={sid}: {len(stats)} bytes")
|