83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Test-Skript fuer aza_consent.py"""
|
|
import os, sys
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from aza_consent import (
|
|
has_valid_consent, record_consent, record_revoke,
|
|
get_consent_status, get_user_history, export_consent_log,
|
|
verify_chain_integrity, _CONSENT_FILE,
|
|
)
|
|
|
|
# Sicherstellen: kein altes Log
|
|
if _CONSENT_FILE.exists():
|
|
_CONSENT_FILE.unlink()
|
|
|
|
uid = "TestUser"
|
|
passed = 0
|
|
failed = 0
|
|
|
|
def check(name, condition):
|
|
global passed, failed
|
|
if condition:
|
|
print(f" PASS: {name}")
|
|
passed += 1
|
|
else:
|
|
print(f" FAIL: {name}")
|
|
failed += 1
|
|
|
|
print("=== TEST 1: Ohne Consent -> blockiert ===")
|
|
check("has_valid_consent == False", not has_valid_consent(uid))
|
|
|
|
print("\n=== TEST 2: Consent erteilen -> erlaubt ===")
|
|
entry = record_consent(uid, source="test")
|
|
check("action == grant", entry["action"] == "grant")
|
|
check("has_valid_consent == True", has_valid_consent(uid))
|
|
check("hash vorhanden", len(entry.get("hash", "")) == 64)
|
|
|
|
print("\n=== TEST 3: Widerruf -> blockiert ===")
|
|
entry2 = record_revoke(uid, source="test")
|
|
check("action == revoke", entry2["action"] == "revoke")
|
|
check("has_valid_consent == False", not has_valid_consent(uid))
|
|
|
|
print("\n=== TEST 4: Erneuter Consent -> erlaubt ===")
|
|
record_consent(uid, source="test")
|
|
check("has_valid_consent == True", has_valid_consent(uid))
|
|
|
|
print("\n=== TEST 5: Hash-Kette (Integritaet) ===")
|
|
ok, errors = verify_chain_integrity()
|
|
check("Kette intakt", ok)
|
|
if errors:
|
|
for e in errors:
|
|
print(f" {e}")
|
|
|
|
print("\n=== TEST 6: Status-Abfrage ===")
|
|
status = get_consent_status(uid)
|
|
check("has_consent == True", status["has_consent"])
|
|
check("version_match == True", status["version_match"])
|
|
check("last_grant vorhanden", status["last_grant"] is not None)
|
|
|
|
print("\n=== TEST 7: Export ===")
|
|
path = export_consent_log()
|
|
check("Exportdatei existiert", os.path.exists(path))
|
|
os.remove(path)
|
|
|
|
print("\n=== TEST 8: User-Historie ===")
|
|
hist = get_user_history(uid)
|
|
check(f"3 Eintraege (gefunden: {len(hist)})", len(hist) == 3)
|
|
|
|
print("\n=== TEST 9: Anderer User -> kein Consent ===")
|
|
check("User2 hat keinen Consent", not has_valid_consent("User2"))
|
|
|
|
# Cleanup
|
|
if _CONSENT_FILE.exists():
|
|
_CONSENT_FILE.unlink()
|
|
|
|
print(f"\n{'='*50}")
|
|
print(f"ERGEBNIS: {passed} PASS, {failed} FAIL")
|
|
if failed == 0:
|
|
print("ALLE TESTS BESTANDEN")
|
|
else:
|
|
sys.exit(1)
|